三维偏序 cdq

Describe:

\(n\) 个元素,第 \(i\) 个元素有 \(a_i、b_i、c_i\) 三个属性,设 \(f(i)\) 表示满足 \(a_j \leq a_i\)\(b_j \leq b_i\)\(c_j \leq c_i\)\(j\) 的数量。

对于 \(d \in \left[0, n\right)\),求 \(f(i) = d\)\(i\) 的数量。

Solution:

终于理解 CDQ。

首先,将第一维排序,分治处理第二维,树状数组处理第三维。CDQ 就是在对第二维归并排序时,将左边对右边的影响进行统计,极其暴力的过程,但时间复杂度却只有 \(O(n \log n)\)。这就导致,CDQ 完全不像其他诸如 bitset 的用法等记忆点,顺着暴力的分治就能获得同样的时间复杂度,码量也不多,处理范围广,对于排序统计这一类问题有奇效。

Code:

bool _Start;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
namespace IO
{
	#define TP template<typename T>
	#define TP_ template<typename T,typename ... T_>
	#ifdef DEBUG
	#define gc() (getchar())
	#else
	char buf[1<<20],*p1,*p2;
	#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
	#endif
	#ifdef DEBUG
	void pc(const char &c)
	{
		putchar(c);
	}
	#else
	char pbuf[1<<20],*pp=pbuf;
	void pc(const char &c)
	{
		if(pp-pbuf==1<<20)
			fwrite(pbuf,1,1<<20,stdout),pp=pbuf;
		*pp++=c;
	}
	struct IO{~IO(){fwrite(pbuf,1,pp-pbuf,stdout);}}_;
	#endif
	TP void read(T &x)
	{
		x=0;static int f;f=0;static char ch;ch=gc();
		for(;ch<'0'||ch>'9';ch=gc())ch=='-'&&(f=1);
		for(;ch>='0'&&ch<='9';ch=gc())x=(x<<1)+(x<<3)+(ch^48);
		f&&(x=-x);
	}
	TP void write(T x)
	{
		if(x<0)
			pc('-'),x=-x;
		static T sta[35],top;top=0;
		do
			sta[++top]=x%10,x/=10;
		while(x);
		while(top)
			pc(sta[top--]^48);
	}
	TP_ void read(T &x,T_&...y){read(x);read(y...);}
	TP void writeln(const T x){write(x);pc('\n');}
	TP void writesp(const T x){write(x);pc(' ');}
	TP_ void writeln(const T x,const T_ ...y){writesp(x);writeln(y...);}
	TP void debugsp(const T x){fprintf(stderr,"%d ",x);}
	TP void debug(const T x){fprintf(stderr,"%d\n",x);}
	TP_ void debug(const T x,const T_...y){debugsp(x);debug(y...);}
	TP inline T max(const T &a,const T &b){return a>b?a:b;}
	TP_ inline T max(const T &a,const T_&...b){return max(a,max(b...));} 
	TP inline T min(const T &a,const T &b){return a<b?a:b;}
	TP_ inline T min(const T &a,const T_&...b){return min(a,min(b...));}
	TP inline void swap(T &a,T &b){static T t;t=a;a=b;b=t;}
	TP inline T abs(const T &a){return a>0?a:-a;}
	#undef TP
	#undef TP_
}
using namespace IO;
using std::cerr;
using LL=long long;
constexpr int N=1e5+5;
struct node
{
	int a,b,c,f,sam;
	friend bool operator !=(const node &x,const node &y)
	{
		return x.a!=y.a||x.b!=y.b||x.c!=y.c;
	}
}s[N],tmp[N];
int c[N<<1],n,K,buc[N];
bool cmp(const node &x,const node &y)
{
	return x.a!=y.a?x.a<y.a:(x.b!=y.b?x.b<y.b:x.c<y.c);
}
int lowbit(int x){return x&-x;}
void add(int x,int k)
{
	for(;x<=K;x+=lowbit(x))
		c[x]+=k;
}
int query(int x)
{
	int sum=0;
	for(;x;x-=lowbit(x))
		sum+=c[x];
	return sum;
}
void cdq(int l,int r)
{
	if(l==r)
		return ;
	int mid=(l+r)>>1;
	cdq(l,mid),cdq(mid+1,r);//递归分治
	for(int i=l,p1=l,p2=mid+1;i<=r;i++)
	{
		if(p2>r||(p1<=mid&&s[p1].b<=s[p2].b))//对第二维排序
			add(s[p1].c,s[p1].sam)/*加入第三维的影响*/,tmp[i]=s[p1++];
		else
			s[p2].f+=query(s[p2].c)/*统计第三维的影响*/,tmp[i]=s[p2++];
	}
	for(int i=l;i<=mid;i++)
		add(s[i].c,-s[i].sam);//消除影响
	for(int i=l;i<=r;i++)
		s[i]=tmp[i];
}
bool _End;
int main()
{
	// fprintf(stderr,"%.2 MBlf\n",(&_End-&_Start)/1048576.0);
	read(n,K);
	for(int i=1;i<=n;i++)
		read(s[i].a,s[i].b,s[i].c);
	std::sort(s+1,s+n+1,cmp);//对第一维排序
	int len=0;
	for(int i=1,sam=1;i<=n;i++,sam++)//排重
		if(s[i]!=s[i+1])
		{
			s[++len]=s[i];
			s[len].sam=sam;
			sam=0;
		}
	cdq(1,len);
	for(int i=1;i<=len;i++)
		buc[s[i].f+s[i].sam-1]+=s[i].sam;
	for(int i=0;i<n;i++)
		writeln(buc[i]);
	return 0;
}

posted @ 2024-02-17 16:45  wmtl_lofty  阅读(32)  评论(0)    收藏  举报