核心思想:在归并排序的过程中利用树状数组来统计答案(每次只分治右边对左边逆序对产生的贡献,其余递归处理即可,利用双指针的方法对于左边区间加入树状数组,对于右边区间query统计答案)
需要注意值相同时的处理方式
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #define IOS ios_base:sync_with_stdio(0); cin.tie(0); #define ll long long #define INF 0x3f3f3f3f #define MEM(x,y) memset(x,y,sizeof(x)) #define int long long #define rep(i , a , b) for(int i = a ; i <= b ; i ++) #define P pair<int,int> #define sc(a) scanf("%lld",&a) #define pf(a) printf("%lld ",a) using namespace std; const int N = 1000010, M = 200010; int n, m; struct Data { int a, b, c, s, res; bool operator< (const Data& t) const { if (a != t.a) return a < t.a; if (b != t.b) return b < t.b; return c < t.c; } bool operator== (const Data& t) const { return a == t.a && b == t.b && c == t.c; } }q[N], w[N]; int tr[M], ans[N]; int lowbit(int x) { return x & -x; } void add(int x, int v) { for (int i = x; i < M; i += lowbit(i)) tr[i] += v; } int query(int x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += tr[i]; return res; } void merge_sort(int l, int r) { if (l >= r) return; int mid = l + r >> 1; merge_sort(l, mid), merge_sort(mid + 1, r); int i = l, j = mid + 1, k = 0; while (i <= mid && j <= r) if (q[i].b <= q[j].b) add(q[i].c, q[i].s), w[k ++ ] = q[i ++ ]; else q[j].res += query(q[j].c), w[k ++ ] = q[j ++ ]; while (i <= mid) add(q[i].c, q[i].s), w[k ++ ] = q[i ++ ]; while (j <= r) q[j].res += query(q[j].c), w[k ++ ] = q[j ++ ]; for (i = l; i <= mid; i ++ ) add(q[i].c, -q[i].s); for (i = l, j = 0; j < k; i ++, j ++ ) q[i] = w[j]; } signed main() { cin >> n >> m; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; q[i] = { a, b, c, 1 }; } sort(q, q + n); int k = 1; for (int i = 1; i < n; i++) if (q[i] == q[k - 1]) q[k - 1].s++; else q[k++] = q[i]; merge_sort(0, k - 1); for (int i = 0; i < k; i++) ans[q[i].res + q[i].s - 1] += q[i].s; for (int i = 0; i < n; i++) printf("%d ", ans[i]); }