#include<bits/stdc++.h>
using namespace std;
const int maxn=300000+10;
struct yal{
int u,v,w;
int getad(int fu){
int ret=(fu^u^v);
return ret;
}
};
struct quer{
char c;
int u,v,w,res;
quer(){
res=0;
}
};
quer allq[maxn];
yal alled[maxn];
vector<int>adj[maxn];
int high[maxn],n,k,now=0,nowq=0,timea=0,dpinc[maxn],dpdec[maxn],val[maxn];
pair<int,int>stf[maxn];
int kaf=(1<<17);
struct segpaeen{
set<pair<int,int>>seg[(1<<18)];
void upd(int i,int l,int r,int tl,int tr,pair<int,int> w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
if(w.first>0){
seg[i].insert(w);
return ;
}
else{
w.first=-w.first;
seg[i].erase(w);
return ;
}
}
int m=(l+r)>>1;
upd((i<<1),l,m,tl,tr,w);
upd((i<<1)^1,m+1,r,tl,tr,w);
}
pair<int,int> pors(int i){
if(i==0){
return make_pair(0,0);
}
pair<int,int> ret=pors((i>>1));
if((int)seg[i].size()==0){
return ret;
}
ret=max(ret,(*seg[i].rbegin()));
return ret;
}
}seg1;
struct segpo{
struct node{
vector<int>v;
vector<int>fen;
int sz;
void create(){
fen.resize((int)v.size()+3);
sz=fen.size();
}
void upd(int i){
while(i<sz){
fen[i]++;
i+=(-i)&i;
}
}
int pors(int i){
int ret=0;
while(i>0){
ret+=fen[i];
i-=(-i)&i;
}
return ret;
}
};
node seg[(1<<18)];
void aval(int i,int w){
if(i==0){
return ;
}
seg[i].v.push_back(w);
return aval((i>>1),w);
}
void pre(){
for(int i=1;i<(1<<18);i++){
seg[i].v.push_back(-1);
sort(seg[i].v.begin(),seg[i].v.end());
seg[i].v.resize(unique(seg[i].v.begin(),seg[i].v.end())-seg[i].v.begin());
seg[i].create();
}
}
void upd(int i,int w){
if(i==0){
return ;
}
int p=lower_bound(seg[i].v.begin(),seg[i].v.end(),w)-seg[i].v.begin();
seg[i].upd(p);
upd((i>>1),w);
}
int pors(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr)
{
return 0;
}
if(l>=tl&&r<=tr){
int p=upper_bound(seg[i].v.begin(),seg[i].v.end(),w)-seg[i].v.begin();
int ret=seg[i].pors(p-1);
return ret;
}
int m=(l+r)>>1;
int ret=pors((i<<1),l,m,tl,tr,w)+pors((i<<1)^1,m+1,r,tl,tr,w);
return ret;
}
}seg2;
void dfs(int u,int para=0){
if(u!=1){
if(val[u]<val[para]){
dpinc[u]=dpinc[para];
dpdec[u]=para;
}
else{
dpinc[u]=para;
dpdec[u]=dpdec[para];
}
}
else{
dpinc[u]=u;
dpdec[u]=u;
high[u]=1;
}
timea++;
stf[u].first=timea;
for(auto x:adj[u]){
int v=alled[x].getad(u);
if(v!=para){
val[v]=alled[x].w;
high[v]=high[u]+1;
dfs(v,u);
}
}
stf[u].second=timea;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=0;i<n+k-1;i++){
char c;
cin>>c;
if(c=='S'){
now++;
cin>>alled[now].u>>alled[now].v;
alled[now].w=now;
nowq++;
allq[nowq].c=c;
allq[nowq].u=alled[now].u;
allq[nowq].v=alled[now].v;
allq[nowq].w=alled[now].w;
adj[alled[now].u].push_back(now);
adj[alled[now].v].push_back(now);
continue;
}
if(c=='Q'){
nowq++;
allq[nowq].c=c;
cin>>allq[nowq].u>>allq[nowq].v;
allq[nowq].w=now;
continue;
}
nowq++;
allq[nowq].c=c;
cin>>allq[nowq].u;
allq[nowq].w=now;
}
dfs(1);
for(int i=1;i<=n;i++){
seg1.upd(1,0,kaf-1,stf[i].first,stf[i].second,make_pair(high[i],i));
seg2.aval(stf[i].first+kaf,stf[dpdec[i]].first);
}
seg2.pre();
for(int i=1;i<=nowq;i++){
if(allq[i].c=='S'){
int u=allq[i].u,v=allq[i].v;
if(stf[v].first<stf[u].first){
swap(u,v);
}
seg2.upd(stf[v].first+kaf,stf[dpdec[v]].first);
seg1.upd(1,0,kaf-1,stf[v].first,stf[v].second,make_pair(-high[v],v));
continue;
}
if(allq[i].c=='Q'){
int u=allq[i].u,v=allq[i].v;
swap(u,v);
pair<int,int> lowa=seg1.pors(kaf+stf[u].first);
//cout<<u<<" "<<lowa<<" 1\n";
if(lowa.first==0||lowa.first<high[dpinc[u]]){
lowa.second=dpinc[u];
}
if((stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second&&stf[v].first>=stf[lowa.second].first)){
cout<<"yes\n";
continue;
}
if(!(stf[v].first>=stf[u].first&&stf[v].second<=stf[lowa.second].second)){
cout<<"no\n";
continue;
}
int cnt=seg2.pors(1,0,kaf-1,stf[v].first,stf[v].first,stf[u].first);
if(cnt==1||u==v){
cout<<"yes\n";
}
else{
cout<<"no\n";
}
continue;
}
int u=allq[i].u;
pair<int,int> lowa=seg1.pors(kaf+stf[u].first);
// cout<<u<<" "<<lowa.second<<" 1\n";
if(lowa.first==0||lowa.first<high[dpinc[u]]){
lowa.second=dpinc[u];
}
//cout<<u<<" "<<lowa.second<<" 2\n";
int cnt=seg2.pors(1,0,kaf-1,stf[u].first+1,stf[lowa.second].second,stf[u].first);
//if(seg2.pors(1,0,kaf-1,stf[u].first,stf[u].first,stf[u].first)==0){
cnt+=high[u]-high[lowa.second]+1;
//}
cout<<cnt<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
56908 KB |
Output is correct |
2 |
Correct |
106 ms |
59132 KB |
Output is correct |
3 |
Correct |
104 ms |
58984 KB |
Output is correct |
4 |
Correct |
115 ms |
59804 KB |
Output is correct |
5 |
Correct |
125 ms |
61072 KB |
Output is correct |
6 |
Correct |
102 ms |
58900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
56908 KB |
Output is correct |
2 |
Correct |
106 ms |
59132 KB |
Output is correct |
3 |
Correct |
104 ms |
58984 KB |
Output is correct |
4 |
Correct |
115 ms |
59804 KB |
Output is correct |
5 |
Correct |
125 ms |
61072 KB |
Output is correct |
6 |
Correct |
102 ms |
58900 KB |
Output is correct |
7 |
Correct |
80 ms |
56768 KB |
Output is correct |
8 |
Correct |
121 ms |
57844 KB |
Output is correct |
9 |
Correct |
109 ms |
57676 KB |
Output is correct |
10 |
Correct |
135 ms |
58572 KB |
Output is correct |
11 |
Correct |
139 ms |
59748 KB |
Output is correct |
12 |
Correct |
109 ms |
57744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
56956 KB |
Output is correct |
2 |
Correct |
479 ms |
85856 KB |
Output is correct |
3 |
Correct |
489 ms |
85784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
56956 KB |
Output is correct |
2 |
Correct |
479 ms |
85856 KB |
Output is correct |
3 |
Correct |
489 ms |
85784 KB |
Output is correct |
4 |
Correct |
90 ms |
56772 KB |
Output is correct |
5 |
Correct |
470 ms |
84528 KB |
Output is correct |
6 |
Correct |
463 ms |
83616 KB |
Output is correct |
7 |
Correct |
482 ms |
83228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
56912 KB |
Output is correct |
2 |
Correct |
1606 ms |
171360 KB |
Output is correct |
3 |
Correct |
1631 ms |
171204 KB |
Output is correct |
4 |
Correct |
742 ms |
168160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
56912 KB |
Output is correct |
2 |
Correct |
1606 ms |
171360 KB |
Output is correct |
3 |
Correct |
1631 ms |
171204 KB |
Output is correct |
4 |
Correct |
742 ms |
168160 KB |
Output is correct |
5 |
Correct |
84 ms |
56780 KB |
Output is correct |
6 |
Correct |
1600 ms |
169488 KB |
Output is correct |
7 |
Correct |
930 ms |
171948 KB |
Output is correct |
8 |
Correct |
1707 ms |
168548 KB |
Output is correct |
9 |
Correct |
1688 ms |
168636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
56908 KB |
Output is correct |
2 |
Correct |
449 ms |
93256 KB |
Output is correct |
3 |
Correct |
803 ms |
93764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
56908 KB |
Output is correct |
2 |
Correct |
449 ms |
93256 KB |
Output is correct |
3 |
Correct |
803 ms |
93764 KB |
Output is correct |
4 |
Correct |
81 ms |
56820 KB |
Output is correct |
5 |
Correct |
482 ms |
91064 KB |
Output is correct |
6 |
Correct |
846 ms |
93212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
56940 KB |
Output is correct |
2 |
Correct |
1603 ms |
171496 KB |
Output is correct |
3 |
Correct |
1571 ms |
171512 KB |
Output is correct |
4 |
Correct |
718 ms |
168088 KB |
Output is correct |
5 |
Correct |
91 ms |
57596 KB |
Output is correct |
6 |
Correct |
453 ms |
93180 KB |
Output is correct |
7 |
Correct |
819 ms |
93760 KB |
Output is correct |
8 |
Correct |
861 ms |
92864 KB |
Output is correct |
9 |
Correct |
900 ms |
92852 KB |
Output is correct |
10 |
Correct |
1119 ms |
131060 KB |
Output is correct |
11 |
Correct |
1075 ms |
129796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
56940 KB |
Output is correct |
2 |
Correct |
1603 ms |
171496 KB |
Output is correct |
3 |
Correct |
1571 ms |
171512 KB |
Output is correct |
4 |
Correct |
718 ms |
168088 KB |
Output is correct |
5 |
Correct |
91 ms |
57596 KB |
Output is correct |
6 |
Correct |
453 ms |
93180 KB |
Output is correct |
7 |
Correct |
819 ms |
93760 KB |
Output is correct |
8 |
Correct |
861 ms |
92864 KB |
Output is correct |
9 |
Correct |
900 ms |
92852 KB |
Output is correct |
10 |
Correct |
1119 ms |
131060 KB |
Output is correct |
11 |
Correct |
1075 ms |
129796 KB |
Output is correct |
12 |
Correct |
80 ms |
56652 KB |
Output is correct |
13 |
Correct |
1684 ms |
169892 KB |
Output is correct |
14 |
Correct |
951 ms |
172748 KB |
Output is correct |
15 |
Correct |
1646 ms |
169744 KB |
Output is correct |
16 |
Correct |
1709 ms |
169648 KB |
Output is correct |
17 |
Correct |
80 ms |
56716 KB |
Output is correct |
18 |
Correct |
494 ms |
91936 KB |
Output is correct |
19 |
Correct |
870 ms |
93128 KB |
Output is correct |
20 |
Correct |
932 ms |
91300 KB |
Output is correct |
21 |
Correct |
923 ms |
91628 KB |
Output is correct |
22 |
Correct |
1168 ms |
125696 KB |
Output is correct |
23 |
Correct |
1109 ms |
130312 KB |
Output is correct |
24 |
Correct |
1103 ms |
132344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
56872 KB |
Output is correct |
2 |
Correct |
111 ms |
59136 KB |
Output is correct |
3 |
Correct |
103 ms |
58988 KB |
Output is correct |
4 |
Correct |
118 ms |
59852 KB |
Output is correct |
5 |
Correct |
122 ms |
61048 KB |
Output is correct |
6 |
Correct |
107 ms |
58976 KB |
Output is correct |
7 |
Correct |
92 ms |
57700 KB |
Output is correct |
8 |
Correct |
470 ms |
85780 KB |
Output is correct |
9 |
Correct |
487 ms |
85828 KB |
Output is correct |
10 |
Correct |
88 ms |
57572 KB |
Output is correct |
11 |
Correct |
1566 ms |
171364 KB |
Output is correct |
12 |
Correct |
1586 ms |
171240 KB |
Output is correct |
13 |
Correct |
725 ms |
168036 KB |
Output is correct |
14 |
Correct |
86 ms |
57592 KB |
Output is correct |
15 |
Correct |
471 ms |
93236 KB |
Output is correct |
16 |
Correct |
828 ms |
93676 KB |
Output is correct |
17 |
Correct |
858 ms |
93044 KB |
Output is correct |
18 |
Correct |
896 ms |
92956 KB |
Output is correct |
19 |
Correct |
1151 ms |
130928 KB |
Output is correct |
20 |
Correct |
1160 ms |
129776 KB |
Output is correct |
21 |
Correct |
462 ms |
86464 KB |
Output is correct |
22 |
Correct |
464 ms |
86376 KB |
Output is correct |
23 |
Correct |
575 ms |
86572 KB |
Output is correct |
24 |
Correct |
586 ms |
86720 KB |
Output is correct |
25 |
Correct |
1215 ms |
128016 KB |
Output is correct |
26 |
Correct |
831 ms |
94136 KB |
Output is correct |
27 |
Correct |
825 ms |
94056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
56872 KB |
Output is correct |
2 |
Correct |
111 ms |
59136 KB |
Output is correct |
3 |
Correct |
103 ms |
58988 KB |
Output is correct |
4 |
Correct |
118 ms |
59852 KB |
Output is correct |
5 |
Correct |
122 ms |
61048 KB |
Output is correct |
6 |
Correct |
107 ms |
58976 KB |
Output is correct |
7 |
Correct |
92 ms |
57700 KB |
Output is correct |
8 |
Correct |
470 ms |
85780 KB |
Output is correct |
9 |
Correct |
487 ms |
85828 KB |
Output is correct |
10 |
Correct |
88 ms |
57572 KB |
Output is correct |
11 |
Correct |
1566 ms |
171364 KB |
Output is correct |
12 |
Correct |
1586 ms |
171240 KB |
Output is correct |
13 |
Correct |
725 ms |
168036 KB |
Output is correct |
14 |
Correct |
86 ms |
57592 KB |
Output is correct |
15 |
Correct |
471 ms |
93236 KB |
Output is correct |
16 |
Correct |
828 ms |
93676 KB |
Output is correct |
17 |
Correct |
858 ms |
93044 KB |
Output is correct |
18 |
Correct |
896 ms |
92956 KB |
Output is correct |
19 |
Correct |
1151 ms |
130928 KB |
Output is correct |
20 |
Correct |
1160 ms |
129776 KB |
Output is correct |
21 |
Correct |
462 ms |
86464 KB |
Output is correct |
22 |
Correct |
464 ms |
86376 KB |
Output is correct |
23 |
Correct |
575 ms |
86572 KB |
Output is correct |
24 |
Correct |
586 ms |
86720 KB |
Output is correct |
25 |
Correct |
1215 ms |
128016 KB |
Output is correct |
26 |
Correct |
831 ms |
94136 KB |
Output is correct |
27 |
Correct |
825 ms |
94056 KB |
Output is correct |
28 |
Correct |
85 ms |
56700 KB |
Output is correct |
29 |
Correct |
116 ms |
57836 KB |
Output is correct |
30 |
Correct |
109 ms |
57736 KB |
Output is correct |
31 |
Correct |
145 ms |
58564 KB |
Output is correct |
32 |
Correct |
139 ms |
59732 KB |
Output is correct |
33 |
Correct |
110 ms |
58012 KB |
Output is correct |
34 |
Correct |
89 ms |
56708 KB |
Output is correct |
35 |
Correct |
458 ms |
84624 KB |
Output is correct |
36 |
Correct |
459 ms |
83912 KB |
Output is correct |
37 |
Correct |
450 ms |
84020 KB |
Output is correct |
38 |
Correct |
84 ms |
56724 KB |
Output is correct |
39 |
Correct |
1631 ms |
169896 KB |
Output is correct |
40 |
Correct |
985 ms |
172828 KB |
Output is correct |
41 |
Correct |
1759 ms |
169588 KB |
Output is correct |
42 |
Correct |
1847 ms |
169420 KB |
Output is correct |
43 |
Correct |
84 ms |
56776 KB |
Output is correct |
44 |
Correct |
515 ms |
92264 KB |
Output is correct |
45 |
Correct |
940 ms |
93076 KB |
Output is correct |
46 |
Correct |
923 ms |
91384 KB |
Output is correct |
47 |
Correct |
960 ms |
91556 KB |
Output is correct |
48 |
Correct |
1229 ms |
125832 KB |
Output is correct |
49 |
Correct |
1090 ms |
130272 KB |
Output is correct |
50 |
Correct |
1209 ms |
132400 KB |
Output is correct |
51 |
Correct |
481 ms |
84932 KB |
Output is correct |
52 |
Correct |
472 ms |
84860 KB |
Output is correct |
53 |
Correct |
425 ms |
84332 KB |
Output is correct |
54 |
Correct |
477 ms |
85300 KB |
Output is correct |
55 |
Correct |
473 ms |
84916 KB |
Output is correct |
56 |
Correct |
600 ms |
85364 KB |
Output is correct |
57 |
Correct |
1276 ms |
128700 KB |
Output is correct |
58 |
Correct |
1056 ms |
92532 KB |
Output is correct |
59 |
Correct |
892 ms |
93784 KB |
Output is correct |