#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef int64_t ll;
#define SZ(x) (int) x.size()
const int N = 120'005, LOG = 18, B = 450, inf = 1e9;
vector<array<int, 3>> adj[N];
vector<int> dp[N];
int lst;
int solve(int i, int par) {
if (~dp[i][par]) return dp[i][par];
int j = (par == SZ(adj[i])? 0 : par + 1);
dp[i][par] = 1;
if (j < SZ(adj[i])) {
if (adj[i][j][1] > lst) return dp[i][par];
dp[i][par] += solve(adj[i][j][0], adj[i][j][2]) + solve(i, j) - 1;
}
return dp[i][par];
}
int st[N], en[N], curt, eulerst[N], euleren[N], eulert;
array<int, 2> par[N];
bool isanc(int u, int v) {
return st[u] <= st[v] && en[v] <= en[u];
}
struct sparse {
array<int, 2> a[2 * N][LOG + 1];
int n;
int logs[2 * N];
void build(int _n) {
n = _n;
logs[1] = 0;
for (int j = 2; j <= n; j++) {
logs[j] = logs[j >> 1] + 1;
}
for (int j = 1; j <= LOG; j++) {
for (int i = 0; i + (1 << (j - 1)) < n; i++) {
a[i][j] = min(a[i][j - 1], a[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int u, int v) {
if (st[u] > st[v]) swap(u, v);
if (isanc(u, v)) {
return u;
}
int l = en[u], r = st[v];
int lg = logs[r - l + 1];
array<int, 2> mn = min(a[l][lg], a[r - (1 << lg) + 1][lg]);
return mn[1];
}
} lca;
void dfs(int i, int h, int p, int part) {
lca.a[curt][0] = {h, i};
st[i] = curt++;
eulerst[i] = eulert++;
par[i] = {p, part};
for (int j = 0; j < SZ(adj[i]); j++) {
auto [nxt, t, nxtidx] = adj[i][j];
if (nxt == p) continue;
dfs(nxt, h + 1, i, t);
lca.a[curt][0] = {h, i};
curt++;
}
en[i] = curt - 1;
euleren[i] = eulert - 1;
}
vector<array<int, 2>> ask[N];
int ans[2 * N];
struct sqrt_decomp {
const int b = 350;
int a[N], lazy[350];
int n;
void init(int _n, int _v) {
n = _n;
for (int i = 0; i < n; i++) {
a[i] = _v;
}
for (int i = 0; i <= n / b; i++) {
lazy[i] = -1;
}
}
void set(int l, int r, int v) {
int s = l / b, e = r / b;
if (s == e) {
for (int i = l; i <= r; i++) {
a[i] = v;
}
return;
}
for (int i = s + 1; i <= e - 1; i++) {
lazy[i] = v;
}
int x = s * b, y = (s + 1) * b - 1;
if (lazy[s] != -1) {
for (int i = x; i <= y; i++) {
a[i] = lazy[s];
}
lazy[s] = -1;
}
for (int i = l; i <= y; i++) {
a[i] = v;
}
x = e * b, y = (e + 1) * b - 1;
if (lazy[e] != -1) {
for (int i = x; i <= y; i++) {
a[i] = lazy[e];
}
lazy[e] = -1;
}
for (int i = x; i <= r; i++) {
a[i] = v;
}
}
int get(int i) {
int x = i / b;
if (lazy[x] != -1) {
return lazy[x];
}
return a[i];
}
} Inc, Dec;
set<int> activeinc, activedec;
bool ininc[N], indec[N];
pair<char, vector<int>> v[2 * N];
void process(int i, int p) {
for (auto [nxt, t, nxtidx] : adj[i]) {
if (nxt == p) continue;
process(nxt, i);
}
ininc[eulerst[i]] = 1;
indec[eulerst[i]] = 1;
for (auto [idx, t] : ask[i]) {
int a, b;
if (v[idx].first == 'Q') {
a = v[idx].second[1], b = v[idx].second[0];
} else {
a = v[t].second[0], b = v[t].second[1];
int x = v[idx].second[0];
if (isanc(b, x)) {
a = x;
} else {
b = a;
a = x;
}
}
if (!indec[eulerst[a]] || !ininc[eulerst[b]]) continue;
int lst1 = Dec.get(eulerst[a]), lst2 = Inc.get(eulerst[b]);
bool ok = lst1 < lst2;
int mx;
if (v[idx].first == 'Q') {
mx = idx;
} else {
mx = t - 1;
}
if (b != i) {
ok &= par[b][1] <= mx;
} else if (a != i) {
ok &= lst1 <= mx;
}
ans[idx] += ok;
}
ask[i].clear();
if (i == 1) return;
int part = par[i][1];
for (auto [nxt, t, nxtidx] : adj[i]) {
if (nxt == p) continue;
if (part < t) {
Inc.set(eulerst[nxt], euleren[nxt], part);
auto it = activedec.lower_bound(eulerst[nxt]);
while (it != activedec.end() && *it <= euleren[nxt]) {
indec[*it] = 0;
it = activedec.erase(it);
}
Dec.set(eulerst[nxt], euleren[nxt], inf);
} else {
auto it = activeinc.lower_bound(eulerst[nxt]);
while (it != activeinc.end() && *it <= euleren[nxt]) {
ininc[*it] = 0;
it = activeinc.erase(it);
}
Inc.set(eulerst[nxt], euleren[nxt], -inf);
Dec.set(eulerst[nxt], euleren[nxt], part);
}
}
activeinc.insert(eulerst[i]);
Inc.set(eulerst[i], eulerst[i], part);
activedec.insert(eulerst[i]);
Dec.set(eulerst[i], eulerst[i], part);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
q += n - 1;
for (int i = 0; i < q; i++) {
cin >> v[i].first;
if (v[i].first == 'S') {
int a, b;
cin >> a >> b;
int x = adj[b].size(), y = adj[a].size();
adj[a].push_back({b, i, x});
adj[b].push_back({a, i, y});
v[i].second = {a, b};
} else if (v[i].first == 'Q') {
int a, b;
cin >> a >> b;
v[i].second = {a, b};
} else {
int a;
cin >> a;
v[i].second = {a};
}
}
dfs(1, 0, 1, 0);
lca.build(curt);
for (int i = 1; i <= n; i++) {
dp[i].resize(adj[i].size() + 1);
for (int j = 0; j <= SZ(adj[i]); j++) {
dp[i][j] = -1;
}
}
vector<array<int, 3>> upd;
lst = -1;
for (int i = 0; i < q; i++) {
if (upd.size() == B) {
lst = i - 1;
upd.clear();
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= SZ(adj[j]); k++) {
dp[j][k] = -1;
}
}
}
if (v[i].first == 'S') {
int a = v[i].second[0], b = v[i].second[1];
if (isanc(b, a)) swap(a, b), swap(v[i].second[0], v[i].second[1]);
upd.push_back({a, b, i});
} else if (v[i].first == 'Q') {
int a = v[i].second[0], b = v[i].second[1];
int mid = lca.query(a, b);
ask[mid].push_back({i, -1});
} else {
int x = v[i].second[0];
ans[i] += solve(x, adj[x].size());
for (auto [a, b, t] : upd) {
if (isanc(b, x)) {
ask[b].push_back({i, t});
} else {
int mid = lca.query(a, x);
ask[mid].push_back({i, t});
}
}
}
}
Dec.init(eulert, -inf);
Inc.init(eulert, inf);
process(1, 1);
for (int i = 0; i < q; i++) {
if (v[i].first == 'Q') {
cout << (ans[i]? "yes\n" : "no\n");
} else if (v[i].first == 'C') {
cout << ans[i] << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
26448 KB |
Output is correct |
2 |
Correct |
38 ms |
28884 KB |
Output is correct |
3 |
Correct |
41 ms |
29132 KB |
Output is correct |
4 |
Correct |
39 ms |
29140 KB |
Output is correct |
5 |
Correct |
39 ms |
29808 KB |
Output is correct |
6 |
Correct |
36 ms |
28992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
26448 KB |
Output is correct |
2 |
Correct |
38 ms |
28884 KB |
Output is correct |
3 |
Correct |
41 ms |
29132 KB |
Output is correct |
4 |
Correct |
39 ms |
29140 KB |
Output is correct |
5 |
Correct |
39 ms |
29808 KB |
Output is correct |
6 |
Correct |
36 ms |
28992 KB |
Output is correct |
7 |
Correct |
40 ms |
29248 KB |
Output is correct |
8 |
Correct |
426 ms |
184144 KB |
Output is correct |
9 |
Correct |
294 ms |
169408 KB |
Output is correct |
10 |
Correct |
435 ms |
151224 KB |
Output is correct |
11 |
Correct |
520 ms |
252284 KB |
Output is correct |
12 |
Correct |
388 ms |
292076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
26064 KB |
Output is correct |
2 |
Correct |
326 ms |
87384 KB |
Output is correct |
3 |
Correct |
353 ms |
87520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
26064 KB |
Output is correct |
2 |
Correct |
326 ms |
87384 KB |
Output is correct |
3 |
Correct |
353 ms |
87520 KB |
Output is correct |
4 |
Correct |
35 ms |
30668 KB |
Output is correct |
5 |
Correct |
1129 ms |
147380 KB |
Output is correct |
6 |
Correct |
1435 ms |
341972 KB |
Output is correct |
7 |
Correct |
1599 ms |
342848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
26456 KB |
Output is correct |
2 |
Correct |
395 ms |
90660 KB |
Output is correct |
3 |
Correct |
416 ms |
90636 KB |
Output is correct |
4 |
Correct |
351 ms |
95832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
26456 KB |
Output is correct |
2 |
Correct |
395 ms |
90660 KB |
Output is correct |
3 |
Correct |
416 ms |
90636 KB |
Output is correct |
4 |
Correct |
351 ms |
95832 KB |
Output is correct |
5 |
Correct |
33 ms |
29144 KB |
Output is correct |
6 |
Correct |
1208 ms |
241928 KB |
Output is correct |
7 |
Correct |
1196 ms |
245436 KB |
Output is correct |
8 |
Correct |
1633 ms |
379648 KB |
Output is correct |
9 |
Correct |
1937 ms |
379808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
26112 KB |
Output is correct |
2 |
Correct |
368 ms |
82248 KB |
Output is correct |
3 |
Correct |
477 ms |
77284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
26112 KB |
Output is correct |
2 |
Correct |
368 ms |
82248 KB |
Output is correct |
3 |
Correct |
477 ms |
77284 KB |
Output is correct |
4 |
Correct |
39 ms |
28896 KB |
Output is correct |
5 |
Correct |
857 ms |
225244 KB |
Output is correct |
6 |
Correct |
1171 ms |
201248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
26452 KB |
Output is correct |
2 |
Correct |
461 ms |
90704 KB |
Output is correct |
3 |
Correct |
492 ms |
91040 KB |
Output is correct |
4 |
Correct |
365 ms |
95884 KB |
Output is correct |
5 |
Correct |
31 ms |
26308 KB |
Output is correct |
6 |
Correct |
337 ms |
82264 KB |
Output is correct |
7 |
Correct |
519 ms |
77224 KB |
Output is correct |
8 |
Correct |
363 ms |
77244 KB |
Output is correct |
9 |
Correct |
401 ms |
77664 KB |
Output is correct |
10 |
Correct |
600 ms |
88584 KB |
Output is correct |
11 |
Correct |
583 ms |
87296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
26452 KB |
Output is correct |
2 |
Correct |
461 ms |
90704 KB |
Output is correct |
3 |
Correct |
492 ms |
91040 KB |
Output is correct |
4 |
Correct |
365 ms |
95884 KB |
Output is correct |
5 |
Correct |
31 ms |
26308 KB |
Output is correct |
6 |
Correct |
337 ms |
82264 KB |
Output is correct |
7 |
Correct |
519 ms |
77224 KB |
Output is correct |
8 |
Correct |
363 ms |
77244 KB |
Output is correct |
9 |
Correct |
401 ms |
77664 KB |
Output is correct |
10 |
Correct |
600 ms |
88584 KB |
Output is correct |
11 |
Correct |
583 ms |
87296 KB |
Output is correct |
12 |
Correct |
34 ms |
29140 KB |
Output is correct |
13 |
Correct |
1306 ms |
241932 KB |
Output is correct |
14 |
Correct |
1354 ms |
245664 KB |
Output is correct |
15 |
Correct |
2048 ms |
379396 KB |
Output is correct |
16 |
Correct |
1939 ms |
379556 KB |
Output is correct |
17 |
Correct |
36 ms |
28900 KB |
Output is correct |
18 |
Correct |
995 ms |
224724 KB |
Output is correct |
19 |
Correct |
1195 ms |
202464 KB |
Output is correct |
20 |
Correct |
1458 ms |
276880 KB |
Output is correct |
21 |
Correct |
1253 ms |
216000 KB |
Output is correct |
22 |
Correct |
2374 ms |
321040 KB |
Output is correct |
23 |
Execution timed out |
3510 ms |
226416 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
26376 KB |
Output is correct |
2 |
Correct |
59 ms |
28880 KB |
Output is correct |
3 |
Correct |
64 ms |
29132 KB |
Output is correct |
4 |
Correct |
51 ms |
29132 KB |
Output is correct |
5 |
Correct |
68 ms |
29700 KB |
Output is correct |
6 |
Correct |
46 ms |
29092 KB |
Output is correct |
7 |
Correct |
27 ms |
26052 KB |
Output is correct |
8 |
Correct |
359 ms |
87300 KB |
Output is correct |
9 |
Correct |
432 ms |
87532 KB |
Output is correct |
10 |
Correct |
30 ms |
26428 KB |
Output is correct |
11 |
Correct |
444 ms |
90616 KB |
Output is correct |
12 |
Correct |
596 ms |
90824 KB |
Output is correct |
13 |
Correct |
463 ms |
95832 KB |
Output is correct |
14 |
Correct |
34 ms |
26264 KB |
Output is correct |
15 |
Correct |
428 ms |
82372 KB |
Output is correct |
16 |
Correct |
453 ms |
77396 KB |
Output is correct |
17 |
Correct |
511 ms |
77196 KB |
Output is correct |
18 |
Correct |
461 ms |
77596 KB |
Output is correct |
19 |
Correct |
673 ms |
88800 KB |
Output is correct |
20 |
Correct |
586 ms |
87368 KB |
Output is correct |
21 |
Correct |
568 ms |
84464 KB |
Output is correct |
22 |
Correct |
393 ms |
84852 KB |
Output is correct |
23 |
Correct |
443 ms |
78524 KB |
Output is correct |
24 |
Correct |
388 ms |
78824 KB |
Output is correct |
25 |
Correct |
573 ms |
85740 KB |
Output is correct |
26 |
Correct |
548 ms |
81444 KB |
Output is correct |
27 |
Correct |
526 ms |
81840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
26376 KB |
Output is correct |
2 |
Correct |
59 ms |
28880 KB |
Output is correct |
3 |
Correct |
64 ms |
29132 KB |
Output is correct |
4 |
Correct |
51 ms |
29132 KB |
Output is correct |
5 |
Correct |
68 ms |
29700 KB |
Output is correct |
6 |
Correct |
46 ms |
29092 KB |
Output is correct |
7 |
Correct |
27 ms |
26052 KB |
Output is correct |
8 |
Correct |
359 ms |
87300 KB |
Output is correct |
9 |
Correct |
432 ms |
87532 KB |
Output is correct |
10 |
Correct |
30 ms |
26428 KB |
Output is correct |
11 |
Correct |
444 ms |
90616 KB |
Output is correct |
12 |
Correct |
596 ms |
90824 KB |
Output is correct |
13 |
Correct |
463 ms |
95832 KB |
Output is correct |
14 |
Correct |
34 ms |
26264 KB |
Output is correct |
15 |
Correct |
428 ms |
82372 KB |
Output is correct |
16 |
Correct |
453 ms |
77396 KB |
Output is correct |
17 |
Correct |
511 ms |
77196 KB |
Output is correct |
18 |
Correct |
461 ms |
77596 KB |
Output is correct |
19 |
Correct |
673 ms |
88800 KB |
Output is correct |
20 |
Correct |
586 ms |
87368 KB |
Output is correct |
21 |
Correct |
568 ms |
84464 KB |
Output is correct |
22 |
Correct |
393 ms |
84852 KB |
Output is correct |
23 |
Correct |
443 ms |
78524 KB |
Output is correct |
24 |
Correct |
388 ms |
78824 KB |
Output is correct |
25 |
Correct |
573 ms |
85740 KB |
Output is correct |
26 |
Correct |
548 ms |
81444 KB |
Output is correct |
27 |
Correct |
526 ms |
81840 KB |
Output is correct |
28 |
Correct |
37 ms |
29240 KB |
Output is correct |
29 |
Correct |
468 ms |
182340 KB |
Output is correct |
30 |
Correct |
292 ms |
174960 KB |
Output is correct |
31 |
Correct |
432 ms |
150988 KB |
Output is correct |
32 |
Correct |
632 ms |
252432 KB |
Output is correct |
33 |
Correct |
400 ms |
291496 KB |
Output is correct |
34 |
Correct |
36 ms |
30456 KB |
Output is correct |
35 |
Correct |
2090 ms |
145692 KB |
Output is correct |
36 |
Correct |
1937 ms |
343716 KB |
Output is correct |
37 |
Correct |
2442 ms |
343204 KB |
Output is correct |
38 |
Correct |
35 ms |
29136 KB |
Output is correct |
39 |
Correct |
1695 ms |
241928 KB |
Output is correct |
40 |
Correct |
1290 ms |
245548 KB |
Output is correct |
41 |
Correct |
2133 ms |
379388 KB |
Output is correct |
42 |
Correct |
2435 ms |
379552 KB |
Output is correct |
43 |
Correct |
36 ms |
28868 KB |
Output is correct |
44 |
Correct |
1039 ms |
225456 KB |
Output is correct |
45 |
Correct |
1271 ms |
203980 KB |
Output is correct |
46 |
Correct |
1412 ms |
268424 KB |
Output is correct |
47 |
Correct |
1403 ms |
215692 KB |
Output is correct |
48 |
Correct |
2247 ms |
320684 KB |
Output is correct |
49 |
Execution timed out |
3532 ms |
195408 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |