码迷,mamicode.com
首页 > 其他好文 > 详细

陌上花开(三维偏序)

时间:2019-08-02 22:19:35      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:main   getc   ons   name   csharp   c99   lowbit   归并   template   

陌上花开,缓缓归矣——赠戴氏夫人  

CDQ模板

一维sort,二维归并,三维树状数组

二维归并时,维护所求值

#include<bits/stdc++.h>
#define re return
#define lowbit(x) (x&(-x))
#define inc(i,l,r) for(int i=l;i<=r;++i)
typedef long long ll;
const int maxn=200005,maxm=400005;
using namespace std;
template<typename T>inline void rd(T&x)
{
	char c;bool f=0;
	while((c=getchar())<‘0‘||c>‘9‘)if(c==‘-‘)f=1;
	x=c^48;
	while((c=getchar())>=‘0‘&&c<=‘9‘)x=x*10+(c^48);
	if(f)x=-x; 
} 

int n,m,c[maxm],ans[maxn];

struct node{
	int a,b,c,w,f;
	bool operator<(node x)const 
	{
		if(a!=x.a)re a<x.a;
		if(b!=x.b)re b<x.b;
		re c<x.c;
	}
	inline void read()
	{
		rd(a),rd(b),rd(c);
	}
	bool operator!=(node x)const 
	{
		re ((a!=x.a)||(b!=x.b)||(c!=x.c));
	}
}h[maxn],t[maxn]; 

inline void add(int x,int y)
{
	while(x<=m){c[x]+=y;x+=lowbit(x);}
}

inline ll sum(int x)
{
	ll res=0;
	while(x){res+=c[x];x-=lowbit(x);}
	re res;
}


inline void CDQ(int l,int r)
{
	if(l==r)re ;
	int mid=(l+r)>>1;
	CDQ(l,mid);CDQ(mid+1,r);
	int p=l,q=mid+1,tot=l;
	while(p<=mid&&q<=r)
	{
		if(h[p].b<=h[q].b)add(h[p].c,h[p].w),t[tot++]=h[p++];
		else h[q].f+=sum(h[q].c),t[tot++]=h[q++];
	}
	while(p<=mid)add(h[p].c,h[p].w),t[tot++]=h[p++];
	while(q<=r)h[q].f+=sum(h[q].c),t[tot++]=h[q++];
	inc(i,l,mid)add(h[i].c,-h[i].w);
	inc(i,l,r)h[i]=t[i];
}
int main()
{
	rd(n),rd(m);
	inc(i,1,n)h[i].read();
	
	sort(h+1,h+n+1);
	
	/*	int tot=0;
	inc(i,1,n)
	{
		if(h[i]!=h[i-1])h[++tot]=h[i];
		++h[tot].w;
	}*/

		int tot=1;
	h[1].w=1;
	inc(i,2,n)
	{
		if(h[i]!=h[i-1])h[++tot]=h[i];
		++h[tot].w;
	}
	CDQ(1,tot);
	inc(i,1,n)ans[h[i].f+h[i].w-1]+=h[i].w;
	inc(i,0,n-1)
	printf("%d\n",ans[i]);
	re 0;
}

  

陌上花开(三维偏序)

标签:main   getc   ons   name   csharp   c99   lowbit   归并   template   

原文地址:https://www.cnblogs.com/lsyyy/p/11291365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!