https://cn.vjudge.net/contest/323709#problem/E
有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),用三个整数表示。
现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。
定义一朵花A比另一朵花B要美丽,当且仅Sa>=Sb,Ca>=Cb,Ma>=Mb。
显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。
Input
第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值。
以下N行,每行三个整数si, ci, mi (1 <= si, ci, mi <= K),表示第i朵花的属性
Output
包含N行,分别表示评级为0...N-1的每级花的数量。
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
三维偏序模板题,一维排序,二维CDQ分治,三维树状数组,注意细节(哭了)
#include<bits/stdc++.h>
//#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int M=2e5+5;
struct node{
int s,c,m;
int id,num;
bool operator<(const node &a)const{
if(s!=a.s) return s<a.s;
else if(c!=a.c) return c<a.c;
else return m<a.m;
}
}q[N],tmp[N];
bool equal(node a,node b){
if(a.s!=b.s) return false;
if(a.c!=b.c) return false;
if(a.m!=b.m) return false;
return true;
}
int sum[M];
inline int lowbit(int x){
return x&(-x);
}
void update(int x,int val){
while(x<M){
sum[x]+=val;
x+=lowbit(x);
}
}
int query(int x){
int res=0;
while(x){
res+=sum[x];
x-=lowbit(x);
}
return res;
}
void sum_clear(int x){
while(x<M){
sum[x]=0;
x+=lowbit(x);
}
}
int rk[N];
int ans[N];
void CDQ(int L,int R){
if(R-L<=1){
rk[q[L].id]=q[L].num-1;
return ;
}
int mid=(L+R)>>1;
CDQ(L,mid);CDQ(mid,R);
int l=L,r=mid;
int cnt=0;
while(l<mid&&r<R){
if(q[l].c<=q[r].c){
update(q[l].m,q[l].num);
tmp[cnt++]=q[l++];
}
else{
rk[q[r].id]+=query(q[r].m);
tmp[cnt++]=q[r++];
}
}
while(l<mid) tmp[cnt++]=q[l++];
while(r<R){
rk[q[r].id]+=query(q[r].m);
tmp[cnt++]=q[r++];
}
for(int i=0;i<cnt;i++){
q[L+i]=tmp[i];
sum_clear(tmp[i].m);
}
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&q[i].s,&q[i].c,&q[i].m);
}
sort(q+1,q+1+n);
q[0].s=q[0].c=q[0].m=-1;
int tot=1;
for(int i=1;i<=n;i++){
if(equal(q[i],q[tot-1])){
q[tot-1].num++;
continue;
}
q[tot]=q[i];
q[tot].num=1;
q[tot].id=tot;
tot++;
}
CDQ(1,tot);
for(int i=1;i<tot;i++){
ans[rk[q[i].id]]+=q[i].num;
}
for(int i=0;i<n;i++) printf("%d\n",ans[i]);
return 0;
}
/*
10 3
2 3 3
3 2 2
3 3 3
3 2 1
2 2 1
1 2 2
3 2 1
2 2 3
2 2 3
3 2 1
2
0
0
5
1
1
0
0
0
1
*/