There are n flowers, each flower has three attributes: flower shape (s), color (c), smell (m), expressed by three integers.
Now to rate each flower, the level of a flower is that it has more beautiful flowers than it can have.
Define that a flower A is more beautiful than another flower B if and only if Sa >= Sb, Ca >= Cb, Ma >= Mb.
Obviously, two flowers may have the same attributes. The number of flowers for each grade needs to be counted.
Input
The first behavior N, K (1 <= N <= 100,000, 1 <= K <= 200,000) denotes the number of flowers and the maximum attribute value, respectively.
The following N lines, each with three integers si, ci, mi (1 <= si, ci, mi <= K), represent the attributes of the first flower
Output
Contains N rows, representing the number of flowers per level with a rating of 0...N-1, respectively.
Sample Input
10 3 3 3 3 2 3 3 2 3 1 3 1 1 3 1 2 1 3 1 1 1 2 1 2 2 1 3 2 1 2 1
Sample Output
3 1 3 0 1 0 1 0 0 1
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1e5+100; const int maxk=2e5+100; struct node{ int a; int b; int c; int id; }a[maxn],tmp[maxn]; int n,k,cnt,tree[maxk],ans[maxn],res[maxk]; int lowbit(int x) { return x&(-x); } void update(int tar,int val) { for(int i=tar;i<=k;i+=lowbit(i)) tree[i]+=val; } int query(int tar) { int res=0; for(int i=tar;i>0;i-=lowbit(i)) res+=tree[i]; return res; } bool cmp(node x,node y) { if(x.a<y.a) return 1; if(x.a>y.a) return 0; if(x.b<y.b) return 1; if(x.b>y.b) return 0; if(x.c<y.c) return 1; return 0; } void CDQ(int l,int r) { if(l==r) return ; int mid=(l+r)>>1,p=l,q=mid+1,cont=l-1; CDQ(l,mid);CDQ(mid+1,r); while(p<=mid&&q<=r) { if(a[p].b<=a[q].b) { update(a[p].c,1); tmp[++cont]=a[p++]; } else { ans[a[q].id]+=query(a[q].c); tmp[++cont]=a[q++]; } } while(q<=r) ans[a[q].id]+=query(a[q].c),tmp[++cont]=a[q++]; for(int i=l;i<p;i++) update(a[i].c,-1); while(p<=mid) tmp[++cont]=a[p++]; for(int i=l;i<=r;i++) a[i]=tmp[i]; } int main() { cnt=1; memset(ans,0,sizeof(ans)); memset(res,0,sizeof(res)); scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c); a[i].id=i; } sort(a+1,a+1+n,cmp); for(int i=n;i>=1;i--) { if(a[i].a==a[i+1].a&&a[i].b==a[i+1].b&&a[i].c==a[i+1].c) ans[a[i].id]+=cnt++; else cnt=1; } CDQ(1,n); for(int i=1;i<=n;i++) res[ans[i]]++; for(int i=0;i<n;i++) printf("%d\n",res[i]); return 0; }