Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CDQ 分治模板

解决偏序问题。

查看代码
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
104
105
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 1e5 + 10, M = 2e5 + 10;
int n, m, ans[N];
struct Node
{
int a, b, c, sz, res;
friend bool operator<(const Node &x, const Node &y)
{
if (x.a != y.a)
return x.a < y.a;
if (x.b != y.b)
return x.b < y.b;
return x.c < y.c;
}
friend bool operator==(const Node &x, const Node &y)
{
return x.a == y.a && x.b == y.b && x.c == y.c;
}
} w[N], tmp[N];
int tr[M];
void add(int x, int k)
{
for (; x <= m; x += x & -x)
tr[x] += k;
}
int query(int x)
{
int res = 0;
for (; x; x -= x & -x)
res += tr[x];
return res;
}
void cdq(int l, int r)
{
if (l == r)
return;
int mid = l + r >> 1;
cdq(l, mid), cdq(mid + 1, r);
int i = l, j = mid + 1, t = 0;
while (i <= mid && j <= r)
if (w[i].b <= w[j].b)
{
add(w[i].c, w[i].sz);
tmp[t++] = w[i++];
}
else
{
w[j].res += query(w[j].c);
tmp[t++] = w[j++];
}
while (i <= mid)
{
add(w[i].c, w[i].sz);
tmp[t++] = w[i++];
}
while (j <= r)
{
w[j].res += query(w[j].c);
tmp[t++] = w[j++];
}
for (int k = l; k <= mid; k++)
add(w[k].c, -w[k].sz);
for (int i = l; i <= r; i++)
w[i] = tmp[i - l];
}
int main()
{
read(n), read(m);
for (int i = 0; i < n; i++)
{
read(w[i].a), read(w[i].b), read(w[i].c);
w[i].sz = 1, w[i].res = 0;
}
sort(w, w + n);
int cur = 1;
for (int i = 1; i < n; i++)
w[cur - 1] == w[i] ? (w[cur - 1].sz++) : (w[cur++] = w[i], 0);
cdq(0, cur - 1);
for (int i = 0; i < cur; i++)
ans[w[i].res + w[i].sz - 1] += w[i].sz;
for (int i = 0; i < n; i++)
write(ans[i]), puts("");
return 0;
}