#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
const int MAX_N = 3e5;
const int infinity = 1e9;
vii tree[MAX_N];
int depth[MAX_N];
pii dp[20][MAX_N];
int increase[MAX_N], decrease[MAX_N];
int loga[MAX_N];
void Dfs(int node, int parent, int w, int len_i, int len_d, int dist) {
depth[node] = dist;
dp[0][node] = {parent, w};
increase[node] = len_i, decrease[node] = len_d;
for (auto [neighbor, d] : tree[node]) {
if (neighbor == parent) continue;
Dfs(neighbor, node, d, (w > d ? len_i : 0) + 1, (w < d ? len_d : 0) + 1, dist + 1);
}
}
bool Dfs(int node, int end, int w) {
if (node == end) return true;
for (auto [neighbor, d] : tree[node]) {
if (d >= w) continue;
if (Dfs(neighbor, end, d)) {
return true;
}
}
return false;
}
int Dfs(int node, int w) {
int c = 1;
for (auto [neighbor, d] : tree[node]) {
if (d <= w) continue;
c += Dfs(neighbor, d);
}
return c;
}
int n, q;
pii Jump(int a, int k) {
int max_w = 0;
for (int i = 0; i <= loga[n]; i++) {
if ((k >> i) & 1) {
chkmax(max_w, dp[i][a].y);
a = dp[i][a].x;
}
}
return {a, max_w};
}
int Solve(int v, int u) {
if (u == v) return 0;
int a = v, b = u;
int max_w = 0;
if (depth[a] > depth[b]) {
pii p = Jump(a, depth[a] - depth[b]);
a = p.x, max_w = p.y;
} else {
pii p = Jump(b, depth[b] - depth[a]);
b = p.x, max_w = p.y;
}
bool bad = a != b;
for (int i = loga[n]; i >= 0; i--) {
if (dp[i][a].x != dp[i][b].x) {
chkmax(max_w, dp[i][a].y);
chkmax(max_w, dp[i][b].y);
a = dp[i][a].x, b = dp[i][b].x;
}
}
int dep_lca = depth[a] - bad;
if (bad) {
chkmax(max_w, dp[0][a].y);
chkmax(max_w, dp[0][b].y);
}
if (dep_lca < depth[v] - decrease[v] || dep_lca < depth[u] - increase[u]) {
return infinity;
}
return (!bad || dp[0][a].y > dp[0][b].y ? max_w : infinity);
}
int cnt[MAX_N][2];
int graph[MAX_N][2];
int Solve(vi &v, int i, int count, int d, int end) {
int node = v[i];
if (node == end) {
return count + cnt[node][0] + cnt[node][1] + 1;
}
if (v[i + 1] == 2 * node) {
if (graph[node][0] == -1) {
return Solve(v, i + 1, 0, 0, end);
}
if (graph[node][0] > d) count = 0;
count += (graph[node][0] < graph[node][1]) * cnt[node][1];
count++;
return Solve(v, i + 1, count, graph[node][0], end);
}
if (graph[node][1] == -1) {
return Solve(v, i + 1, 0, 0, end);
}
if (graph[node][1] > d) count = 0;
count += (graph[node][1] < graph[node][0]) * cnt[node][0];
count++;
return Solve(v, i + 1, count, graph[node][1], end);
}
char type[MAX_N];
int a[MAX_N], d[MAX_N], ans[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
graph[i][0] = graph[i][1] = -1;
}
// for (int i = 1; i <= n; i++) cnt[i] = 1;
for (int i = 0; i < n + q - 1; i++) {
cin >> type[i];
if (type[i] == 'S') {
int A, b;
cin >> A >> b;
tree[A].push_back({b, i});
tree[b].push_back({A, i});
int x = max(A, b);
int y = min(A, b);
graph[y][x & 1] = i;
int last = i;
cnt[y][x & 1]++;
x >>= 1;
while (x) {
int par = x >> 1;
int next_w = graph[par][x & 1];
if (next_w == -1 || next_w > last) break;
cnt[par][x & 1]++;
x = par, last = next_w;
}
} else if (type[i] == 'Q') {
cin >> a[i] >> d[i];
} else {
cin >> d[i];
// vi path;
int x = d[i];
int count = cnt[x][0] + cnt[x][1] + 1;
int last = graph[x >> 1][x & 1];
if (last != -1) {
while (x > 1) {
int par = x >> 1;
int next_w = graph[par][x & 1];
if (next_w == -1 || next_w < last) break;
count += (graph[par][x & 1] < graph[par][x & 1 ^ 1]) * cnt[par][x & 1 ^ 1];
count++;
// path.push_back(x);
x = par, last = next_w;
}
}
ans[i] = count;
// reverse(all(path));
// ans[i] = Solve(path, 0, 0, 0, d[i]);
}
}
for (int i = 2; i <= n; i++) {
loga[i] = loga[i >> 1] + 1;
}
Dfs(1, 0, 0, 0, 0, 0);
for (int i = 1; i <= loga[n]; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j].x = dp[i - 1][dp[i - 1][j].x].x;
dp[i][j].y = max(dp[i - 1][j].y, dp[i - 1][dp[i - 1][j].x].y);
}
}
for (int i = 0; i < n + q - 1; i++) {
if (type[i] == 'Q') {
ans[i] = Solve(a[i], d[i]) <= i;
}
}
for (int i = 0; i < n + q - 1; i++) {
if (type[i] == 'Q') {
cout << (ans[i] ? "yes\n" : "no\n");
} else if (type[i] == 'C') {
cout << ans[i] << '\n';
}
}
return 0;
}
//5 7
//S 1 2
//S 1 3
//S 2 4
//C 1
//C 4
//S 2 5
//C 1
//C 2
//C 3
//C 4
//C 5
//5 5
//S 2 4
//S 1 3
//S 1 2
//S 2 5
//C 1
//C 2
//C 3
//C 4
//C 5
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:168:64: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
168 | count += (graph[par][x & 1] < graph[par][x & 1 ^ 1]) * cnt[par][x & 1 ^ 1];
| ^
servers.cpp:168:87: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
168 | count += (graph[par][x & 1] < graph[par][x & 1 ^ 1]) * cnt[par][x & 1 ^ 1];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9292 KB |
Output is correct |
2 |
Correct |
36 ms |
10060 KB |
Output is correct |
3 |
Correct |
33 ms |
10056 KB |
Output is correct |
4 |
Correct |
42 ms |
10288 KB |
Output is correct |
5 |
Correct |
42 ms |
10356 KB |
Output is correct |
6 |
Correct |
34 ms |
10136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9292 KB |
Output is correct |
2 |
Correct |
36 ms |
10060 KB |
Output is correct |
3 |
Correct |
33 ms |
10056 KB |
Output is correct |
4 |
Correct |
42 ms |
10288 KB |
Output is correct |
5 |
Correct |
42 ms |
10356 KB |
Output is correct |
6 |
Correct |
34 ms |
10136 KB |
Output is correct |
7 |
Incorrect |
28 ms |
9232 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9300 KB |
Output is correct |
2 |
Correct |
119 ms |
34364 KB |
Output is correct |
3 |
Correct |
150 ms |
34492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9300 KB |
Output is correct |
2 |
Correct |
119 ms |
34364 KB |
Output is correct |
3 |
Correct |
150 ms |
34492 KB |
Output is correct |
4 |
Incorrect |
24 ms |
9320 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9300 KB |
Output is correct |
2 |
Correct |
133 ms |
43908 KB |
Output is correct |
3 |
Correct |
148 ms |
43852 KB |
Output is correct |
4 |
Correct |
136 ms |
43800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9300 KB |
Output is correct |
2 |
Correct |
133 ms |
43908 KB |
Output is correct |
3 |
Correct |
148 ms |
43852 KB |
Output is correct |
4 |
Correct |
136 ms |
43800 KB |
Output is correct |
5 |
Incorrect |
24 ms |
9244 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
9316 KB |
Output is correct |
2 |
Correct |
130 ms |
34864 KB |
Output is correct |
3 |
Correct |
145 ms |
34700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
9316 KB |
Output is correct |
2 |
Correct |
130 ms |
34864 KB |
Output is correct |
3 |
Correct |
145 ms |
34700 KB |
Output is correct |
4 |
Correct |
27 ms |
9292 KB |
Output is correct |
5 |
Correct |
104 ms |
34852 KB |
Output is correct |
6 |
Correct |
129 ms |
34520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9268 KB |
Output is correct |
2 |
Correct |
129 ms |
43844 KB |
Output is correct |
3 |
Correct |
135 ms |
43820 KB |
Output is correct |
4 |
Correct |
134 ms |
43924 KB |
Output is correct |
5 |
Correct |
25 ms |
9360 KB |
Output is correct |
6 |
Correct |
113 ms |
34804 KB |
Output is correct |
7 |
Correct |
120 ms |
34712 KB |
Output is correct |
8 |
Correct |
171 ms |
36064 KB |
Output is correct |
9 |
Correct |
131 ms |
35560 KB |
Output is correct |
10 |
Correct |
151 ms |
40660 KB |
Output is correct |
11 |
Correct |
205 ms |
40012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
9268 KB |
Output is correct |
2 |
Correct |
129 ms |
43844 KB |
Output is correct |
3 |
Correct |
135 ms |
43820 KB |
Output is correct |
4 |
Correct |
134 ms |
43924 KB |
Output is correct |
5 |
Correct |
25 ms |
9360 KB |
Output is correct |
6 |
Correct |
113 ms |
34804 KB |
Output is correct |
7 |
Correct |
120 ms |
34712 KB |
Output is correct |
8 |
Correct |
171 ms |
36064 KB |
Output is correct |
9 |
Correct |
131 ms |
35560 KB |
Output is correct |
10 |
Correct |
151 ms |
40660 KB |
Output is correct |
11 |
Correct |
205 ms |
40012 KB |
Output is correct |
12 |
Incorrect |
24 ms |
9300 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
9328 KB |
Output is correct |
2 |
Correct |
38 ms |
10060 KB |
Output is correct |
3 |
Correct |
43 ms |
10060 KB |
Output is correct |
4 |
Correct |
43 ms |
10196 KB |
Output is correct |
5 |
Correct |
37 ms |
10356 KB |
Output is correct |
6 |
Correct |
32 ms |
10060 KB |
Output is correct |
7 |
Correct |
24 ms |
9332 KB |
Output is correct |
8 |
Correct |
118 ms |
34452 KB |
Output is correct |
9 |
Correct |
127 ms |
34360 KB |
Output is correct |
10 |
Correct |
27 ms |
9300 KB |
Output is correct |
11 |
Correct |
132 ms |
43784 KB |
Output is correct |
12 |
Correct |
128 ms |
43796 KB |
Output is correct |
13 |
Correct |
130 ms |
43848 KB |
Output is correct |
14 |
Correct |
26 ms |
9288 KB |
Output is correct |
15 |
Correct |
115 ms |
34860 KB |
Output is correct |
16 |
Correct |
129 ms |
34636 KB |
Output is correct |
17 |
Correct |
162 ms |
36128 KB |
Output is correct |
18 |
Correct |
147 ms |
35588 KB |
Output is correct |
19 |
Correct |
141 ms |
40700 KB |
Output is correct |
20 |
Correct |
188 ms |
39968 KB |
Output is correct |
21 |
Correct |
128 ms |
35284 KB |
Output is correct |
22 |
Correct |
132 ms |
35380 KB |
Output is correct |
23 |
Correct |
138 ms |
35400 KB |
Output is correct |
24 |
Correct |
128 ms |
35500 KB |
Output is correct |
25 |
Correct |
136 ms |
37560 KB |
Output is correct |
26 |
Correct |
141 ms |
33876 KB |
Output is correct |
27 |
Correct |
115 ms |
33736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
9328 KB |
Output is correct |
2 |
Correct |
38 ms |
10060 KB |
Output is correct |
3 |
Correct |
43 ms |
10060 KB |
Output is correct |
4 |
Correct |
43 ms |
10196 KB |
Output is correct |
5 |
Correct |
37 ms |
10356 KB |
Output is correct |
6 |
Correct |
32 ms |
10060 KB |
Output is correct |
7 |
Correct |
24 ms |
9332 KB |
Output is correct |
8 |
Correct |
118 ms |
34452 KB |
Output is correct |
9 |
Correct |
127 ms |
34360 KB |
Output is correct |
10 |
Correct |
27 ms |
9300 KB |
Output is correct |
11 |
Correct |
132 ms |
43784 KB |
Output is correct |
12 |
Correct |
128 ms |
43796 KB |
Output is correct |
13 |
Correct |
130 ms |
43848 KB |
Output is correct |
14 |
Correct |
26 ms |
9288 KB |
Output is correct |
15 |
Correct |
115 ms |
34860 KB |
Output is correct |
16 |
Correct |
129 ms |
34636 KB |
Output is correct |
17 |
Correct |
162 ms |
36128 KB |
Output is correct |
18 |
Correct |
147 ms |
35588 KB |
Output is correct |
19 |
Correct |
141 ms |
40700 KB |
Output is correct |
20 |
Correct |
188 ms |
39968 KB |
Output is correct |
21 |
Correct |
128 ms |
35284 KB |
Output is correct |
22 |
Correct |
132 ms |
35380 KB |
Output is correct |
23 |
Correct |
138 ms |
35400 KB |
Output is correct |
24 |
Correct |
128 ms |
35500 KB |
Output is correct |
25 |
Correct |
136 ms |
37560 KB |
Output is correct |
26 |
Correct |
141 ms |
33876 KB |
Output is correct |
27 |
Correct |
115 ms |
33736 KB |
Output is correct |
28 |
Incorrect |
30 ms |
9312 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |