cdq分治
三维偏序问题
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int ans[maxn], c[maxn];
int tot, n, m;
struct node{ int a, b, c, cnt, ans; } q[maxn], p[maxn], tmp[maxn];
inline int read()
{
int s = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ (s *= 10) += ch-'0'; ch = getchar(); }
return s*f;
}
bool cmp1(node x,node y)
{
if(x.a == y.a && x.b == y.b) return x.c < y.c;
else if(x.a == y.a) return x.b < y.b;
return x.a < y.a;
}
bool cmp2(node x,node y)
{
return x.b == y.b ? x.c < y.c : x.b < y.b;
}
int lowbit(int x){ return x&(-x); }
void update(int k,int x)
{
for(int i = k; i <= m; i += lowbit(i)) c[i] += x;
}
int query(int k)
{
int res = 0;
for(int i = k; i; i -= lowbit(i)) res += c[i];
return res;
}
void cdq(int l,int r)
{
if(l == r) return ;
int mid = (l+r)>>1;
cdq(l,mid); cdq(mid+1,r);
int i = l, j = mid+1, tp = l;
while(i <= mid || j <= r){
if(j > r || (i <= mid && p[i].b <= p[j].b)) update(p[i].c,p[i].cnt),tmp[tp++] = p[i++];
else p[j].ans += query(p[j].c), tmp[tp++] = p[j++];
}
for(j = l; j < i; j++) update(p[j].c,-p[j].cnt);
for(int i = l; i <= r; i++) p[i] = tmp[i];
}
int main()
{
n = read(); m = read();
for(int i = 1; i <= n; i++){
q[i].a = read(); q[i].b = read(); q[i].c = read();
}
sort(q+1,q+n+1,cmp1);
for(int i = 1,j = 0; i <= n; i++){
j++;
if(q[i].a != q[i+1].a || q[i].b != q[i+1].b || q[i].c != q[i+1].c){
p[++tot] = q[i]; p[tot].cnt = j;
j = 0;
}
}
cdq(1,tot);
for(int i = 1; i <= tot; i++) ans[p[i].ans+p[i].cnt-1] += p[i].cnt;
for(int i = 0; i < n; i++) printf("%d\n",ans[i]);
return 0;
}