BZOJ3262: 陌上花开(CDQ分治)

传送门

题意:
n个三元组(a,b,c),求对于每一个 i aj<=ai bj<=bi cj<=ci j <script type="math/tex" id="MathJax-Element-143">j</script>个数

题解:
三维偏序,两位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');
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值