CDQ分治 陌上花开 HYSBZ - 3262 三维

题目链接:https://vjudge.net/problem/HYSBZ-3262
题意:三维偏序,有重复。
转自:https://zhuanlan.zhihu.com/p/55322598
思路:先用sort对a排序,然后用归并排序的思路对b排序的同时对c用树状数组统计贡献。
第一维是a,第二维是b,第三维是c。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,m,c[maxn*2],ans[maxn],cnt;
struct NODE
{
    int a,b,c,w,f;//w为相同的数量 f为符合题意且不与本体相同的个数
} e[maxn],t[maxn];
bool cmp(NODE a,NODE b)
{
    if(a.a!=b.a)
        return a.a<b.a;
    if(a.b!=b.b)
        return a.b<b.b;
    return a.c<b.c;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int y)
{
    for(; x<=m; x+=lowbit(x))
        c[x]+=y;
}
int sum(int x)
{
    int ans=0;
    for(; x; x-=lowbit(x))
        ans+=c[x];
    return ans;
}
void CDQ (int l,int r)
{
    int mid=(l+r)/2;
    if(l==r)
        return ;
    CDQ(l,mid);
    CDQ(mid+1,r);
    int p=l,q=mid+1,tot=l;
    while(p<=mid&&q<=r)
    {
        if(e[p].b<=e[q].b)
        {
            update(e[p].c,e[p].w);
            t[tot++]=e[p++];
        }
        else
        {
            e[q].f+=sum(e[q].c);
            t[tot++]=e[q++];
        }
    }
    while(p<=mid)
    {
        update(e[p].c,e[p].w);
        t[tot++]=e[p++];
    }
    while(q<=r)
    {
        e[q].f+=sum(e[q].c);
        t[tot++]=e[q++];
    }
    for(int i=l; i<=mid; i++)//清空树状数组  memset会tle
    {
        update(e[i].c,-e[i].w);
    }
    for(int i=l; i<=r; i++)
    {
        e[i]=t[i];
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
        e[i].w=1;
    }
    sort(e+1,e+1+n,cmp);
    cnt=1;
    for(int i=2; i<=n; i++)//处理重复
    {
        if(e[i].a==e[cnt].a&&e[i].b==e[cnt].b&&e[i].c==e[cnt].c)
        {
            e[cnt].w++;
        }
        else
        {
            e[++cnt]=e[i];
        }
    }
    CDQ(1,cnt);
    for(int i=1; i<=cnt; i++)//统计答案
    {
        ans[e[i].f+e[i].w-1]+=e[i].w;
    }
    for(int i=0; i<n; i++)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值