#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 3e5 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int fen[maxn];
vector<int> his;
void upd(int i, bool add = true){
if(add) his.pb(i);
for(i++; i < maxn; i += i&-i) fen[i] += add?1:-1;
}
int get(int i){
int res = 0;
for(i++; i; i -= i&-i) res += fen[i];
return res;
}
vector<pp> adj[maxn];
vector<pp> query1[maxn];
vector<int> query2[maxn];
int cnt[maxn], id[maxn], tp[maxn], ans[maxn], n;
bool ban[maxn], rc[maxn], cr[maxn];
void dfs(int r, int p = -1, bool st = true){ //
cnt[r] = st?1:-1;
for(auto[c, t]: adj[r]) if(!ban[c] && c - p) dfs(c, r, st), cnt[r] += st?cnt[c]:0;
}
int find_cent(int r, int p, int tot){ //
for(auto[c, t]: adj[r]) if(c - p && !ban[c] && (cnt[c]<<1) > tot) return find_cent(c, r, tot);
return r;
}
void dfs2(int r, int p, int ID){
id[r] = ID;
if(r - ID){
rc[r] = rc[p]&(tp[r] > tp[p]);
cr[r] = cr[p]&(tp[r] < tp[p]);
}
for(auto[c, t]: adj[r]) if(c - p && !ban[c]) tp[c] = t, dfs2(c, r, ID);
}
vector<int> tmp;
void dfs3(int r, int p){
if(rc[r]) tmp.pb(tp[r]);
for(auto[c, t]: query1[r]) if(cnt[c] + 1 && id[c] == -1){
ans[t] = cr[r]&(tp[id[r]] < t);
} else if(cnt[c] + 1 && tp[id[c]] > tp[id[r]]){
ans[t] = cr[r]&rc[c]&(tp[c] < t);
}
for(int t: query2[r]) if(cr[r] && tp[id[r]] < t){
ans[t] += get(t);
}
for(auto[c, t]: adj[r]) if(c - p && !ban[c]) dfs3(c, r);
}
void reset(int r){
for(int c: his) upd(c, false);
his.clear();
dfs(r, -1, false);
ban[r] = true;
}
void dec(int r){
dfs(r);
r = find_cent(r, -1, cnt[r]);
sort(all(adj[r]), [&](pp a, pp b){ return a.ss > b.ss; });
rc[r] = cr[r] = true, id[r] = -1;
for(auto[c, t]: adj[r]) if(!ban[c]) rc[c] = cr[c] = true, tp[c] = t, dfs2(c, r, c);
upd(0);
for(auto[c, t]: adj[r]) if(!ban[c]){
dfs3(c, r);
for(int v: tmp) upd(v);
tmp.clear();
}
for(auto[c, t]: query1[r]) if(cnt[c] + 1 && rc[c] && t > tp[c]) ans[t] = true;
for(int t: query2[r]) ans[t] += get(t);
reset(r);
for(auto[c, t]: adj[r]) if(!ban[c]) dec(c);
}
int main(){ IOS();
int k; cin >> n >> k;
vector<int> ty(n+k);
rep(i,0,n+k-1){
char c; cin >> c;
if(c == 'S'){
int u, v; cin >> u >> v; u--, v--;
adj[u].pb({v, i}), adj[v].pb({u, i});
}else if(c == 'Q'){
ty[i] = 1;
int u, v; cin >> u >> v; u--, v--;
if(u - v) query1[v].pb({u, i});
else ans[i] = 1;
}else{
ty[i] = 2;
int d; cin >> d; d--;
query2[d].pb(i);
}
}
dec(0);
rep(i,0,n+k-1) if(ty[i]){
if(ty[i]&1) cout << (ans[i]?"yes":"no") << '\n';
else cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24660 KB |
Output is correct |
2 |
Correct |
46 ms |
25048 KB |
Output is correct |
3 |
Correct |
51 ms |
24904 KB |
Output is correct |
4 |
Correct |
55 ms |
25172 KB |
Output is correct |
5 |
Correct |
48 ms |
24912 KB |
Output is correct |
6 |
Correct |
40 ms |
24872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24660 KB |
Output is correct |
2 |
Correct |
46 ms |
25048 KB |
Output is correct |
3 |
Correct |
51 ms |
24904 KB |
Output is correct |
4 |
Correct |
55 ms |
25172 KB |
Output is correct |
5 |
Correct |
48 ms |
24912 KB |
Output is correct |
6 |
Correct |
40 ms |
24872 KB |
Output is correct |
7 |
Correct |
38 ms |
24620 KB |
Output is correct |
8 |
Correct |
47 ms |
24704 KB |
Output is correct |
9 |
Correct |
49 ms |
25652 KB |
Output is correct |
10 |
Correct |
50 ms |
25956 KB |
Output is correct |
11 |
Correct |
51 ms |
25696 KB |
Output is correct |
12 |
Correct |
48 ms |
25500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24788 KB |
Output is correct |
2 |
Correct |
169 ms |
34496 KB |
Output is correct |
3 |
Correct |
199 ms |
34332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24788 KB |
Output is correct |
2 |
Correct |
169 ms |
34496 KB |
Output is correct |
3 |
Correct |
199 ms |
34332 KB |
Output is correct |
4 |
Correct |
40 ms |
24760 KB |
Output is correct |
5 |
Correct |
178 ms |
34136 KB |
Output is correct |
6 |
Correct |
121 ms |
34560 KB |
Output is correct |
7 |
Correct |
135 ms |
34688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
24708 KB |
Output is correct |
2 |
Correct |
397 ms |
41508 KB |
Output is correct |
3 |
Correct |
389 ms |
41548 KB |
Output is correct |
4 |
Correct |
258 ms |
42444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
24708 KB |
Output is correct |
2 |
Correct |
397 ms |
41508 KB |
Output is correct |
3 |
Correct |
389 ms |
41548 KB |
Output is correct |
4 |
Correct |
258 ms |
42444 KB |
Output is correct |
5 |
Correct |
35 ms |
24652 KB |
Output is correct |
6 |
Correct |
400 ms |
42144 KB |
Output is correct |
7 |
Correct |
285 ms |
45896 KB |
Output is correct |
8 |
Correct |
385 ms |
44568 KB |
Output is correct |
9 |
Correct |
482 ms |
44528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24640 KB |
Output is correct |
2 |
Correct |
265 ms |
33784 KB |
Output is correct |
3 |
Correct |
322 ms |
32848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
24640 KB |
Output is correct |
2 |
Correct |
265 ms |
33784 KB |
Output is correct |
3 |
Correct |
322 ms |
32848 KB |
Output is correct |
4 |
Correct |
36 ms |
24724 KB |
Output is correct |
5 |
Correct |
283 ms |
34296 KB |
Output is correct |
6 |
Correct |
324 ms |
36240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24692 KB |
Output is correct |
2 |
Correct |
446 ms |
41548 KB |
Output is correct |
3 |
Correct |
428 ms |
41444 KB |
Output is correct |
4 |
Correct |
267 ms |
42576 KB |
Output is correct |
5 |
Correct |
34 ms |
24704 KB |
Output is correct |
6 |
Correct |
267 ms |
33864 KB |
Output is correct |
7 |
Correct |
311 ms |
32876 KB |
Output is correct |
8 |
Correct |
356 ms |
33912 KB |
Output is correct |
9 |
Correct |
374 ms |
33956 KB |
Output is correct |
10 |
Correct |
427 ms |
37580 KB |
Output is correct |
11 |
Correct |
446 ms |
36300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24692 KB |
Output is correct |
2 |
Correct |
446 ms |
41548 KB |
Output is correct |
3 |
Correct |
428 ms |
41444 KB |
Output is correct |
4 |
Correct |
267 ms |
42576 KB |
Output is correct |
5 |
Correct |
34 ms |
24704 KB |
Output is correct |
6 |
Correct |
267 ms |
33864 KB |
Output is correct |
7 |
Correct |
311 ms |
32876 KB |
Output is correct |
8 |
Correct |
356 ms |
33912 KB |
Output is correct |
9 |
Correct |
374 ms |
33956 KB |
Output is correct |
10 |
Correct |
427 ms |
37580 KB |
Output is correct |
11 |
Correct |
446 ms |
36300 KB |
Output is correct |
12 |
Correct |
33 ms |
24708 KB |
Output is correct |
13 |
Correct |
428 ms |
42052 KB |
Output is correct |
14 |
Correct |
270 ms |
45988 KB |
Output is correct |
15 |
Correct |
429 ms |
44520 KB |
Output is correct |
16 |
Correct |
459 ms |
44560 KB |
Output is correct |
17 |
Correct |
38 ms |
25500 KB |
Output is correct |
18 |
Correct |
265 ms |
37300 KB |
Output is correct |
19 |
Correct |
340 ms |
36260 KB |
Output is correct |
20 |
Correct |
352 ms |
37312 KB |
Output is correct |
21 |
Correct |
368 ms |
37512 KB |
Output is correct |
22 |
Correct |
404 ms |
39868 KB |
Output is correct |
23 |
Correct |
583 ms |
40452 KB |
Output is correct |
24 |
Correct |
528 ms |
39892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24660 KB |
Output is correct |
2 |
Correct |
44 ms |
25120 KB |
Output is correct |
3 |
Correct |
40 ms |
24840 KB |
Output is correct |
4 |
Correct |
48 ms |
25164 KB |
Output is correct |
5 |
Correct |
45 ms |
24908 KB |
Output is correct |
6 |
Correct |
43 ms |
24844 KB |
Output is correct |
7 |
Correct |
32 ms |
24748 KB |
Output is correct |
8 |
Correct |
182 ms |
34364 KB |
Output is correct |
9 |
Correct |
170 ms |
34368 KB |
Output is correct |
10 |
Correct |
33 ms |
24640 KB |
Output is correct |
11 |
Correct |
364 ms |
41548 KB |
Output is correct |
12 |
Correct |
384 ms |
41492 KB |
Output is correct |
13 |
Correct |
250 ms |
42456 KB |
Output is correct |
14 |
Correct |
32 ms |
24640 KB |
Output is correct |
15 |
Correct |
252 ms |
33928 KB |
Output is correct |
16 |
Correct |
308 ms |
32904 KB |
Output is correct |
17 |
Correct |
374 ms |
33916 KB |
Output is correct |
18 |
Correct |
336 ms |
33952 KB |
Output is correct |
19 |
Correct |
397 ms |
37496 KB |
Output is correct |
20 |
Correct |
445 ms |
36248 KB |
Output is correct |
21 |
Correct |
197 ms |
34508 KB |
Output is correct |
22 |
Correct |
221 ms |
33948 KB |
Output is correct |
23 |
Correct |
265 ms |
33852 KB |
Output is correct |
24 |
Correct |
296 ms |
33904 KB |
Output is correct |
25 |
Correct |
474 ms |
37988 KB |
Output is correct |
26 |
Correct |
340 ms |
31732 KB |
Output is correct |
27 |
Correct |
287 ms |
31472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24660 KB |
Output is correct |
2 |
Correct |
44 ms |
25120 KB |
Output is correct |
3 |
Correct |
40 ms |
24840 KB |
Output is correct |
4 |
Correct |
48 ms |
25164 KB |
Output is correct |
5 |
Correct |
45 ms |
24908 KB |
Output is correct |
6 |
Correct |
43 ms |
24844 KB |
Output is correct |
7 |
Correct |
32 ms |
24748 KB |
Output is correct |
8 |
Correct |
182 ms |
34364 KB |
Output is correct |
9 |
Correct |
170 ms |
34368 KB |
Output is correct |
10 |
Correct |
33 ms |
24640 KB |
Output is correct |
11 |
Correct |
364 ms |
41548 KB |
Output is correct |
12 |
Correct |
384 ms |
41492 KB |
Output is correct |
13 |
Correct |
250 ms |
42456 KB |
Output is correct |
14 |
Correct |
32 ms |
24640 KB |
Output is correct |
15 |
Correct |
252 ms |
33928 KB |
Output is correct |
16 |
Correct |
308 ms |
32904 KB |
Output is correct |
17 |
Correct |
374 ms |
33916 KB |
Output is correct |
18 |
Correct |
336 ms |
33952 KB |
Output is correct |
19 |
Correct |
397 ms |
37496 KB |
Output is correct |
20 |
Correct |
445 ms |
36248 KB |
Output is correct |
21 |
Correct |
197 ms |
34508 KB |
Output is correct |
22 |
Correct |
221 ms |
33948 KB |
Output is correct |
23 |
Correct |
265 ms |
33852 KB |
Output is correct |
24 |
Correct |
296 ms |
33904 KB |
Output is correct |
25 |
Correct |
474 ms |
37988 KB |
Output is correct |
26 |
Correct |
340 ms |
31732 KB |
Output is correct |
27 |
Correct |
287 ms |
31472 KB |
Output is correct |
28 |
Correct |
36 ms |
24632 KB |
Output is correct |
29 |
Correct |
48 ms |
24736 KB |
Output is correct |
30 |
Correct |
42 ms |
25700 KB |
Output is correct |
31 |
Correct |
51 ms |
25980 KB |
Output is correct |
32 |
Correct |
50 ms |
25676 KB |
Output is correct |
33 |
Correct |
42 ms |
25572 KB |
Output is correct |
34 |
Correct |
33 ms |
25560 KB |
Output is correct |
35 |
Correct |
174 ms |
36928 KB |
Output is correct |
36 |
Correct |
107 ms |
34624 KB |
Output is correct |
37 |
Correct |
114 ms |
34840 KB |
Output is correct |
38 |
Correct |
34 ms |
25548 KB |
Output is correct |
39 |
Correct |
419 ms |
44996 KB |
Output is correct |
40 |
Correct |
300 ms |
46076 KB |
Output is correct |
41 |
Correct |
408 ms |
44552 KB |
Output is correct |
42 |
Correct |
394 ms |
44464 KB |
Output is correct |
43 |
Correct |
34 ms |
25572 KB |
Output is correct |
44 |
Correct |
275 ms |
37344 KB |
Output is correct |
45 |
Correct |
315 ms |
36380 KB |
Output is correct |
46 |
Correct |
357 ms |
37396 KB |
Output is correct |
47 |
Correct |
433 ms |
37564 KB |
Output is correct |
48 |
Correct |
395 ms |
39772 KB |
Output is correct |
49 |
Correct |
589 ms |
40376 KB |
Output is correct |
50 |
Correct |
515 ms |
39872 KB |
Output is correct |
51 |
Correct |
147 ms |
36048 KB |
Output is correct |
52 |
Correct |
119 ms |
35576 KB |
Output is correct |
53 |
Correct |
117 ms |
35136 KB |
Output is correct |
54 |
Correct |
163 ms |
35716 KB |
Output is correct |
55 |
Correct |
122 ms |
35020 KB |
Output is correct |
56 |
Correct |
269 ms |
36292 KB |
Output is correct |
57 |
Correct |
379 ms |
39204 KB |
Output is correct |
58 |
Correct |
441 ms |
35788 KB |
Output is correct |
59 |
Correct |
298 ms |
34968 KB |
Output is correct |