3262: 陌上花开

3262: 陌上花开

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 2133  Solved: 954
[Submit][Status][Discuss]

Description

有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

HINT

1 <= N <= 100,000, 1 <= K <= 200,000

Source

 
#include<cstdio>
#include<algorithm>
#define lowbit(x) (x&(-x))
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=2e5+5;
struct node{int opt,x,y,z,cnt,id;}q[N],tq[N];
int n,m,tot,sz,c[N],ans[N],Ans[N];
bool operator ==(const node &a,const node &b){
    return a.x==b.x&&a.y==b.y&&a.z==b.z;
}
inline bool cmp1(const node &a,const node &b){
    return a.x!=b.x?a.x<b.x:
           a.y!=b.y?a.y<b.y:
           a.z<b.z;
}
inline bool cmp2(const node &a,const node &b){
    return a.y!=b.y?a.y<b.y:
           a.id<b.id;
}
void update(int p,int v){
    for(int i=p;i<=n;i+=lowbit(i)) c[i]+=v;
}
int query(int p){
    int res=0;
    for(int i=p;i;i-=lowbit(i)) res+=c[i];
    return res;
}
void CDQ(int l,int r){
    if(l==r) return ;
    int mid=l+r>>1,ql=l,qr=mid+1;
    for(int i=l;i<=mid;i++) q[i].opt=1;
    for(int i=mid+1;i<=r;i++) q[i].opt=2;
    for(int i=l;i<=r;i++) tq[i]=q[i];
    sort(tq+l,tq+r+1,cmp2);
    for(int i=l;i<=r;i++){
        if(tq[i].opt==1) update(tq[i].z,tq[i].cnt);
        if(tq[i].opt==2) ans[tq[i].id]+=query(tq[i].z);
    }
    for(int i=l;i<=r;i++){
        if(tq[i].opt==1) update(tq[i].z,-tq[i].cnt);
    }
    CDQ(l,mid);
    CDQ(mid+1,r);
}
int main(){
    m=read();n=read();
    for(int i=1;i<=m;i++){
        tq[i].x=read();
        tq[i].y=read();
        tq[i].z=read();
        tq[i].cnt=1;
    }
    sort(tq+1,tq+m+1,cmp1);
    for(int i=1;i<=m;i++){
        if(tq[i]==tq[i-1]) q[tot].cnt++;
        else q[++tot]=tq[i],q[tot].id=++sz;
    }
    sort(q+1,q+tot+1,cmp1);
    CDQ(1,tot);
    for(int i=1;i<=tot;i++) Ans[ans[q[i].id]+q[i].cnt-1]+=q[i].cnt;
    for(int i=0;i<m;i++) printf("%d
",Ans[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/shenben/p/6772545.html