题解:三维偏序(陌上花开)【CDQ分治】

20 篇文章 0 订阅
12 篇文章 0 订阅

题意:求所有的    x j &lt; x i    y j &lt; y i    z j &lt; z i    ~~x_j&lt;x_i~~y_j&lt;y_i~~z_j&lt;z_i~~   xj<xi  yj<yi  zj<zi  的 j 的个数

于是,我们就需要用到强大的CDQ分治

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;

#define re register
#define gc getchar()
inline int read()
{
	re int x(0);re char ch(gc);
	while(ch<'0'||ch>'9') ch=gc;
	while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=gc;
	return x;
}

const int N=2000010;
struct node {int x,y,z,w,id;}a[N];
int n,k,w[N],c[N],sum[N],t[N];

bool cmpx(node a,node b)
{
	if(a.x!=b.x) return a.x<b.x;
	if(a.y!=b.y) return a.y<b.y;
	return a.z<b.z;
}
bool cmpy(node a,node b)
{
	if(a.y!=b.y) return a.y<b.y;
	if(a.z!=b.z) return a.z<b.z;
	return a.x<b.x;
}
int query (int x)
{
    int ans=0;
    while (x) {
        ans+=c[x];
        x-=x&-x;
    }
    return ans;
}
void add (int x,int y)
{
    while (x<=k) {
        c[x]+=y;
        x+=x&-x;
    }
}
void cdq(int l,int r)
{
	if(l==r) return;
	int mid=(l+r)>>1;
	cdq(l,mid),cdq(mid+1,r);
	sort(a+l,a+mid+1,cmpy);
    sort (a+mid+1,a+r+1,cmpy);
    int t=l;
    for(int i=mid+1;i<=r;++i)
    {
    	while(a[t].y<=a[i].y&&t<=mid)
    		add(a[t].z,a[t].w),++t;
    	sum[a[i].id]+=query(a[i].z);
	}
	for(int i=l;i<t;++i)
		add(a[i].z,-a[i].w);
}

int main()
{
	n=read(),k=read();
	for(int i=1;i<=n;++i)
	{
		a[i].x=read();
		a[i].y=read();
		a[i].z=read();
	}
	sort(a+1,a+1+n,cmpx);
	int c=n;n=0;
	for(int i=1;i<=c;++i)
	{
		if(a[i].x!=a[i-1].x||a[i].y!=a[i-1].y||a[i].z!=a[i-1].z)
			a[++n]=a[i],a[n].w=1;
		else a[n].w++;
	}
	for(int i=1;i<=n;++i)
		a[i].id=i,sum[i]=a[i].w-1;
	cdq (1,n); 
    for (int i=1;i<=n;i++) t[sum[a[i].id]]+=a[i].w;
    for (int i=1;i<=c;i++) cout<<t[i-1]<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值