#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 5e4 + 4;
const int INF = 1e9;
struct Node {
int fi, se;
bool operator < (const Node &other) const {
return se > other.se;
}
};
struct seg_tree {
vector<int> t;
seg_tree() {
t.resize(4 * maxn);
}
void update(int pos, int val, int v = 1, int l = 1, int r = maxn - 1) {
if (l == r) {
t[v] += val;
return;
}
int m = (l + r) / 2;
if (pos <= m) update(pos, val, 2 * v, l, m);
else update(pos, val, 2 * v + 1, m + 1, r);
t[v] = t[2 * v] + t[2 * v + 1];
}
int get(int r, int l = 1, int v = 1, int tl = 1, int tr = maxn - 1) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[v];
int m = (tl + tr) / 2;
int L = get(r, l, 2 * v, tl, m);
int R = get(r, l, 2 * v + 1, m + 1, tr);
return L + R;
}
} IT;
int n, k, res[maxn];
set<Node> adj[maxn];
vector<int> query_c[maxn], cur;
set<pair<int,int>> query_q[maxn];
map<int,int> L;
stack<pair<int,int>> rev;
void dfs_in(int u, int p, int pre) {
auto it = query_q[u].begin();
while (it != query_q[u].end()) {
auto idx = L.find(it->fi);
if (idx == L.end() || idx->se > it->se) {
it++;
continue;
}
res[it->se] = -1;
it = query_q[u].erase(it);
}
cur.push_back(u);
for (auto it : adj[u]) {
int v = it.fi, w = it.se;
if (w < pre && v != p)
dfs_in(v, u, w);
}
}
void dfs_out(int u, int p, int pre) {
L[u] = pre;
IT.update(pre, 1);
rev.push({pre, 1});
for (auto it : adj[u]) {
int v = it.fi, w = it.se;
if (w > pre && v != p)
dfs_out(v, u, w);
}
}
void Solve(int u) {
L.clear();
for (auto it : adj[u]) {
cur.clear();
int v = it.fi, w = it.se;
L[u] = w;
dfs_in(v, u, w);
for (auto i : cur) {
for (auto it : query_c[i]) if (it > w)
res[it] += IT.get(it) + 1;
}
dfs_out(v, u, w);
}
auto it = query_q[u].begin();
while (it != query_q[u].end()) {
auto idx = L.find(it->fi);
if (idx == L.end() || idx->se > it->se) {
it++;
continue;
}
res[it->se] = -1;
it = query_q[u].erase(it);
}
for (auto it : query_c[u]) {
res[it] += IT.get(it) + 1;
}
while (!rev.empty()) {
int u, i; tie(u, i) = rev.top(); rev.pop();
IT.update(u, -i);
}
}
struct Centroid_Decomposition {
int sz[maxn];
int dfs(int u, int p) {
sz[u] = 1;
for (auto it : adj[u])
if (it.fi != p)
sz[u] += dfs(it.fi, u);
return sz[u];
}
int Centroid(int u, int p, int tot) {
for (auto it : adj[u])
if (it.fi != p && sz[it.fi] > tot / 2)
return Centroid(it.fi, u, tot);
return u;
}
void build(int u) {
int tot = dfs(u, 0);
int c = Centroid(u, 0, tot);
Solve(c);
vector<Node> del(adj[c].begin(), adj[c].end());
for (auto it : del) {
adj[c].erase(it);
adj[it.fi].erase(adj[it.fi].find({c, it.se}));
build(it.fi);
}
}
} Cen;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= n + k - 1; i++)
res[i] = -3;
for (int i = 1; i <= n + k - 1; i++) {
char type; int u; cin >> type >> u;
if (type == 'S') {
int v; cin >> v;
adj[u].insert({v, i});
adj[v].insert({u, i});
}
else if (type == 'Q') {
int v; cin >> v;
if (u == v) res[i] = -1;
else {
query_q[v].insert({u, i});
res[i] = -2;
}
}
else {
query_c[u].pb(i);
res[i] = 0;
}
}
Cen.build(1);
for (int i = 1; i <= n + k - 1; i++)
if (res[i] != -3) {
if (res[i] >= 0) cout << res[i];
else cout << (res[i] == -1 ? "yes" : "no");
cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
40388 KB |
Output is correct |
2 |
Correct |
93 ms |
40624 KB |
Output is correct |
3 |
Correct |
121 ms |
40544 KB |
Output is correct |
4 |
Correct |
86 ms |
40664 KB |
Output is correct |
5 |
Correct |
99 ms |
40692 KB |
Output is correct |
6 |
Correct |
107 ms |
40780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
40388 KB |
Output is correct |
2 |
Correct |
93 ms |
40624 KB |
Output is correct |
3 |
Correct |
121 ms |
40544 KB |
Output is correct |
4 |
Correct |
86 ms |
40664 KB |
Output is correct |
5 |
Correct |
99 ms |
40692 KB |
Output is correct |
6 |
Correct |
107 ms |
40780 KB |
Output is correct |
7 |
Correct |
79 ms |
40028 KB |
Output is correct |
8 |
Correct |
108 ms |
38276 KB |
Output is correct |
9 |
Correct |
87 ms |
38188 KB |
Output is correct |
10 |
Correct |
109 ms |
38272 KB |
Output is correct |
11 |
Correct |
105 ms |
38400 KB |
Output is correct |
12 |
Correct |
85 ms |
38348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
40292 KB |
Output is correct |
2 |
Correct |
390 ms |
57772 KB |
Output is correct |
3 |
Correct |
411 ms |
57700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
40292 KB |
Output is correct |
2 |
Correct |
390 ms |
57772 KB |
Output is correct |
3 |
Correct |
411 ms |
57700 KB |
Output is correct |
4 |
Correct |
76 ms |
40004 KB |
Output is correct |
5 |
Correct |
387 ms |
56884 KB |
Output is correct |
6 |
Correct |
269 ms |
54576 KB |
Output is correct |
7 |
Correct |
247 ms |
54528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
40340 KB |
Output is correct |
2 |
Correct |
397 ms |
54776 KB |
Output is correct |
3 |
Correct |
445 ms |
54808 KB |
Output is correct |
4 |
Correct |
707 ms |
61636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
40340 KB |
Output is correct |
2 |
Correct |
397 ms |
54776 KB |
Output is correct |
3 |
Correct |
445 ms |
54808 KB |
Output is correct |
4 |
Correct |
707 ms |
61636 KB |
Output is correct |
5 |
Correct |
66 ms |
39968 KB |
Output is correct |
6 |
Correct |
573 ms |
54212 KB |
Output is correct |
7 |
Correct |
623 ms |
58616 KB |
Output is correct |
8 |
Correct |
462 ms |
53136 KB |
Output is correct |
9 |
Correct |
484 ms |
53120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
40364 KB |
Output is correct |
2 |
Correct |
599 ms |
55568 KB |
Output is correct |
3 |
Correct |
376 ms |
50856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
40364 KB |
Output is correct |
2 |
Correct |
599 ms |
55568 KB |
Output is correct |
3 |
Correct |
376 ms |
50856 KB |
Output is correct |
4 |
Correct |
73 ms |
39952 KB |
Output is correct |
5 |
Correct |
581 ms |
54292 KB |
Output is correct |
6 |
Correct |
443 ms |
50204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
40268 KB |
Output is correct |
2 |
Correct |
413 ms |
54784 KB |
Output is correct |
3 |
Correct |
410 ms |
54808 KB |
Output is correct |
4 |
Correct |
568 ms |
61732 KB |
Output is correct |
5 |
Correct |
61 ms |
40324 KB |
Output is correct |
6 |
Correct |
497 ms |
55472 KB |
Output is correct |
7 |
Correct |
360 ms |
50792 KB |
Output is correct |
8 |
Correct |
359 ms |
52332 KB |
Output is correct |
9 |
Correct |
399 ms |
51436 KB |
Output is correct |
10 |
Correct |
628 ms |
53560 KB |
Output is correct |
11 |
Correct |
606 ms |
55044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
40268 KB |
Output is correct |
2 |
Correct |
413 ms |
54784 KB |
Output is correct |
3 |
Correct |
410 ms |
54808 KB |
Output is correct |
4 |
Correct |
568 ms |
61732 KB |
Output is correct |
5 |
Correct |
61 ms |
40324 KB |
Output is correct |
6 |
Correct |
497 ms |
55472 KB |
Output is correct |
7 |
Correct |
360 ms |
50792 KB |
Output is correct |
8 |
Correct |
359 ms |
52332 KB |
Output is correct |
9 |
Correct |
399 ms |
51436 KB |
Output is correct |
10 |
Correct |
628 ms |
53560 KB |
Output is correct |
11 |
Correct |
606 ms |
55044 KB |
Output is correct |
12 |
Correct |
64 ms |
39976 KB |
Output is correct |
13 |
Correct |
431 ms |
54180 KB |
Output is correct |
14 |
Correct |
505 ms |
58620 KB |
Output is correct |
15 |
Correct |
427 ms |
53132 KB |
Output is correct |
16 |
Correct |
418 ms |
53152 KB |
Output is correct |
17 |
Correct |
64 ms |
40012 KB |
Output is correct |
18 |
Correct |
540 ms |
54296 KB |
Output is correct |
19 |
Correct |
367 ms |
50224 KB |
Output is correct |
20 |
Correct |
378 ms |
51040 KB |
Output is correct |
21 |
Correct |
400 ms |
50508 KB |
Output is correct |
22 |
Correct |
646 ms |
52528 KB |
Output is correct |
23 |
Correct |
879 ms |
53524 KB |
Output is correct |
24 |
Correct |
746 ms |
54592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
40296 KB |
Output is correct |
2 |
Correct |
96 ms |
40604 KB |
Output is correct |
3 |
Correct |
124 ms |
40516 KB |
Output is correct |
4 |
Correct |
98 ms |
40748 KB |
Output is correct |
5 |
Correct |
86 ms |
40744 KB |
Output is correct |
6 |
Correct |
101 ms |
40732 KB |
Output is correct |
7 |
Correct |
62 ms |
40352 KB |
Output is correct |
8 |
Correct |
360 ms |
57856 KB |
Output is correct |
9 |
Correct |
397 ms |
57728 KB |
Output is correct |
10 |
Correct |
63 ms |
40244 KB |
Output is correct |
11 |
Correct |
416 ms |
54820 KB |
Output is correct |
12 |
Correct |
394 ms |
54808 KB |
Output is correct |
13 |
Correct |
473 ms |
61640 KB |
Output is correct |
14 |
Correct |
61 ms |
40296 KB |
Output is correct |
15 |
Correct |
481 ms |
55440 KB |
Output is correct |
16 |
Correct |
336 ms |
50792 KB |
Output is correct |
17 |
Correct |
372 ms |
52412 KB |
Output is correct |
18 |
Correct |
364 ms |
51392 KB |
Output is correct |
19 |
Correct |
628 ms |
53560 KB |
Output is correct |
20 |
Correct |
599 ms |
54952 KB |
Output is correct |
21 |
Correct |
470 ms |
57228 KB |
Output is correct |
22 |
Correct |
454 ms |
55808 KB |
Output is correct |
23 |
Correct |
518 ms |
52624 KB |
Output is correct |
24 |
Correct |
442 ms |
52576 KB |
Output is correct |
25 |
Correct |
548 ms |
55056 KB |
Output is correct |
26 |
Correct |
340 ms |
50800 KB |
Output is correct |
27 |
Correct |
349 ms |
50668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
40296 KB |
Output is correct |
2 |
Correct |
96 ms |
40604 KB |
Output is correct |
3 |
Correct |
124 ms |
40516 KB |
Output is correct |
4 |
Correct |
98 ms |
40748 KB |
Output is correct |
5 |
Correct |
86 ms |
40744 KB |
Output is correct |
6 |
Correct |
101 ms |
40732 KB |
Output is correct |
7 |
Correct |
62 ms |
40352 KB |
Output is correct |
8 |
Correct |
360 ms |
57856 KB |
Output is correct |
9 |
Correct |
397 ms |
57728 KB |
Output is correct |
10 |
Correct |
63 ms |
40244 KB |
Output is correct |
11 |
Correct |
416 ms |
54820 KB |
Output is correct |
12 |
Correct |
394 ms |
54808 KB |
Output is correct |
13 |
Correct |
473 ms |
61640 KB |
Output is correct |
14 |
Correct |
61 ms |
40296 KB |
Output is correct |
15 |
Correct |
481 ms |
55440 KB |
Output is correct |
16 |
Correct |
336 ms |
50792 KB |
Output is correct |
17 |
Correct |
372 ms |
52412 KB |
Output is correct |
18 |
Correct |
364 ms |
51392 KB |
Output is correct |
19 |
Correct |
628 ms |
53560 KB |
Output is correct |
20 |
Correct |
599 ms |
54952 KB |
Output is correct |
21 |
Correct |
470 ms |
57228 KB |
Output is correct |
22 |
Correct |
454 ms |
55808 KB |
Output is correct |
23 |
Correct |
518 ms |
52624 KB |
Output is correct |
24 |
Correct |
442 ms |
52576 KB |
Output is correct |
25 |
Correct |
548 ms |
55056 KB |
Output is correct |
26 |
Correct |
340 ms |
50800 KB |
Output is correct |
27 |
Correct |
349 ms |
50668 KB |
Output is correct |
28 |
Correct |
99 ms |
39948 KB |
Output is correct |
29 |
Correct |
92 ms |
38292 KB |
Output is correct |
30 |
Correct |
88 ms |
38224 KB |
Output is correct |
31 |
Correct |
91 ms |
38344 KB |
Output is correct |
32 |
Correct |
94 ms |
38388 KB |
Output is correct |
33 |
Correct |
83 ms |
38228 KB |
Output is correct |
34 |
Correct |
59 ms |
39868 KB |
Output is correct |
35 |
Correct |
367 ms |
56628 KB |
Output is correct |
36 |
Correct |
225 ms |
54212 KB |
Output is correct |
37 |
Correct |
241 ms |
54232 KB |
Output is correct |
38 |
Correct |
60 ms |
39756 KB |
Output is correct |
39 |
Correct |
401 ms |
53936 KB |
Output is correct |
40 |
Correct |
490 ms |
58364 KB |
Output is correct |
41 |
Correct |
419 ms |
52924 KB |
Output is correct |
42 |
Correct |
420 ms |
52948 KB |
Output is correct |
43 |
Correct |
60 ms |
39676 KB |
Output is correct |
44 |
Correct |
551 ms |
54136 KB |
Output is correct |
45 |
Correct |
367 ms |
49964 KB |
Output is correct |
46 |
Correct |
412 ms |
50784 KB |
Output is correct |
47 |
Correct |
380 ms |
50272 KB |
Output is correct |
48 |
Correct |
587 ms |
52272 KB |
Output is correct |
49 |
Correct |
835 ms |
53268 KB |
Output is correct |
50 |
Correct |
718 ms |
54336 KB |
Output is correct |
51 |
Correct |
339 ms |
54176 KB |
Output is correct |
52 |
Correct |
247 ms |
54304 KB |
Output is correct |
53 |
Correct |
238 ms |
54256 KB |
Output is correct |
54 |
Correct |
241 ms |
54324 KB |
Output is correct |
55 |
Correct |
276 ms |
51320 KB |
Output is correct |
56 |
Correct |
438 ms |
49744 KB |
Output is correct |
57 |
Correct |
401 ms |
52752 KB |
Output is correct |
58 |
Correct |
484 ms |
49892 KB |
Output is correct |
59 |
Correct |
387 ms |
50292 KB |
Output is correct |