//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <set>
//#include <ext/pb_ds/assoc_contatiner.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug() {cout << endl;}
template<class T, class ... U> void debug(T a, U ... b) { cout << a << " ";debug(b...);}
template<class T> void pary (T l, T r) {
while (l != r) cout << (*l) << " ", l++;
cout << endl;
}
#define ll long long
#define maxn 120005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct query{
int ti, a, b;
query() {ti = 0, a = 0, b = 0;}
query(int x, int y, int z) {ti = x, a = y, b = z;}
};
vector<query> que;
int anc[17][maxn], ma[17][maxn], mi[17][maxn], dep[maxn];
bool inc[17][maxn], decr[17][maxn];
vector<pii> adj[maxn];
void dfs(int n, int par, int d) {
anc[0][n] = par;
dep[n] = d;
inc[0][n] = decr[0][n] = 1;
for (auto v:adj[n]) {
if (v.ff != par) {
ma[0][v.ff] = mi[0][v.ff] = v.ss;
dfs(v.ff, n, d + 1);
}
}
}
pair<int, bool> mono(int a, int b) {
bool sw = 0, ic = 1, dc = 1;
int big = 0, small = 1<<20;
if (dep[a] < dep[b]) sw = 1, swap(a, b);
int hdif = dep[a] - dep[b], cnt = 0;
while (hdif) {
if (hdif & 1) {
ic &= inc[cnt][a] && ma[0][a] > big;
dc &= decr[cnt][a] && ma[0][a] < small;
big = max(big, ma[cnt][a]), small = min(small, mi[cnt][a]);
a = anc[cnt][a];
}
hdif >>= 1, cnt++;
}
return {big, sw ? dc : ic};
}
pair<int, bool> lca(int a, int b) {
bool sw = 0;
if (dep[a] < dep[b]) sw = 1, swap(a, b);
int hdif = dep[a] - dep[b], cnt = 0;
while (hdif) {
if (hdif & 1) {
a = anc[cnt][a];
}
hdif >>= 1, cnt++;
}
if (a == b) return {a, true};
for (int i = 16;i >= 0;i--) {
if (anc[i][a] != anc[i][b]) {
a = anc[i][a], b = anc[i][b];
}
}
bool val = ma[0][a] < ma[0][b];
return {anc[0][a], val ^ sw};
}
int main() {
io
int n, k;
cin >> n >> k;
int cur = 1;
for (int i = 0;i < n + k - 1;i++) {
char type;
cin >> type;
if (type == 'S') {
int a, b;
cin >> a >> b;
adj[a].push_back({b, cur});
adj[b].push_back({a, cur});
cur++;
que.push_back(query(0, a, b));
} else if (type == 'Q') {
int a, b;
cin >> a >> b;
que.push_back(query(1, b, a));
} else {
int d;
cin >> d;
que.push_back(query(2, d, 0));
}
}
dfs(1, 0, 1);
//pary(ma[0] + 1, ma[0] + n + 1);
for (int i = 1;i < 17;i++) {
for (int j = 1;j <= n;j++) {
anc[i][j] = anc[i - 1][anc[i - 1][j]];
ma[i][j] = max(ma[i - 1][j], ma[i - 1][anc[i - 1][j]]);
mi[i][j] = min(mi[i - 1][j], mi[i - 1][anc[i - 1][j]]);
inc[i][j] = inc[i - 1][j] && inc[i - 1][anc[i - 1][j]] && ma[i - 1][j] < ma[0][anc[i - 1][j]];
decr[i][j] = decr[i - 1][j] && decr[i - 1][anc[i - 1][j]] && mi[i - 1][j] > ma[0][anc[i - 1][j]];
//debug(i, j, ma[i][j], mi[i][j]);
}
}
cur = 0;
for (auto i:que) {
if (i.ti == 1) {
pii p1 = lca(i.a, i.b);
//debug(p1.ff, p1.ss);
pii p2 = mono(i.a, p1.ff), p3 = mono(p1.ff, i.b);
//debug(p2.ff, p2.ss, p3.ff, p3.ss);
int big = max(p2.ff, p3.ff);
bool ans = (big <= cur) && p2.ss && p3.ss && p1.ss;
cout << (ans ? "yes" : "no") << "\n";
} else if (i.ti == 0) {
cur++;
}
}
}
/*
6 3
S 1 2
S 1 3
S 3 4
Q 5 1
S 4 5
S 1 6
Q 5 1
Q 1 5
C 1
C 2
C 3
C 4
C 5
C 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6324 KB |
Output is correct |
2 |
Correct |
59 ms |
7760 KB |
Output is correct |
3 |
Correct |
50 ms |
7912 KB |
Output is correct |
4 |
Correct |
74 ms |
7956 KB |
Output is correct |
5 |
Correct |
60 ms |
8164 KB |
Output is correct |
6 |
Correct |
47 ms |
7840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6324 KB |
Output is correct |
2 |
Correct |
59 ms |
7760 KB |
Output is correct |
3 |
Correct |
50 ms |
7912 KB |
Output is correct |
4 |
Correct |
74 ms |
7956 KB |
Output is correct |
5 |
Correct |
60 ms |
8164 KB |
Output is correct |
6 |
Correct |
47 ms |
7840 KB |
Output is correct |
7 |
Incorrect |
39 ms |
6284 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6336 KB |
Output is correct |
2 |
Correct |
166 ms |
42360 KB |
Output is correct |
3 |
Correct |
154 ms |
42212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6336 KB |
Output is correct |
2 |
Correct |
166 ms |
42360 KB |
Output is correct |
3 |
Correct |
154 ms |
42212 KB |
Output is correct |
4 |
Incorrect |
38 ms |
6320 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
6324 KB |
Output is correct |
2 |
Correct |
159 ms |
47100 KB |
Output is correct |
3 |
Correct |
164 ms |
47036 KB |
Output is correct |
4 |
Correct |
201 ms |
47040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
6324 KB |
Output is correct |
2 |
Correct |
159 ms |
47100 KB |
Output is correct |
3 |
Correct |
164 ms |
47036 KB |
Output is correct |
4 |
Correct |
201 ms |
47040 KB |
Output is correct |
5 |
Incorrect |
40 ms |
6336 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6232 KB |
Output is correct |
2 |
Correct |
171 ms |
42720 KB |
Output is correct |
3 |
Correct |
148 ms |
43164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6232 KB |
Output is correct |
2 |
Correct |
171 ms |
42720 KB |
Output is correct |
3 |
Correct |
148 ms |
43164 KB |
Output is correct |
4 |
Incorrect |
40 ms |
6284 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6224 KB |
Output is correct |
2 |
Correct |
184 ms |
46968 KB |
Output is correct |
3 |
Correct |
140 ms |
47016 KB |
Output is correct |
4 |
Correct |
202 ms |
46936 KB |
Output is correct |
5 |
Correct |
44 ms |
6240 KB |
Output is correct |
6 |
Correct |
171 ms |
42736 KB |
Output is correct |
7 |
Correct |
150 ms |
43248 KB |
Output is correct |
8 |
Correct |
270 ms |
43656 KB |
Output is correct |
9 |
Correct |
221 ms |
43500 KB |
Output is correct |
10 |
Correct |
216 ms |
47380 KB |
Output is correct |
11 |
Correct |
351 ms |
46836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6224 KB |
Output is correct |
2 |
Correct |
184 ms |
46968 KB |
Output is correct |
3 |
Correct |
140 ms |
47016 KB |
Output is correct |
4 |
Correct |
202 ms |
46936 KB |
Output is correct |
5 |
Correct |
44 ms |
6240 KB |
Output is correct |
6 |
Correct |
171 ms |
42736 KB |
Output is correct |
7 |
Correct |
150 ms |
43248 KB |
Output is correct |
8 |
Correct |
270 ms |
43656 KB |
Output is correct |
9 |
Correct |
221 ms |
43500 KB |
Output is correct |
10 |
Correct |
216 ms |
47380 KB |
Output is correct |
11 |
Correct |
351 ms |
46836 KB |
Output is correct |
12 |
Incorrect |
39 ms |
6164 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6200 KB |
Output is correct |
2 |
Correct |
62 ms |
7780 KB |
Output is correct |
3 |
Correct |
52 ms |
7924 KB |
Output is correct |
4 |
Correct |
75 ms |
7920 KB |
Output is correct |
5 |
Correct |
63 ms |
8100 KB |
Output is correct |
6 |
Correct |
48 ms |
7804 KB |
Output is correct |
7 |
Correct |
45 ms |
6344 KB |
Output is correct |
8 |
Correct |
152 ms |
42224 KB |
Output is correct |
9 |
Correct |
145 ms |
42328 KB |
Output is correct |
10 |
Correct |
38 ms |
6276 KB |
Output is correct |
11 |
Correct |
161 ms |
49340 KB |
Output is correct |
12 |
Correct |
167 ms |
49412 KB |
Output is correct |
13 |
Correct |
238 ms |
49260 KB |
Output is correct |
14 |
Correct |
41 ms |
6324 KB |
Output is correct |
15 |
Correct |
185 ms |
42768 KB |
Output is correct |
16 |
Correct |
143 ms |
43124 KB |
Output is correct |
17 |
Correct |
245 ms |
43516 KB |
Output is correct |
18 |
Correct |
206 ms |
43664 KB |
Output is correct |
19 |
Correct |
204 ms |
47356 KB |
Output is correct |
20 |
Correct |
315 ms |
46852 KB |
Output is correct |
21 |
Correct |
174 ms |
42808 KB |
Output is correct |
22 |
Correct |
167 ms |
42952 KB |
Output is correct |
23 |
Correct |
216 ms |
43248 KB |
Output is correct |
24 |
Correct |
180 ms |
43168 KB |
Output is correct |
25 |
Correct |
161 ms |
44388 KB |
Output is correct |
26 |
Correct |
147 ms |
42620 KB |
Output is correct |
27 |
Correct |
152 ms |
42724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6200 KB |
Output is correct |
2 |
Correct |
62 ms |
7780 KB |
Output is correct |
3 |
Correct |
52 ms |
7924 KB |
Output is correct |
4 |
Correct |
75 ms |
7920 KB |
Output is correct |
5 |
Correct |
63 ms |
8100 KB |
Output is correct |
6 |
Correct |
48 ms |
7804 KB |
Output is correct |
7 |
Correct |
45 ms |
6344 KB |
Output is correct |
8 |
Correct |
152 ms |
42224 KB |
Output is correct |
9 |
Correct |
145 ms |
42328 KB |
Output is correct |
10 |
Correct |
38 ms |
6276 KB |
Output is correct |
11 |
Correct |
161 ms |
49340 KB |
Output is correct |
12 |
Correct |
167 ms |
49412 KB |
Output is correct |
13 |
Correct |
238 ms |
49260 KB |
Output is correct |
14 |
Correct |
41 ms |
6324 KB |
Output is correct |
15 |
Correct |
185 ms |
42768 KB |
Output is correct |
16 |
Correct |
143 ms |
43124 KB |
Output is correct |
17 |
Correct |
245 ms |
43516 KB |
Output is correct |
18 |
Correct |
206 ms |
43664 KB |
Output is correct |
19 |
Correct |
204 ms |
47356 KB |
Output is correct |
20 |
Correct |
315 ms |
46852 KB |
Output is correct |
21 |
Correct |
174 ms |
42808 KB |
Output is correct |
22 |
Correct |
167 ms |
42952 KB |
Output is correct |
23 |
Correct |
216 ms |
43248 KB |
Output is correct |
24 |
Correct |
180 ms |
43168 KB |
Output is correct |
25 |
Correct |
161 ms |
44388 KB |
Output is correct |
26 |
Correct |
147 ms |
42620 KB |
Output is correct |
27 |
Correct |
152 ms |
42724 KB |
Output is correct |
28 |
Incorrect |
50 ms |
6200 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |