#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=310000;
int n,m,s,l,a1[N],a2[N],rt[N],ans[N];
char op[N];
struct point
{
int sum,lp,rp;
};
struct dgb_segment_tree
{
int n=0;
point a[(N<<2)*100];
int newp()
{
++n;
a[n].sum=a[n].lp=a[n].rp=0;
return n;
}
void pushup(int p)
{
a[p].sum=0;
if(a[p].lp)
a[p].sum+=a[a[p].lp].sum;
if(a[p].rp)
a[p].sum+=a[a[p].rp].sum;
// a[p].sum=a[a[p].lp].sum+a[a[p].rp].sum;
}
void change(int oldroot,int &p,int l,int r,int x,int d)
{
p=newp();
a[p]=a[oldroot];
if(l==r)
{
a[p].sum+=d;
// cout<<p<<" "<<l<<" "<<r<<" "<<a[p].sum<<endl;
return ;
}
int mid=(l+r)>>1;
// cout<<"P"<<p<<endl;
if(x<=mid)
change(a[oldroot].lp,a[p].lp,l,mid,x,d);
else
change(a[oldroot].rp,a[p].rp,mid+1,r,x,d);
pushup(p);
// cout<<p<<" "<<l<<" "<<r<<" "<<a[p].sum<<endl;
}
inline int merge(int x,int y,int l,int r)
{
if(!x||!y)
return x|y;
int p=newp();
if(l==r)
{
a[p].sum=a[x].sum+a[y].sum;
return p;
}
int mid=(l+r)>>1;
a[p].lp=merge(a[x].lp,a[y].lp,l,mid);
a[p].rp=merge(a[x].rp,a[y].rp,mid+1,r);
pushup(p);
return p;
}
int ask(int p,int l,int r,int askl,int askr)
{
// cout<<p<<" "<<l<<" "<<r<<" "<<a[p].sum<<endl;
if(!p)
return 0;
if(askl<=l&&r<=askr)
return a[p].sum;
int mid=(l+r)>>1,sum=0;
if(askl<=mid)
sum+=ask(a[p].lp,l,mid,askl,askr);
if(mid<askr)
sum+=ask(a[p].rp,mid+1,r,askl,askr);
return sum;
}
void clear()
{
for(int i=0;i<=n;i++)
a[i].lp=a[i].rp=a[i].sum=0;
n=0;
}
}T;
int main()
{
scanf("%d%d",&n,&m);
m+=n-1;
rt[0]=T.newp();
// cout<<T.a[rt[0]].sum<<endl;
for(int i=1;i<=n;i++)
{
T.change(rt[0],rt[i],1,n,i,1);
// cout<<T.a[rt[i]].sum<<endl;
}
for(int i=1;i<=m;i++)
{
do
{
op[i]=getchar();
}while(op[i]!='S'&&op[i]!='Q'&&op[i]!='C');
if(op[i]=='S')
{
scanf("%d%d",&a1[i],&a2[i]);
rt[a1[i]]=rt[a2[i]]=T.merge(rt[a1[i]],rt[a2[i]],1,n);
}
if(op[i]=='Q')
{
scanf("%d%d",&a1[i],&a2[i]);
ans[i]=T.ask(rt[a1[i]],1,n,a2[i],a2[i]);
}
if(op[i]=='C')
scanf("%d",&a1[i]);
}
T.clear();
for(int i=1;i<=n;i++)
{
rt[i]=T.newp();
}
for(int i=m;i>=1;i--)
if(op[i]=='S')
{
T.change(rt[a1[i]],rt[a1[i]],1,m,i,1);
rt[a1[i]]=rt[a2[i]]=T.merge(rt[a1[i]],rt[a2[i]],1,m);
}
for(int i=1;i<=m;i++)
{
if(op[i]=='Q')
{
if(ans[i])
puts("yes");
else
puts("no");
}
if(op[i]=='C')
{
printf("%d\n",T.ask(rt[a1[i]],1,m,1,i)+1);
}
}
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
servers.cpp:106:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d%d",&a1[i],&a2[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
servers.cpp:111:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d%d",&a1[i],&a2[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
servers.cpp:115:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d",&a1[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3160 KB |
Output is correct |
2 |
Correct |
23 ms |
4688 KB |
Output is correct |
3 |
Correct |
29 ms |
4944 KB |
Output is correct |
4 |
Correct |
23 ms |
4696 KB |
Output is correct |
5 |
Correct |
24 ms |
4700 KB |
Output is correct |
6 |
Correct |
33 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3160 KB |
Output is correct |
2 |
Correct |
23 ms |
4688 KB |
Output is correct |
3 |
Correct |
29 ms |
4944 KB |
Output is correct |
4 |
Correct |
23 ms |
4696 KB |
Output is correct |
5 |
Correct |
24 ms |
4700 KB |
Output is correct |
6 |
Correct |
33 ms |
4944 KB |
Output is correct |
7 |
Correct |
15 ms |
3164 KB |
Output is correct |
8 |
Correct |
25 ms |
4440 KB |
Output is correct |
9 |
Correct |
28 ms |
4648 KB |
Output is correct |
10 |
Correct |
24 ms |
4440 KB |
Output is correct |
11 |
Correct |
25 ms |
4436 KB |
Output is correct |
12 |
Correct |
29 ms |
4788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3164 KB |
Output is correct |
2 |
Correct |
200 ms |
54788 KB |
Output is correct |
3 |
Correct |
203 ms |
54664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3164 KB |
Output is correct |
2 |
Correct |
200 ms |
54788 KB |
Output is correct |
3 |
Correct |
203 ms |
54664 KB |
Output is correct |
4 |
Correct |
17 ms |
3160 KB |
Output is correct |
5 |
Correct |
194 ms |
54612 KB |
Output is correct |
6 |
Correct |
177 ms |
52816 KB |
Output is correct |
7 |
Correct |
162 ms |
53872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3164 KB |
Output is correct |
2 |
Correct |
170 ms |
55184 KB |
Output is correct |
3 |
Correct |
171 ms |
55376 KB |
Output is correct |
4 |
Correct |
131 ms |
55056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3164 KB |
Output is correct |
2 |
Correct |
170 ms |
55184 KB |
Output is correct |
3 |
Correct |
171 ms |
55376 KB |
Output is correct |
4 |
Correct |
131 ms |
55056 KB |
Output is correct |
5 |
Correct |
14 ms |
3160 KB |
Output is correct |
6 |
Correct |
169 ms |
54696 KB |
Output is correct |
7 |
Correct |
131 ms |
54868 KB |
Output is correct |
8 |
Correct |
168 ms |
54504 KB |
Output is correct |
9 |
Correct |
167 ms |
54352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3044 KB |
Output is correct |
2 |
Correct |
136 ms |
51736 KB |
Output is correct |
3 |
Correct |
157 ms |
48144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3044 KB |
Output is correct |
2 |
Correct |
136 ms |
51736 KB |
Output is correct |
3 |
Correct |
157 ms |
48144 KB |
Output is correct |
4 |
Correct |
15 ms |
3160 KB |
Output is correct |
5 |
Correct |
129 ms |
51540 KB |
Output is correct |
6 |
Correct |
158 ms |
47700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3160 KB |
Output is correct |
2 |
Correct |
161 ms |
55336 KB |
Output is correct |
3 |
Correct |
153 ms |
55356 KB |
Output is correct |
4 |
Correct |
125 ms |
55128 KB |
Output is correct |
5 |
Correct |
15 ms |
3164 KB |
Output is correct |
6 |
Correct |
139 ms |
51664 KB |
Output is correct |
7 |
Correct |
176 ms |
48216 KB |
Output is correct |
8 |
Correct |
141 ms |
40492 KB |
Output is correct |
9 |
Correct |
142 ms |
39864 KB |
Output is correct |
10 |
Correct |
179 ms |
55324 KB |
Output is correct |
11 |
Correct |
209 ms |
55120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3160 KB |
Output is correct |
2 |
Correct |
161 ms |
55336 KB |
Output is correct |
3 |
Correct |
153 ms |
55356 KB |
Output is correct |
4 |
Correct |
125 ms |
55128 KB |
Output is correct |
5 |
Correct |
15 ms |
3164 KB |
Output is correct |
6 |
Correct |
139 ms |
51664 KB |
Output is correct |
7 |
Correct |
176 ms |
48216 KB |
Output is correct |
8 |
Correct |
141 ms |
40492 KB |
Output is correct |
9 |
Correct |
142 ms |
39864 KB |
Output is correct |
10 |
Correct |
179 ms |
55324 KB |
Output is correct |
11 |
Correct |
209 ms |
55120 KB |
Output is correct |
12 |
Correct |
15 ms |
3160 KB |
Output is correct |
13 |
Correct |
159 ms |
54752 KB |
Output is correct |
14 |
Correct |
136 ms |
54832 KB |
Output is correct |
15 |
Correct |
155 ms |
54356 KB |
Output is correct |
16 |
Correct |
157 ms |
54484 KB |
Output is correct |
17 |
Correct |
15 ms |
3152 KB |
Output is correct |
18 |
Correct |
147 ms |
51360 KB |
Output is correct |
19 |
Correct |
162 ms |
47724 KB |
Output is correct |
20 |
Correct |
144 ms |
39888 KB |
Output is correct |
21 |
Correct |
137 ms |
39504 KB |
Output is correct |
22 |
Correct |
175 ms |
54608 KB |
Output is correct |
23 |
Correct |
203 ms |
53156 KB |
Output is correct |
24 |
Correct |
206 ms |
54160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3176 KB |
Output is correct |
2 |
Correct |
23 ms |
4604 KB |
Output is correct |
3 |
Correct |
30 ms |
4944 KB |
Output is correct |
4 |
Correct |
24 ms |
4696 KB |
Output is correct |
5 |
Correct |
22 ms |
4700 KB |
Output is correct |
6 |
Correct |
35 ms |
4856 KB |
Output is correct |
7 |
Correct |
15 ms |
3164 KB |
Output is correct |
8 |
Correct |
209 ms |
54768 KB |
Output is correct |
9 |
Correct |
206 ms |
54868 KB |
Output is correct |
10 |
Correct |
15 ms |
3180 KB |
Output is correct |
11 |
Correct |
174 ms |
55380 KB |
Output is correct |
12 |
Correct |
166 ms |
55384 KB |
Output is correct |
13 |
Correct |
128 ms |
55124 KB |
Output is correct |
14 |
Correct |
16 ms |
3164 KB |
Output is correct |
15 |
Correct |
138 ms |
51796 KB |
Output is correct |
16 |
Correct |
156 ms |
48036 KB |
Output is correct |
17 |
Correct |
128 ms |
40532 KB |
Output is correct |
18 |
Correct |
136 ms |
39960 KB |
Output is correct |
19 |
Correct |
178 ms |
55380 KB |
Output is correct |
20 |
Correct |
182 ms |
55264 KB |
Output is correct |
21 |
Correct |
194 ms |
55124 KB |
Output is correct |
22 |
Correct |
184 ms |
55120 KB |
Output is correct |
23 |
Correct |
173 ms |
47444 KB |
Output is correct |
24 |
Correct |
176 ms |
47440 KB |
Output is correct |
25 |
Correct |
176 ms |
45908 KB |
Output is correct |
26 |
Correct |
186 ms |
52776 KB |
Output is correct |
27 |
Correct |
190 ms |
54808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3176 KB |
Output is correct |
2 |
Correct |
23 ms |
4604 KB |
Output is correct |
3 |
Correct |
30 ms |
4944 KB |
Output is correct |
4 |
Correct |
24 ms |
4696 KB |
Output is correct |
5 |
Correct |
22 ms |
4700 KB |
Output is correct |
6 |
Correct |
35 ms |
4856 KB |
Output is correct |
7 |
Correct |
15 ms |
3164 KB |
Output is correct |
8 |
Correct |
209 ms |
54768 KB |
Output is correct |
9 |
Correct |
206 ms |
54868 KB |
Output is correct |
10 |
Correct |
15 ms |
3180 KB |
Output is correct |
11 |
Correct |
174 ms |
55380 KB |
Output is correct |
12 |
Correct |
166 ms |
55384 KB |
Output is correct |
13 |
Correct |
128 ms |
55124 KB |
Output is correct |
14 |
Correct |
16 ms |
3164 KB |
Output is correct |
15 |
Correct |
138 ms |
51796 KB |
Output is correct |
16 |
Correct |
156 ms |
48036 KB |
Output is correct |
17 |
Correct |
128 ms |
40532 KB |
Output is correct |
18 |
Correct |
136 ms |
39960 KB |
Output is correct |
19 |
Correct |
178 ms |
55380 KB |
Output is correct |
20 |
Correct |
182 ms |
55264 KB |
Output is correct |
21 |
Correct |
194 ms |
55124 KB |
Output is correct |
22 |
Correct |
184 ms |
55120 KB |
Output is correct |
23 |
Correct |
173 ms |
47444 KB |
Output is correct |
24 |
Correct |
176 ms |
47440 KB |
Output is correct |
25 |
Correct |
176 ms |
45908 KB |
Output is correct |
26 |
Correct |
186 ms |
52776 KB |
Output is correct |
27 |
Correct |
190 ms |
54808 KB |
Output is correct |
28 |
Correct |
16 ms |
3164 KB |
Output is correct |
29 |
Correct |
25 ms |
4436 KB |
Output is correct |
30 |
Correct |
33 ms |
4688 KB |
Output is correct |
31 |
Correct |
30 ms |
4432 KB |
Output is correct |
32 |
Correct |
24 ms |
4444 KB |
Output is correct |
33 |
Correct |
30 ms |
4676 KB |
Output is correct |
34 |
Correct |
17 ms |
3160 KB |
Output is correct |
35 |
Correct |
194 ms |
54528 KB |
Output is correct |
36 |
Correct |
175 ms |
52816 KB |
Output is correct |
37 |
Correct |
166 ms |
54096 KB |
Output is correct |
38 |
Correct |
15 ms |
3164 KB |
Output is correct |
39 |
Correct |
168 ms |
54780 KB |
Output is correct |
40 |
Correct |
124 ms |
54864 KB |
Output is correct |
41 |
Correct |
165 ms |
54352 KB |
Output is correct |
42 |
Correct |
160 ms |
54352 KB |
Output is correct |
43 |
Correct |
16 ms |
3200 KB |
Output is correct |
44 |
Correct |
134 ms |
51540 KB |
Output is correct |
45 |
Correct |
156 ms |
47552 KB |
Output is correct |
46 |
Correct |
133 ms |
39992 KB |
Output is correct |
47 |
Correct |
139 ms |
39504 KB |
Output is correct |
48 |
Correct |
167 ms |
54632 KB |
Output is correct |
49 |
Correct |
195 ms |
53240 KB |
Output is correct |
50 |
Correct |
203 ms |
54096 KB |
Output is correct |
51 |
Correct |
174 ms |
54328 KB |
Output is correct |
52 |
Correct |
167 ms |
54736 KB |
Output is correct |
53 |
Correct |
160 ms |
53588 KB |
Output is correct |
54 |
Correct |
164 ms |
54100 KB |
Output is correct |
55 |
Correct |
165 ms |
53840 KB |
Output is correct |
56 |
Correct |
179 ms |
47184 KB |
Output is correct |
57 |
Correct |
142 ms |
45132 KB |
Output is correct |
58 |
Correct |
163 ms |
48976 KB |
Output is correct |
59 |
Correct |
173 ms |
54864 KB |
Output is correct |