题意: 问每个点的等级
CDQ 注意处理一下完全相等的点即可
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) #define pb push_back #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) #define lson l,m,pos<<1 #define rson m+1,r,pos<<1|1 typedef pair<int,int>pii; ////////////////////////////////// const int N=1e6+10; int M,t[N],n,cnt,num,ans[N],maxx; int lowbit(int i){return i&(-i);} void add(int x,int v){for(;x<=100000+5;x+=lowbit(x))t[x]+=v;} int get(int x){int ans=0;for(;x;x-=lowbit(x)) ans+=t[x];return ans;} struct node { int x,y,z,w,ans,id; }s[N]; bool cmp(node a,node b) { return a.x==b.x?a.y==b.y?a.z<b.z:a.y<b.y:a.x<b.x; } bool cmpy(node a,node b) { return a.y<b.y||a.y==b.y&&a.z<b.z; } void cbq(int l,int r) { if(l==r)return ; int mid=(l+r)>>1; cbq(l,mid);cbq(mid+1,r); sort(s+l,s+mid+1,cmpy);sort(s+mid+1,s+r+1,cmpy); int j=l; rep(i,mid+1,r) { while(s[j].y<=s[i].y&&j<=mid) add(s[j].z,1),j++; ans[s[i].id]+=get(s[i].z); } rep(i,l,j-1)//这里j-1 不能改成mid add(s[i].z,-1); } void sol() { RI(n);//CLR(t,0); rep(i,1,n){RIII(s[i].x,s[i].y,s[i].z);s[i].id=i;ans[i]=0; } sort(s+1,s+1+n,cmp); cbq(1,n); repp(i,n-1,1) if(s[i].x==s[i+1].x&&s[i].y==s[i+1].y&&s[i].z==s[i+1].z) ans[s[i].id]=ans[s[i+1].id]; rep(i,1,n) printf("%d ",ans[i]); } int main() { int cas;RI(cas); while(cas--)sol(); return 0; }