# bzoj3262陌上花开 cdq分治入门题

Description

Input

Output

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 <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++)
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编辑  收藏  举报