陌上花開 HYSBZ - 3262 三維偏序問題 CDQ分治+樹狀數組

有n朵花,每朵花有三個屬性:花形(s)、顏色(c)、氣味(m),用三個整數表示。

現在要對每朵花評級,一朵花的級別是它擁有的美麗能超過的花的數量。

定義一朵花A比另一朵花B要美麗,當且僅Sa>=Sb,Ca>=Cb,Ma>=Mb。

顯然,兩朵花可能有同樣的屬性。需要統計出評出每個等級的花的數量。

Input

第一行爲N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分別表示花的數量和最大屬性值。

以下N行,每行三個整數si, ci, mi (1 <= si, ci, mi <= K),表示第i朵花的屬性

Output

包含N行,分別表示評級爲0...N-1的每級花的數量。

Sample Input

10 3 3 3 3 2 3 3 2 3 1 3 1 1 3 1 2 1 3 1 1 1 2 1 2 2 1 3 2 1 2 1

Sample Output

3 1 3 0 1 0 1 0 0 1

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
const int maxk=2e5+100;
struct node{
	int a;
	int b;
	int c;
	int id;
}a[maxn],tmp[maxn];
int n,k,cnt,tree[maxk],ans[maxn],res[maxk];
int lowbit(int x)
{
	return x&(-x);
}
void update(int tar,int val)
{
	for(int i=tar;i<=k;i+=lowbit(i)) tree[i]+=val;
}
int query(int tar)
{
	int res=0;
	for(int i=tar;i>0;i-=lowbit(i)) res+=tree[i];
	return res;
}
bool cmp(node x,node y)
{
	if(x.a<y.a) return 1;
	if(x.a>y.a) return 0;
	if(x.b<y.b) return 1;
	if(x.b>y.b) return 0;
	if(x.c<y.c) return 1;
	return 0;
}
void CDQ(int l,int r)
{
	if(l==r) return ;
	int mid=(l+r)>>1,p=l,q=mid+1,cont=l-1;
	CDQ(l,mid);CDQ(mid+1,r);
	while(p<=mid&&q<=r)
	{
		if(a[p].b<=a[q].b)
		{
			update(a[p].c,1);
			tmp[++cont]=a[p++];
		}
		else
		{
			ans[a[q].id]+=query(a[q].c);
			tmp[++cont]=a[q++];
		}
	}
	while(q<=r) ans[a[q].id]+=query(a[q].c),tmp[++cont]=a[q++];
	for(int i=l;i<p;i++) update(a[i].c,-1);
	while(p<=mid) tmp[++cont]=a[p++];
	for(int i=l;i<=r;i++) a[i]=tmp[i];
}
int main()
{
	cnt=1;
	memset(ans,0,sizeof(ans));
	memset(res,0,sizeof(res));
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c);
		a[i].id=i;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=n;i>=1;i--)
	{
		if(a[i].a==a[i+1].a&&a[i].b==a[i+1].b&&a[i].c==a[i+1].c) ans[a[i].id]+=cnt++;
		else cnt=1; 
	}
	CDQ(1,n);
	for(int i=1;i<=n;i++) res[ans[i]]++;
	for(int i=0;i<n;i++) printf("%d\n",res[i]);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章