bzoj3262陌上花开 cdq分治入门题

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

水一发cdq分治

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define RG register
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define gmax(i, j) ((i) < (j) ? (i = j) : (i))
#define gmin(i, j) ((i) > (j) ? (i = j) : (i))
#define sqr(x) ((x) * (x))
#define dabs(x) ((x) < (0) ? (-x) : (x))
#define lowbit(x) ((x) & (-x))
#define For(i, a, b) for(RG int i = a, __u = b; i <= __u; i++)
#define set_file(File) freopen(#File".in","r",stdin)

const int MaxN = 100010;
const int MaxK = 200010;

struct flower{
	int x, y, z, cnt, ans;
}a[MaxN];

bool cmpx(const flower &u, const flower &v){
	if(u.x != v.x)return u.x < v.x;
	if(u.y != v.y)return u.y < v.y;
	return u.z < v.z;
}

bool cmpy(const flower &u, const flower &v){
	if(u.y != v.y)return u.y < v.y;
	return u.z < v.z;
}

int n, K, tot, ans[MaxN];

struct BIT{
	int c[MaxK];
	inline void add(RG int i, RG int d){
		for(; i <= n; i += lowbit(i))
			c[i] += d;
	}
	inline int sum(RG int i){
		RG int res=0;
		for(; i; i -= lowbit(i))
			res += c[i];
		return res;
	}
}b;

void CDQ(RG int l, RG int r){
	if(l == r){
		a[l].ans += a[l].cnt - 1;
		return;
	}
	RG int mid = l + r >> 1;
	CDQ(l, mid), CDQ(mid + 1, r);
	std::sort(a + l, a + mid + 1, cmpy);
	std::sort(a + mid + 1, a + r + 1, cmpy);
	RG int j = l;
	For(i, mid + 1, r){
		for(; a[j].y <= a[i].y && j <= mid; j++)
			b.add(a[j].z, a[j].cnt);
		a[i].ans += b.sum(a[i].z);
	}
	For(i, l, j - 1) b.add(a[i].z, -a[i].cnt);
	std::sort(a + l, a + r + 1, cmpy);
}

int main(){
	set_file(bzoj3262);
	scanf("%d%d", &n, &K);
	For(i, 1, n) scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z), a[i].ans = 1;
	std::sort(a + 1, a + 1 + n, cmpx);
	For(i, 1, n)
		if(i != 1 && a[i - 1].x == a[i].x && a[i - 1].y == a[i].y && a[i - 1].z == a[i].z)
			a[tot].cnt++;
		else
			a[++tot] = a[i], a[tot].cnt = 1;
	std::sort(a + 1, a + 1 + tot, cmpx);
	CDQ(1, tot);
	For(i, 1, tot) ans[a[i].ans] += a[i].cnt;
	For(i, 1, n) printf("%d\n",ans[i]);
	return 0;
}

  

posted @ 2017-02-16 22:01  keshuqi  阅读(297)  评论(1编辑  收藏  举报