bzoj 3262: 陌上花开 (cdq分治)

3262: 陌上花开

Time Limit: 20 Sec   Memory Limit: 256 MB
Submit: 1700   Solved: 742
[ Submit][ Status][ Discuss]

Description

有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

HINT

1 <= N <= 100,000, 1 <= K <= 200,000

Source

树套树 CDQ分治



复习cdq
排序搞一维,cdq降一维,树状数组处理一维

代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<climits>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define N 400020
#define lowbit(x) x&-x
using namespace std;
inline int read()
{
    int x=0,f=1;char ch;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,tot;
struct node{int a,b,c,d,num;}p[N],tmp[N];
bool operator < (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;
}
bool cmp(node a,node b)
{
    if(a.b!=b.b)return a.b<b.b;
    if(a.d!=b.d)return a.d<b.d;
    return a.c<b.c;
}
int C[N],ans[N];
void add(int x,int val)
{
    while(x<=m)
    {
	C[x]+=val;
	x+=lowbit(x);
    }
}
int ask(int x)
{
    int ret=0;
    while(x)
    {
	ret+=C[x];
	x-=lowbit(x);
    }return ret;
}
void cdq(int l,int r)
{
    if(l==r)return ;int mid=(l+r)>>1;
    for(int i=l;i<=r;i++)
		if(p[i].d<=mid)add(p[i].c,p[i].num);
		else ans[p[i].d]+=ask(p[i].c);
    int L=l,R=mid+1;
    for(int i=l;i<=r;i++)
	if(p[i].d<=mid)
	{
	    add(p[i].c,-p[i].num);
	    tmp[L++]=p[i];
	}
	else tmp[R++]=p[i];
	for(int i=l;i<=r;i++)
		p[i]=tmp[i];
    cdq(l,mid);cdq(mid+1,r);
}
int ret[N];
int main()
{
//	freopen("in.txt","r",stdin);
//	freopen("my.txt","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++)
	p[i].a=read(),p[i].b=read(),p[i].c=read();
    sort(p+1,p+1+n);p[++tot]=p[1];p[tot].d=1,p[tot].num=1;
    for(int i=2;i<=n;i++)
    {
		if(p[i].a==p[i-1].a&&p[i].b==p[i-1].b&&p[i].c==p[i-1].c)
			p[tot].num++;
		else p[++tot]=p[i],p[tot].num=1,p[tot].d=tot;
    }sort(p+1,p+1+tot,cmp);for(int i=1;i<=tot;i++)ans[p[i].d]=p[i].num-1;
    cdq(1,tot);
//    for(int i=1;i<=tot;i++)
 //   	printf("%d %d %d : %d\n",p[i].a,p[i].b,p[i].c,ans[p[i].d]);
	for(int i=1;i<=tot;i++)
		ret[ans[i]]+=p[i].num;
	for(int i=0;i<n;i++)
		printf("%d\n",ret[i]);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值