BZOJ3262: 陌上花开(CDQ分治)

来源:互联网 发布:电脑发送手机短信软件 编辑:程序博客网 时间:2024/06/05 07:41

传送门

题意:
n个三元组(a,b,c),求对于每一个iaj<=aibj<=bicj<=cij个数

题解:
三维偏序,两位CDQ解决,第三维树状数组即可。

#include<bits/stdc++.h>using namespace std;const int Maxn=1e5+50,Maxm=2e5+50;streambuf *ib,*ob;inline int read(){    char ch=ib->sbumpc();int i=0,f=1;    while(!isdigit(ch)){if(ch=='-')f=-1;ch=ib->sbumpc();}    while(isdigit(ch)){i=(i<<1)+(i<<3)+ch-'0';ch=ib->sbumpc();}    return i*f;}int buf[50];inline void W(int x){    if(!x){ob->sputc('0');return;}    if(x<0){x=-x;ob->sputc('-');}    while(x){buf[++buf[0]]=x%10,x/=10;}    while(buf[0])ob->sputc('0'+buf[buf[0]--]);}int n,k,ans[Maxn],bit[Maxm],cnt[Maxn],a[Maxn];struct node{    int a,b,c,pos;    friend inline bool operator <(const node &a,const node &b)    {        if(a.a!=b.a)return a.a<b.a;        else if(a.b!=b.b)return a.b<b.b;        else return a.c<b.c;    }    friend inline bool operator ==(const node &a,const node &b)    {        return a.a==b.a&&a.b==b.b&&a.c==b.c;    }}q[Maxn],tmp[Maxn];inline void insert(int pos,int val){for(;pos<=k;pos+=(pos&(-pos)))bit[pos]+=val;}inline int query(int pos){    int res=0;    for(;pos;pos-=(pos&(-pos)))    res+=bit[pos];    return res;}inline void solve(int l,int r){    if(l==r)return;    int mid=(l+r)>>1;    solve(l,mid);    solve(mid+1,r);    for(int i=l;i<=r;i++)tmp[i]=q[i];    int head1=l,head2=mid+1,pos=l;    while(head1<=mid&&head2<=r)    {        if(tmp[head1].b<=tmp[head2].b)        {            insert(tmp[head1].c,cnt[tmp[head1].pos]);            q[pos++]=tmp[head1++];        }        else        {            ans[tmp[head2].pos]+=query(tmp[head2].c);            q[pos++]=tmp[head2++];        }    }    while(head1<=mid)    {        insert(tmp[head1].c,cnt[tmp[head1].pos]);        q[pos++]=tmp[head1++];    }    while(head2<=r)    {        ans[tmp[head2].pos]+=query(tmp[head2].c);        q[pos++]=tmp[head2++];    }    for(int i=l;i<=mid;i++)insert(tmp[i].c,-cnt[tmp[i].pos]);}int main(){    ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);ib=cin.rdbuf();ob=cout.rdbuf();    n=read(),k=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);int t=0;    for(int i=1;i<=n;i++)    {        if(i==1||!(q[i]==q[i-1]))++t;        cnt[t]++;q[i].pos=t;    }    int m=unique(q+1,q+n+1)-q-1;    solve(1,m);    for(int i=1;i<=m;i++)a[ans[i]+cnt[i]-1]+=cnt[i];    for(int i=0;i<n;i++)W(a[i]),ob->sputc('\n');}
原创粉丝点击