题解 | 维护x的秩

维护x的秩

https://www.nowcoder.com/practice/0ade0d95c85349beb934a90b1d9b02be

#include <algorithm>
#include <vector>
class Rank {
  public:
    vector<int> getRankOfNumber(vector<int> A, int n) {
        // write code here
        vector<int> sorted(A);
        sort(sorted.begin(), sorted.end());
        sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
        auto get_id = [&](int x) {
            return lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin() + 1;

        };
        vector<int> BIT(n + 2, 0);
        auto update = [&](int x) {
            while (x < BIT.size()) {
                BIT[x]++;
                x += x & -x;
            }

        };
        auto query = [&](int x) {
            int res = 0;
            while (x > 0) {
                res += BIT[x];
                x -= x & -x;
            }
            return res;
        };
        vector<int> res(n,0);
        for (int i = 0; i < n; ++i) {
            int id = get_id(A[i]);
            res[i] = query(id - 1);
            update(id);
        }
        return res;

    }
};

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
Java大菜狗:纯纯招黑奴,一天还不到两百那么多要求,还不迟到早退,以为啥啊,给一点工资做一堆活,还以不拖欠员工工资为荣,这是什么值得骄傲的事情吗,纯纯***公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务