三维偏序,一维一维解决:第一维排序,第二维 CDQ ,第三维树状数组(或 CDQ )。

先按照 $a$ 进行排序,考虑 CDQ ,先分治左右两边,使得左右两边的节点都按 $b$ 排序,于是依次从两边取(优先 $b$ , $b$ 相等优先 $c$),左边的取出时在树状数组中更新,右边的取出时从树状数组中查询,考虑贡献。

需要注意的是,$a$ 、 $b$ 、 $c$ 的值都相同的节点有可能互相产生贡献,很难用 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
// ==============================
// author: memset0
// website: https://memset0.cn
// ==============================
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int read() {
int x = 0; bool m = 0; char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') m = 1, c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (m) return -x; else return x;
}

const int maxn = 100010;

int n, m, p;
int ans[maxn], sum[maxn << 1], cnt[maxn];
struct node {
int a, b, c, i, s;
} e[maxn], t[maxn];

bool operator == (node a, node b) {
return a.a == b.a && a.b == b.b && a.c == b.c;
}

bool cmp1(node a, node b) {
if (a.a != b.a) return a.a < b.a;
if (a.b != b.b) return a.b < b.b;
return a.c < b.c;
}

#define lowbit(x) ((x)&(-(x)))

void add(int k, int x) {
for (int i = k; i <= m; i += lowbit(i))
sum[i] += x;
}
int ask(int k) {
int ans = 0;
for (int i = k; i >= 1; i -= lowbit(i))
ans += sum[i];
return ans;
}

void solve(int l, int r) {
int mid = (l + r) >> 1;
if (l == r) return;
solve(l, mid); // 先分治解决左右两边
solve(mid + 1, r); // 在计算左边对右边的贡献
int L = l, R = mid + 1, T = 0;
while (L <= mid && R <= r) {
if (e[L].b < e[R].b || (e[L].b == e[R].b && e[L].c <= e[R].c)) {
add(e[L].c, e[L].s);
t[++T] = e[L++];
} else {
ans[e[R].i] += ask(e[R].c);
t[++T] = e[R++];
}
}
while (L <= mid)
t[++T] = e[L++];
while (R <= r) {
ans[e[R].i] += ask(e[R].c);
t[++T] = e[R++];
}
L = l, R = mid + 1;
while (L <= mid && R <= r) {
if (e[L].b < e[R].b || (e[L].b == e[R].b && e[L].c <= e[R].c)) {
add(e[L].c, -e[L].s);
L++;
} else R++;
}
for (int i = 1; i <= T; i++)
e[l + i - 1] = t[i];
}

int main() {

n = read(), m = read();
for (int i = 1; i <= n; i++)
e[i].a = read(), e[i].b = read(), e[i].c = read(), e[i].i = i, e[i].s = 1;
sort(e + 1, e + n + 1, cmp1);
p = 1;
for (int i = 2; i <= n; i++)
if (e[i] == e[i - 1])
e[p].s++;
else
e[++p] = e[i];

swap(n, p);
solve(1, n); // 进行 CDQ
for (int i = 1; i <= n; i++)
ans[e[i].i] += e[i].s - 1;
for (int i = 1; i <= n; i++)
cnt[ans[e[i].i]] += e[i].s;
for (int i = 0; i < p; i++)
printf("%d\n", cnt[i]);

return 0;
}