# bzoj3262 陌上花开

【题解】

cdq分治。

cdq分治的时候呢，我们定义过程solve(l,r)表示解决p[l...r]的问题。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, K, ans[M], N;
struct pa {
int a, b, c, s, ans;
pa() {}
pa(int a, int b, int c) : a(a), b(b), c(c) {}
friend bool operator <(pa a, pa b) {
return a.a < b.a || (a.a == b.a && a.b < b.b) || (a.a == b.a && a.b == b.b && a.c < b.c);
}
friend bool operator ==(pa a, pa b) {
return a.a == b.a && a.b == b.b && a.c == b.c;
}
}t[M], p[M];
int pn = 0;

inline bool cmp(pa a, pa b) {
return a.b < b.b || (a.b == b.b && a.c < b.c) || (a.b == b.b && a.a == b.a && a.c < b.c);
}

struct BIT {
int c[M<<1], n;
# define lb(x) (x&(-x))
inline void set(int _n) {
n = _n;
memset(c, 0, sizeof c);
}
inline void edt(int x, int d) {
for (; x<=n; x+=lb(x)) c[x] += d;
}
inline int sum(int x) {
int ret = 0;
for (; x; x-=lb(x)) ret += c[x];
return ret;
}
}T;

inline void solve(int l, int r) {
if(l == r) {
p[l].ans += p[l].s-1;
return ;
}
int mid = l+r>>1, t1n = l-1, t2n = mid;
for (int i=l; i<=r; ++i) {
if(p[i].a <= mid) t[++t1n] = p[i];
else t[++t2n] = p[i];
}
for (int i=l; i<=r; ++i) p[i] = t[i];
int j = l;
for (int i=mid+1; i<=r; ++i) {
for(; j <= mid && p[j].b <= p[i].b; ++j) T.edt(p[j].c, p[j].s);
p[i].ans += T.sum(p[i].c);
}
for (int i=l; i<j; ++i) T.edt(p[i].c, -p[i].s);
solve(l, mid); solve(mid+1, r);
}

int main() {
cin >> n >> K; T.set(K); N = n;
for (int i=1; i<=n; ++i) {
scanf("%d%d%d", &t[i].a, &t[i].b, &t[i].c);
t[i].s = 1;
}
sort(t+1, t+n+1);
p[++pn] = t[1];
for (int i=2; i<=n; ++i)
if(!(t[i] == t[i-1])) p[++pn] = t[i];
else p[pn].s ++;
n = pn;
for (int i=1; i<=n; ++i) p[i].a = i;
sort(p+1, p+n+1, cmp);
solve(1, n);
for (int i=1; i<=n; ++i) ans[p[i].ans] += p[i].s;
for (int i=0; i<=N-1; ++i) printf("%d\n", ans[i]);
return 0;
}
View Code

posted @ 2017-06-05 12:56  Galaxies  阅读(192)  评论(0编辑  收藏  举报