# loj＃6032. 「雅礼集训 2017 Day2」水箱(并查集 贪心 扫描线)

2021-07-19 18:35:03

## Sol

``````#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e6 + 10, INF = 1e9 + 7, mod = 998244353;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, cnt, ans[MAXN], f[MAXN], fa[MAXN];
int find(int x) {
return fa[x] ? fa[x] = find(fa[x]) : x;
}
struct Query {
int opt, x, y;
bool operator < (const Query &rhs) const {
return y == rhs.y ? opt < rhs.opt : y < rhs.y;
}
}q[MAXN];
void solve() {
memset(ans, 0, sizeof(ans)); memset(f, 0, sizeof(f)); memset(fa, 0, sizeof(fa));
cnt = 0;
for(int i = 1; i < N; i++) q[++cnt] = {-1, i, read()};
for(int i = 1; i <= M; i++) q[++cnt].x = read(), q[cnt].y = read(), q[cnt].opt = read();
stable_sort(q + 1, q + cnt + 1);
int ret = 0;
for(int i = 1; i <= cnt; i++) {
int op = q[i].opt, x = q[i].x;
if(op == -1) {
int y = find(x + 1); x = find(x);
fa[y] = x; f[x] += f[y]; ans[x] += ans[y]; chmax(ret, ans[x]);
} else if(op == 0) {
chmax(ret, ++ans[find(x)]);
} else {
x = find(x); chmax(ans[x], ++f[x]);
chmax(ret, ans[x]);
}
}
cout << ret << '\n';
}
int main() {
for(int T = read(); T--; solve());
return 0;
}
```

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.
```

https://blog.51cto.com/u_15239936/2867506