C++ AC code (Inversion number using Segment tree)


  • 0
    B

    用线段树求逆序数。。

    class Solution {
    private:
    	typedef vector<int> vec;
    public:
    	vec countSmaller(vec& nums) {
    		vec ans;
    		int n = 0;
    		if (!(n = nums.size())) return ans;
    		ans.resize(n, 0);
    		ret = new P[n + 10];
    		for (int i = 1; i <= n; i++) ret[i] = P(nums[i - 1], i);
    		sort(ret + 1, ret + n + 1);
    		seg = new int[(n + 10) << 2];
    		memset(seg, 0, sizeof(int)* ((n + 10) << 2));
    		for (int i = 1; i <= n; i++) {
    			int v = query(1, 1, n, ret[i].id, n);
    			insert(1, 1, n, ret[i].id);
    			ans[ret[i].id - 1] = v;
    		}
    		delete[]seg; delete[]ret;
    		return ans;
    	}
    private:
    	struct P {
    		int v, id;
    		P(int _i_ = 0, int _j_ = 0) :v(_i_), id(_j_) {}
    		friend bool operator<(const P &a, const P &b) {
    			return a.v == b.v ? a.id < b.id : a.v < b.v;
    		}
    	}*ret;
    	int *seg;
    	inline void insert(int root, int l, int r, int p) {
    		if (p > r || p < l) return;
    		if (p <= l && p >= r) { seg[root]++; return; }
    		int mid = (l + r) >> 1;
    		insert(root << 1, l, mid, p);
    		insert(root << 1 | 1, mid + 1, r, p);
    		seg[root] = seg[root << 1] + seg[root << 1 | 1];
    	}
    	inline int query(int root, int l, int r, int x, int y) {
    		if (x > r || y < l) return 0;
    		if (x <= l && y >= r) return seg[root];
    		int ret = 0;
    		int mid = (l + r) >> 1;
    		ret += query(root<<1, l, mid, x, y);
    		ret += query(root << 1 | 1, mid + 1, r, x, y);
    		return ret;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.