「BZOJ-3262」陌上花开-树套树

有n朵花,每朵花有三个属性:花形(s),颜色(c),气味(m),又三个整数表示。现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。定义一朵花A比另一朵花B要美丽,当且仅当Sa>=Sb,Ca>=Cb,Ma>=Mb。显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。

链接

BZOJ-3262

输入

第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值。

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

输出

包含N行,分别表示评级为0…N-1的每级花的数量。

样例

输入

1
2
3
4
5
6
7
8
9
10
11
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

输出

1
2
3
4
5
6
7
8
9
10
3
1
3
0
1
0
1
0
0
1

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

题解

此题可写 $cdq$ 分治,但树套树看完题就知道怎么做了,一维排序,二维树状数组,三维平衡树求一下 $rank$ 就完了…

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
const int IN_LEN = 10000000, OUT_LEN = 10000000;
inline int nextChar() {
static char buf[IN_LEN], *h, *t;
if (h == t) {
t = (h = buf) + fread(buf, 1, IN_LEN, stdin);
if (h == t) return -1;
}
return *h++;
}
template<class T>
inline bool read(T &x) {
static bool iosig = 0;
static char c;
for (iosig = 0, c = nextChar(); !isdigit(c); c = nextChar()) {
if (c == -1) return false;
if (c == '-') iosig = 1;
}
for (x = 0; isdigit(c); c = nextChar()) x = (x << 1) + (x << 3) + (c ^ '0');
if (iosig) x = -x;
return true;
}
char obuf[OUT_LEN], *oh = obuf;
inline void writeChar(const char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void write(T x) {
static int buf[30], cnt;
if (!x) writeChar(48);
else {
if (x < 0) writeChar('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) writeChar(buf[cnt--]);
}
}
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
const int MAXN = 100005;
struct Flower {
int a, b, c;
inline friend bool operator < (const Flower &a, const Flower &b) {
return (a.a == b.a) ? ((a.b == b.b) ? (a.c < b.c) : (a.b < b.b)) : (a.a < b.a);
}
} f[MAXN];
struct Node {
Node *lc, *rc;
int s, v, w, rank;
} node[5000005], *cur = node;
Node *root[MAXN << 1];
inline void update(Node *k) { k->s = (k->lc ? k->lc->s : 0) + (k->rc ? k->rc->s : 0) + k->w; }
inline void lRotate(Node *&k) {
Node *t = k->rc;
k->rc = t->lc, t->lc = k, update(k), update(t), k = t;
}
inline void rRotate(Node *&k) {
Node *t = k->lc;
k->lc = t->rc, t->rc = k, update(k), update(t), k = t;
}
inline void insert(Node *&k, int x) {
if (!k) {
k = ++cur, k->rank = rand(), k->v = x, k->s = k->w = 1;
return;
}
k->s++;
if (x == k->v) return (void)k->w++;
else if (x < k->v) {
insert(k->lc, x);
if (k->lc->rank < k->rank) rRotate(k);
} else {
insert(k->rc, x);
if (k->rc->rank < k->rank) lRotate(k);
}
}
inline int rank(Node *k, int x) {
if (!k) return 0;
if (x == k->v) return (k->lc ? k->lc->s : 0) + k->w;
else if(x < k->v) return rank(k->lc, x);
else return (k->lc ? k->lc->s : 0) + k->w + rank(k->rc, x);
}
int n, m;
inline int lowbit(int k) { return k & -k; }
inline int query(int k, int x) {
register int ans = 0;
for (; k; k -= lowbit(k)) ans += rank(root[k], x);
return ans;
}
inline void update(int k, int x) { for (; k <= m; k += lowbit(k)) insert(root[k], x); }
int ans[MAXN], sum[MAXN];
int main() {
srand(495);
read(n), read(m);
for (register int i = 1; i <= n; i++) read(f[i].a), read(f[i].b), read(f[i].c);
std::sort(f + 1, f + n + 1);
for (register int i = 1; i <= n; i++) {
if (f[i].a == f[i + 1].a && f[i].b == f[i + 1].b && f[i].c == f[i + 1].c && i != n) sum[i + 1] += sum[i] + 1;
else ans[query(f[i].b, f[i].c)] += sum[i] + 1;
update(f[i].b, f[i].c);
}
for (register int i = 0; i < n; i++) write(ans[i]), writeChar('\n');
flush();
return 0;
}
分享到