#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=3e5+5;
int N,K;
char tp[MX];
int A[MX], B[MX];
vector<pair<int,int>> adj[MX], Q[MX];
vector<int> C[MX];
bool vis[MX];
int ans[MX], sz[MX], stk[MX];
struct fenwick {
int t[MX];
void upd(int v, int x) {
for(int i=v;i<MX;i+=i&-i) t[i]+=x;
}
int que(int v) {
int res=0;
for(int i=v;i>0;i-=i&-i) res+=t[i];
return res;
}
int que(int l, int r) {
return que(r)-que(l-1);
}
} ft;
int getSize(int v, int p) {
sz[v]=1;
for(auto [u,w]:adj[v]) {
if(u==p || vis[u]) continue;
sz[v]+=getSize(u,v);
}
return sz[v];
}
int getCen(int v, int p, int s) {
for(auto [u,w]:adj[v]) {
if(u==p || vis[u] || 2*sz[u]<=s) continue;
return getCen(u,v,s);
}
return v;
}
void calc(int v, int p, int prv, int head) {
for(auto [a,id]:Q[v]) {
if(stk[a]!=-1 && stk[a]<=id) {
ans[id]=1;
}
}
for(auto id:C[v]) {
if(head>id) continue;
ans[id]+=ft.que(head,id);
}
for(auto [u,w]:adj[v]) {
if(u==p || vis[u]) continue;
if(prv<w) continue;
calc(u,v,w,head);
}
}
void upd(int v, int p, int prv) {
stk[v]=prv;
ft.upd(prv,1);
for(auto [u,w]:adj[v]) {
if(u==p || vis[u]) continue;
if(prv>w) continue;
upd(u,v,w);
}
}
void rem(int v, int p, int prv) {
stk[v]=-1;
ft.upd(prv,-1);
for(auto [u,w]:adj[v]) {
if(u==p || vis[u]) continue;
if(prv>w) continue;
rem(u,v,w);
}
}
void cdc(int v, int p) {
int s=getSize(v,v);
int cen=getCen(v,v,s);
vis[cen]=1;
for(auto [u,w]:adj[cen]) {
if(vis[u]) continue;
stk[cen]=w;
ft.upd(w,1);
calc(u,cen,w,w);
upd(u,cen,w);
ft.upd(w,-1);
}
stk[cen]=0;
for(auto [a,id]:Q[cen]) {
if(stk[a]!=-1 && stk[a]<=id) {
ans[id]=1;
}
}
for(auto id:C[cen]) {
ans[id]+=ft.que(1,id);
}
stk[cen]=-1;
for(auto [u,w]:adj[cen]) {
if(vis[u]) continue;
rem(u,cen,w);
}
for(auto [u,w]:adj[cen]) {
if(vis[u]) continue;
cdc(u,cen);
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>K;
for(int i=0;i<MX;i++) stk[i]=-1;
for(int i=1;i<N+K;i++) {
cin>>tp[i]>>A[i];
if(tp[i]=='S') {
cin>>B[i];
adj[A[i]].push_back({B[i],i});
adj[B[i]].push_back({A[i],i});
} else if(tp[i]=='Q') {
cin>>B[i];
// B -> A path increasing
Q[B[i]].push_back({A[i],i});
} else {
C[A[i]].push_back(i);
}
}
for(int i=1;i<=N;i++) {
sort(adj[i].begin(),adj[i].end(),[&](auto x, auto y) {
return x.second>y.second;
});
}
cdc(1,0);
for(int i=1;i<N+K;i++) {
if(tp[i]=='S') continue;
if(tp[i]=='Q') {
if(ans[i]) cout<<"yes\n";
else cout<<"no\n";
} else {
cout<<ans[i]+1<<'\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
27484 KB |
Output is correct |
2 |
Correct |
32 ms |
27988 KB |
Output is correct |
3 |
Correct |
29 ms |
28248 KB |
Output is correct |
4 |
Correct |
36 ms |
28240 KB |
Output is correct |
5 |
Correct |
30 ms |
27872 KB |
Output is correct |
6 |
Correct |
30 ms |
27920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
27484 KB |
Output is correct |
2 |
Correct |
32 ms |
27988 KB |
Output is correct |
3 |
Correct |
29 ms |
28248 KB |
Output is correct |
4 |
Correct |
36 ms |
28240 KB |
Output is correct |
5 |
Correct |
30 ms |
27872 KB |
Output is correct |
6 |
Correct |
30 ms |
27920 KB |
Output is correct |
7 |
Correct |
25 ms |
27484 KB |
Output is correct |
8 |
Correct |
33 ms |
27740 KB |
Output is correct |
9 |
Correct |
31 ms |
27472 KB |
Output is correct |
10 |
Correct |
46 ms |
27728 KB |
Output is correct |
11 |
Correct |
34 ms |
27472 KB |
Output is correct |
12 |
Correct |
31 ms |
27220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
27476 KB |
Output is correct |
2 |
Correct |
94 ms |
37576 KB |
Output is correct |
3 |
Correct |
84 ms |
37568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
27476 KB |
Output is correct |
2 |
Correct |
94 ms |
37576 KB |
Output is correct |
3 |
Correct |
84 ms |
37568 KB |
Output is correct |
4 |
Correct |
26 ms |
27644 KB |
Output is correct |
5 |
Correct |
84 ms |
37236 KB |
Output is correct |
6 |
Correct |
66 ms |
34832 KB |
Output is correct |
7 |
Correct |
63 ms |
35012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
27472 KB |
Output is correct |
2 |
Correct |
149 ms |
44664 KB |
Output is correct |
3 |
Correct |
147 ms |
44628 KB |
Output is correct |
4 |
Correct |
131 ms |
44368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
27472 KB |
Output is correct |
2 |
Correct |
149 ms |
44664 KB |
Output is correct |
3 |
Correct |
147 ms |
44628 KB |
Output is correct |
4 |
Correct |
131 ms |
44368 KB |
Output is correct |
5 |
Correct |
29 ms |
27476 KB |
Output is correct |
6 |
Correct |
163 ms |
44848 KB |
Output is correct |
7 |
Correct |
141 ms |
44880 KB |
Output is correct |
8 |
Correct |
143 ms |
43864 KB |
Output is correct |
9 |
Correct |
150 ms |
43860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
27476 KB |
Output is correct |
2 |
Correct |
128 ms |
37876 KB |
Output is correct |
3 |
Correct |
116 ms |
37968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
27476 KB |
Output is correct |
2 |
Correct |
128 ms |
37876 KB |
Output is correct |
3 |
Correct |
116 ms |
37968 KB |
Output is correct |
4 |
Correct |
23 ms |
27404 KB |
Output is correct |
5 |
Correct |
136 ms |
37972 KB |
Output is correct |
6 |
Correct |
124 ms |
37972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
27472 KB |
Output is correct |
2 |
Correct |
145 ms |
44628 KB |
Output is correct |
3 |
Correct |
149 ms |
44624 KB |
Output is correct |
4 |
Correct |
131 ms |
44368 KB |
Output is correct |
5 |
Correct |
24 ms |
27480 KB |
Output is correct |
6 |
Correct |
124 ms |
37972 KB |
Output is correct |
7 |
Correct |
138 ms |
37972 KB |
Output is correct |
8 |
Correct |
128 ms |
38228 KB |
Output is correct |
9 |
Correct |
126 ms |
38740 KB |
Output is correct |
10 |
Correct |
169 ms |
41900 KB |
Output is correct |
11 |
Correct |
166 ms |
40272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
27472 KB |
Output is correct |
2 |
Correct |
145 ms |
44628 KB |
Output is correct |
3 |
Correct |
149 ms |
44624 KB |
Output is correct |
4 |
Correct |
131 ms |
44368 KB |
Output is correct |
5 |
Correct |
24 ms |
27480 KB |
Output is correct |
6 |
Correct |
124 ms |
37972 KB |
Output is correct |
7 |
Correct |
138 ms |
37972 KB |
Output is correct |
8 |
Correct |
128 ms |
38228 KB |
Output is correct |
9 |
Correct |
126 ms |
38740 KB |
Output is correct |
10 |
Correct |
169 ms |
41900 KB |
Output is correct |
11 |
Correct |
166 ms |
40272 KB |
Output is correct |
12 |
Correct |
24 ms |
27480 KB |
Output is correct |
13 |
Correct |
148 ms |
44628 KB |
Output is correct |
14 |
Correct |
174 ms |
44792 KB |
Output is correct |
15 |
Correct |
152 ms |
43860 KB |
Output is correct |
16 |
Correct |
153 ms |
43968 KB |
Output is correct |
17 |
Correct |
25 ms |
27484 KB |
Output is correct |
18 |
Correct |
139 ms |
37928 KB |
Output is correct |
19 |
Correct |
138 ms |
37716 KB |
Output is correct |
20 |
Correct |
153 ms |
38488 KB |
Output is correct |
21 |
Correct |
143 ms |
38996 KB |
Output is correct |
22 |
Correct |
184 ms |
40276 KB |
Output is correct |
23 |
Correct |
228 ms |
41004 KB |
Output is correct |
24 |
Correct |
209 ms |
40532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
27324 KB |
Output is correct |
2 |
Correct |
32 ms |
28164 KB |
Output is correct |
3 |
Correct |
29 ms |
28248 KB |
Output is correct |
4 |
Correct |
30 ms |
28228 KB |
Output is correct |
5 |
Correct |
31 ms |
27968 KB |
Output is correct |
6 |
Correct |
31 ms |
27984 KB |
Output is correct |
7 |
Correct |
30 ms |
27472 KB |
Output is correct |
8 |
Correct |
99 ms |
37484 KB |
Output is correct |
9 |
Correct |
90 ms |
37576 KB |
Output is correct |
10 |
Correct |
29 ms |
27472 KB |
Output is correct |
11 |
Correct |
150 ms |
44584 KB |
Output is correct |
12 |
Correct |
136 ms |
44624 KB |
Output is correct |
13 |
Correct |
137 ms |
44368 KB |
Output is correct |
14 |
Correct |
21 ms |
27480 KB |
Output is correct |
15 |
Correct |
154 ms |
37728 KB |
Output is correct |
16 |
Correct |
136 ms |
37976 KB |
Output is correct |
17 |
Correct |
146 ms |
38224 KB |
Output is correct |
18 |
Correct |
169 ms |
38836 KB |
Output is correct |
19 |
Correct |
231 ms |
42068 KB |
Output is correct |
20 |
Correct |
179 ms |
40276 KB |
Output is correct |
21 |
Correct |
119 ms |
37868 KB |
Output is correct |
22 |
Correct |
128 ms |
37976 KB |
Output is correct |
23 |
Correct |
147 ms |
38112 KB |
Output is correct |
24 |
Correct |
168 ms |
38416 KB |
Output is correct |
25 |
Correct |
182 ms |
41336 KB |
Output is correct |
26 |
Correct |
140 ms |
36768 KB |
Output is correct |
27 |
Correct |
146 ms |
36692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
27324 KB |
Output is correct |
2 |
Correct |
32 ms |
28164 KB |
Output is correct |
3 |
Correct |
29 ms |
28248 KB |
Output is correct |
4 |
Correct |
30 ms |
28228 KB |
Output is correct |
5 |
Correct |
31 ms |
27968 KB |
Output is correct |
6 |
Correct |
31 ms |
27984 KB |
Output is correct |
7 |
Correct |
30 ms |
27472 KB |
Output is correct |
8 |
Correct |
99 ms |
37484 KB |
Output is correct |
9 |
Correct |
90 ms |
37576 KB |
Output is correct |
10 |
Correct |
29 ms |
27472 KB |
Output is correct |
11 |
Correct |
150 ms |
44584 KB |
Output is correct |
12 |
Correct |
136 ms |
44624 KB |
Output is correct |
13 |
Correct |
137 ms |
44368 KB |
Output is correct |
14 |
Correct |
21 ms |
27480 KB |
Output is correct |
15 |
Correct |
154 ms |
37728 KB |
Output is correct |
16 |
Correct |
136 ms |
37976 KB |
Output is correct |
17 |
Correct |
146 ms |
38224 KB |
Output is correct |
18 |
Correct |
169 ms |
38836 KB |
Output is correct |
19 |
Correct |
231 ms |
42068 KB |
Output is correct |
20 |
Correct |
179 ms |
40276 KB |
Output is correct |
21 |
Correct |
119 ms |
37868 KB |
Output is correct |
22 |
Correct |
128 ms |
37976 KB |
Output is correct |
23 |
Correct |
147 ms |
38112 KB |
Output is correct |
24 |
Correct |
168 ms |
38416 KB |
Output is correct |
25 |
Correct |
182 ms |
41336 KB |
Output is correct |
26 |
Correct |
140 ms |
36768 KB |
Output is correct |
27 |
Correct |
146 ms |
36692 KB |
Output is correct |
28 |
Correct |
27 ms |
27480 KB |
Output is correct |
29 |
Correct |
41 ms |
27676 KB |
Output is correct |
30 |
Correct |
36 ms |
27356 KB |
Output is correct |
31 |
Correct |
40 ms |
27792 KB |
Output is correct |
32 |
Correct |
41 ms |
27396 KB |
Output is correct |
33 |
Correct |
41 ms |
27220 KB |
Output is correct |
34 |
Correct |
31 ms |
27412 KB |
Output is correct |
35 |
Correct |
112 ms |
37220 KB |
Output is correct |
36 |
Correct |
78 ms |
34752 KB |
Output is correct |
37 |
Correct |
83 ms |
35016 KB |
Output is correct |
38 |
Correct |
25 ms |
27412 KB |
Output is correct |
39 |
Correct |
190 ms |
44772 KB |
Output is correct |
40 |
Correct |
175 ms |
44884 KB |
Output is correct |
41 |
Correct |
181 ms |
43884 KB |
Output is correct |
42 |
Correct |
169 ms |
43860 KB |
Output is correct |
43 |
Correct |
24 ms |
27416 KB |
Output is correct |
44 |
Correct |
192 ms |
37900 KB |
Output is correct |
45 |
Correct |
148 ms |
37896 KB |
Output is correct |
46 |
Correct |
161 ms |
38572 KB |
Output is correct |
47 |
Correct |
189 ms |
38928 KB |
Output is correct |
48 |
Correct |
255 ms |
40404 KB |
Output is correct |
49 |
Correct |
403 ms |
41200 KB |
Output is correct |
50 |
Correct |
245 ms |
40516 KB |
Output is correct |
51 |
Correct |
102 ms |
36076 KB |
Output is correct |
52 |
Correct |
84 ms |
35920 KB |
Output is correct |
53 |
Correct |
118 ms |
35372 KB |
Output is correct |
54 |
Correct |
87 ms |
36004 KB |
Output is correct |
55 |
Correct |
88 ms |
35840 KB |
Output is correct |
56 |
Correct |
162 ms |
37420 KB |
Output is correct |
57 |
Correct |
167 ms |
39112 KB |
Output is correct |
58 |
Correct |
288 ms |
37144 KB |
Output is correct |
59 |
Correct |
185 ms |
36920 KB |
Output is correct |