/*
"care a facut teste cu Lattice reduction attack e ciudat"
"linistiti va putin"
- 2023 -
*/
#include<bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
#define int long long
using namespace std;
struct operatie
{
char type;
int u;
int v;
int moment;
int ans;
};
struct idk
{
int type;///0 - update, 1 - query
int l;
int r;
int who;
};
bool cmp(idk a, idk b)
{
if (a.l!=b.l)
return a.l>b.l;
else
return a.type<b.type;
}
struct edge
{
int to;
int w;
};
vector<idk> events;
operatie q[240005];
vector<operatie> qq[120005];
map<int,pair<int,int>> cresc;
map<int,pair<int,int>> desc;
int aib[240005];
vector<edge> nodes[120005];
bool blocked[120005];
int sz[120005];
int maxx;
void update(int idx, int delta)
{
while (idx<=maxx)
{
aib[idx]+=delta;
idx+=idx&(-idx);
}
}
int query(int idx)
{
int ans;
ans=0;
while (idx>0)
{
ans+=aib[idx];
idx-=idx&(-idx);
}
return ans;
}
void recalc(int node, int parent)
{
sz[node]=1;
for (auto x : nodes[node])
{
if (x.to!=parent && !blocked[x.to])
{
recalc(x.to,node);
sz[node]+=sz[x.to];
}
}
}
int find_centroid(int node, int parent, int compsize)
{
for (auto x : nodes[node])
{
if (x.to!=parent && !blocked[x.to] && sz[x.to]>compsize/2)
{
return find_centroid(x.to,node,compsize);
}
}
return node;
}
bool canadd;
void add_cresc(int node, int parent, int first, int last)
{
cresc[node]= {first,last};
if (canadd)
events.push_back({0,first,last,0});
for (auto x : nodes[node])
{
if (x.to!=parent && !blocked[x.to] && x.w>last)
{
add_cresc(x.to,node,first,x.w);
}
}
}
void rem_cresc(int node, int parent, int first, int last)
{
cresc[node]= {-1,-1};
for (auto x : nodes[node])
{
if (x.to!=parent && !blocked[x.to] && x.w>last)
{
rem_cresc(x.to,node,first,x.w);
}
}
}
int globc;
void solve(int node, int parent, int first, int last)
{
desc[node]={first,last};
for (auto x : qq[node])
{
if (x.type=='Q')
{
if (x.u==x.v)
q[x.moment].ans|=1;
else if (x.u==globc && first<x.moment)
q[x.moment].ans|=1;
else if (cresc.find(x.u)==cresc.end() || (cresc[x.u].first==-1 && cresc[x.u].second==-1))
q[x.moment].ans|=0;
else if (cresc[x.u].first>first && cresc[x.u].second<x.moment)
q[x.moment].ans|=1;
}
else
{
events.push_back({1,first+1,x.moment-1,x.moment});
}
}
for (auto x : nodes[node])
{
if (x.to!=parent && !blocked[x.to] && x.w<last)
{
solve(x.to,node,first,x.w);
}
}
}
void cd(int node)
{
int centroid;
cresc.clear();
desc.clear();
events.clear();
recalc(node,-1);
centroid=find_centroid(node,-1,sz[node]);
globc=centroid;
blocked[centroid]=1;
canadd=1;
for (auto x : nodes[centroid])
{
if (!blocked[x.to])
{
add_cresc(x.to,centroid,x.w,x.w);
}
}
for (auto x : qq[centroid])
{
if (x.type=='Q')
{
if (x.u==x.v)
q[x.moment].ans|=1;
else if (cresc.find(x.u)==cresc.end() || (cresc[x.u].first==-1 && cresc[x.u].second==-1))
q[x.moment].ans|=0;
else if (cresc[x.u].second<x.moment)
q[x.moment].ans|=1;
}
else
{
events.push_back({1,1,x.moment-1,x.moment});
}
}
canadd=0;
for (auto x : nodes[centroid])
{
if (!blocked[x.to])
{
rem_cresc(x.to,centroid,x.w,x.w);
solve(x.to,centroid,x.w,x.w);
add_cresc(x.to,centroid,x.w,x.w);
}
}
sort(events.begin(),events.end(),cmp);
for (auto x : events)
{
if (x.type==0)
{
update(x.r,1);
}
else
{
if (desc.find(q[x.who].u)==desc.end())
q[x.who].ans+=0;
else if (desc[q[x.who].u].first<=x.r)
q[x.who].ans+=1;
q[x.who].ans+=query(x.r);
}
}
for (auto x : events)
{
if (x.type==0)
{
update(x.r,-1);
}
}
for (auto x : nodes[centroid])
{
if (!blocked[x.to])
{
cd(x.to);
}
}
}
signed main()
{
/*
ifstream fin("secvp.in");
ofstream fout("secvp.out");
*/
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k,i,j;
cin >> n >> k;
for (i=1; i<=n+k-1; i++)
{
cin >> q[i].type;
q[i].moment=i;
if (q[i].type=='S')
{
cin >> q[i].u >> q[i].v;
nodes[q[i].u].push_back({q[i].v,i});
nodes[q[i].v].push_back({q[i].u,i});
}
if (q[i].type=='Q')
{
cin >> q[i].u >> q[i].v;
qq[q[i].v].push_back(q[i]);
}
if (q[i].type=='C')
{
cin >> q[i].u;
q[i].ans=1;
qq[q[i].u].push_back(q[i]);
}
}
maxx=n+k-1;
cd(1);
for (i=1; i<=n+k-1; i++)
{
if (q[i].type=='Q')
{
if (q[i].ans==1)
cout << "yes\n";
else
cout << "no\n";
}
else if (q[i].type=='C')
{
cout << q[i].ans << "\n";
}
}
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:247:15: warning: unused variable 'j' [-Wunused-variable]
247 | int n,k,i,j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21996 KB |
Output is correct |
2 |
Correct |
41 ms |
23112 KB |
Output is correct |
3 |
Correct |
47 ms |
22080 KB |
Output is correct |
4 |
Correct |
48 ms |
23112 KB |
Output is correct |
5 |
Correct |
37 ms |
24656 KB |
Output is correct |
6 |
Correct |
67 ms |
24468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21996 KB |
Output is correct |
2 |
Correct |
41 ms |
23112 KB |
Output is correct |
3 |
Correct |
47 ms |
22080 KB |
Output is correct |
4 |
Correct |
48 ms |
23112 KB |
Output is correct |
5 |
Correct |
37 ms |
24656 KB |
Output is correct |
6 |
Correct |
67 ms |
24468 KB |
Output is correct |
7 |
Correct |
34 ms |
22820 KB |
Output is correct |
8 |
Correct |
52 ms |
24372 KB |
Output is correct |
9 |
Correct |
59 ms |
23552 KB |
Output is correct |
10 |
Correct |
53 ms |
24392 KB |
Output is correct |
11 |
Correct |
44 ms |
24396 KB |
Output is correct |
12 |
Correct |
66 ms |
25508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
21996 KB |
Output is correct |
2 |
Correct |
350 ms |
52400 KB |
Output is correct |
3 |
Correct |
351 ms |
49716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
21996 KB |
Output is correct |
2 |
Correct |
350 ms |
52400 KB |
Output is correct |
3 |
Correct |
351 ms |
49716 KB |
Output is correct |
4 |
Correct |
25 ms |
22924 KB |
Output is correct |
5 |
Correct |
425 ms |
53912 KB |
Output is correct |
6 |
Correct |
262 ms |
54264 KB |
Output is correct |
7 |
Correct |
268 ms |
52720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
21996 KB |
Output is correct |
2 |
Correct |
214 ms |
34412 KB |
Output is correct |
3 |
Correct |
192 ms |
37236 KB |
Output is correct |
4 |
Correct |
424 ms |
51200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
21996 KB |
Output is correct |
2 |
Correct |
214 ms |
34412 KB |
Output is correct |
3 |
Correct |
192 ms |
37236 KB |
Output is correct |
4 |
Correct |
424 ms |
51200 KB |
Output is correct |
5 |
Correct |
21 ms |
22008 KB |
Output is correct |
6 |
Correct |
218 ms |
37268 KB |
Output is correct |
7 |
Correct |
477 ms |
55216 KB |
Output is correct |
8 |
Correct |
213 ms |
36836 KB |
Output is correct |
9 |
Correct |
203 ms |
36936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21996 KB |
Output is correct |
2 |
Correct |
405 ms |
40888 KB |
Output is correct |
3 |
Correct |
202 ms |
31816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21996 KB |
Output is correct |
2 |
Correct |
405 ms |
40888 KB |
Output is correct |
3 |
Correct |
202 ms |
31816 KB |
Output is correct |
4 |
Correct |
32 ms |
22968 KB |
Output is correct |
5 |
Correct |
573 ms |
45684 KB |
Output is correct |
6 |
Correct |
219 ms |
34632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21996 KB |
Output is correct |
2 |
Correct |
222 ms |
34396 KB |
Output is correct |
3 |
Correct |
263 ms |
34572 KB |
Output is correct |
4 |
Correct |
452 ms |
53948 KB |
Output is correct |
5 |
Correct |
21 ms |
21996 KB |
Output is correct |
6 |
Correct |
466 ms |
43596 KB |
Output is correct |
7 |
Correct |
193 ms |
31816 KB |
Output is correct |
8 |
Correct |
218 ms |
33080 KB |
Output is correct |
9 |
Correct |
212 ms |
32328 KB |
Output is correct |
10 |
Correct |
596 ms |
43456 KB |
Output is correct |
11 |
Correct |
530 ms |
43044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21996 KB |
Output is correct |
2 |
Correct |
222 ms |
34396 KB |
Output is correct |
3 |
Correct |
263 ms |
34572 KB |
Output is correct |
4 |
Correct |
452 ms |
53948 KB |
Output is correct |
5 |
Correct |
21 ms |
21996 KB |
Output is correct |
6 |
Correct |
466 ms |
43596 KB |
Output is correct |
7 |
Correct |
193 ms |
31816 KB |
Output is correct |
8 |
Correct |
218 ms |
33080 KB |
Output is correct |
9 |
Correct |
212 ms |
32328 KB |
Output is correct |
10 |
Correct |
596 ms |
43456 KB |
Output is correct |
11 |
Correct |
530 ms |
43044 KB |
Output is correct |
12 |
Correct |
22 ms |
22832 KB |
Output is correct |
13 |
Correct |
197 ms |
37344 KB |
Output is correct |
14 |
Correct |
472 ms |
55112 KB |
Output is correct |
15 |
Correct |
230 ms |
36936 KB |
Output is correct |
16 |
Correct |
205 ms |
36884 KB |
Output is correct |
17 |
Correct |
22 ms |
22824 KB |
Output is correct |
18 |
Correct |
590 ms |
45744 KB |
Output is correct |
19 |
Correct |
231 ms |
34632 KB |
Output is correct |
20 |
Correct |
247 ms |
34888 KB |
Output is correct |
21 |
Correct |
274 ms |
35400 KB |
Output is correct |
22 |
Correct |
779 ms |
47612 KB |
Output is correct |
23 |
Correct |
845 ms |
45488 KB |
Output is correct |
24 |
Correct |
780 ms |
47544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21996 KB |
Output is correct |
2 |
Correct |
36 ms |
24648 KB |
Output is correct |
3 |
Correct |
43 ms |
23364 KB |
Output is correct |
4 |
Correct |
34 ms |
23112 KB |
Output is correct |
5 |
Correct |
33 ms |
23316 KB |
Output is correct |
6 |
Correct |
62 ms |
23256 KB |
Output is correct |
7 |
Correct |
23 ms |
22956 KB |
Output is correct |
8 |
Correct |
325 ms |
49776 KB |
Output is correct |
9 |
Correct |
318 ms |
52276 KB |
Output is correct |
10 |
Correct |
21 ms |
23020 KB |
Output is correct |
11 |
Correct |
209 ms |
37196 KB |
Output is correct |
12 |
Correct |
220 ms |
35988 KB |
Output is correct |
13 |
Correct |
452 ms |
51128 KB |
Output is correct |
14 |
Correct |
26 ms |
22924 KB |
Output is correct |
15 |
Correct |
497 ms |
43704 KB |
Output is correct |
16 |
Correct |
211 ms |
31828 KB |
Output is correct |
17 |
Correct |
206 ms |
32072 KB |
Output is correct |
18 |
Correct |
234 ms |
32400 KB |
Output is correct |
19 |
Correct |
600 ms |
46204 KB |
Output is correct |
20 |
Correct |
631 ms |
45860 KB |
Output is correct |
21 |
Correct |
440 ms |
48936 KB |
Output is correct |
22 |
Correct |
409 ms |
45472 KB |
Output is correct |
23 |
Correct |
252 ms |
34528 KB |
Output is correct |
24 |
Correct |
226 ms |
31820 KB |
Output is correct |
25 |
Correct |
302 ms |
42304 KB |
Output is correct |
26 |
Correct |
301 ms |
35920 KB |
Output is correct |
27 |
Correct |
378 ms |
37960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
21996 KB |
Output is correct |
2 |
Correct |
36 ms |
24648 KB |
Output is correct |
3 |
Correct |
43 ms |
23364 KB |
Output is correct |
4 |
Correct |
34 ms |
23112 KB |
Output is correct |
5 |
Correct |
33 ms |
23316 KB |
Output is correct |
6 |
Correct |
62 ms |
23256 KB |
Output is correct |
7 |
Correct |
23 ms |
22956 KB |
Output is correct |
8 |
Correct |
325 ms |
49776 KB |
Output is correct |
9 |
Correct |
318 ms |
52276 KB |
Output is correct |
10 |
Correct |
21 ms |
23020 KB |
Output is correct |
11 |
Correct |
209 ms |
37196 KB |
Output is correct |
12 |
Correct |
220 ms |
35988 KB |
Output is correct |
13 |
Correct |
452 ms |
51128 KB |
Output is correct |
14 |
Correct |
26 ms |
22924 KB |
Output is correct |
15 |
Correct |
497 ms |
43704 KB |
Output is correct |
16 |
Correct |
211 ms |
31828 KB |
Output is correct |
17 |
Correct |
206 ms |
32072 KB |
Output is correct |
18 |
Correct |
234 ms |
32400 KB |
Output is correct |
19 |
Correct |
600 ms |
46204 KB |
Output is correct |
20 |
Correct |
631 ms |
45860 KB |
Output is correct |
21 |
Correct |
440 ms |
48936 KB |
Output is correct |
22 |
Correct |
409 ms |
45472 KB |
Output is correct |
23 |
Correct |
252 ms |
34528 KB |
Output is correct |
24 |
Correct |
226 ms |
31820 KB |
Output is correct |
25 |
Correct |
302 ms |
42304 KB |
Output is correct |
26 |
Correct |
301 ms |
35920 KB |
Output is correct |
27 |
Correct |
378 ms |
37960 KB |
Output is correct |
28 |
Correct |
22 ms |
21984 KB |
Output is correct |
29 |
Correct |
44 ms |
24444 KB |
Output is correct |
30 |
Correct |
48 ms |
23552 KB |
Output is correct |
31 |
Correct |
42 ms |
24392 KB |
Output is correct |
32 |
Correct |
39 ms |
24392 KB |
Output is correct |
33 |
Correct |
69 ms |
25508 KB |
Output is correct |
34 |
Correct |
23 ms |
22944 KB |
Output is correct |
35 |
Correct |
338 ms |
54396 KB |
Output is correct |
36 |
Correct |
251 ms |
54460 KB |
Output is correct |
37 |
Correct |
321 ms |
54684 KB |
Output is correct |
38 |
Correct |
22 ms |
22832 KB |
Output is correct |
39 |
Correct |
218 ms |
37192 KB |
Output is correct |
40 |
Correct |
553 ms |
55208 KB |
Output is correct |
41 |
Correct |
298 ms |
36948 KB |
Output is correct |
42 |
Correct |
270 ms |
37184 KB |
Output is correct |
43 |
Correct |
32 ms |
20484 KB |
Output is correct |
44 |
Correct |
595 ms |
45744 KB |
Output is correct |
45 |
Correct |
241 ms |
34564 KB |
Output is correct |
46 |
Correct |
209 ms |
34888 KB |
Output is correct |
47 |
Correct |
220 ms |
35312 KB |
Output is correct |
48 |
Correct |
704 ms |
47540 KB |
Output is correct |
49 |
Correct |
751 ms |
45588 KB |
Output is correct |
50 |
Correct |
666 ms |
47712 KB |
Output is correct |
51 |
Correct |
308 ms |
51660 KB |
Output is correct |
52 |
Correct |
256 ms |
56864 KB |
Output is correct |
53 |
Correct |
289 ms |
55100 KB |
Output is correct |
54 |
Correct |
279 ms |
55508 KB |
Output is correct |
55 |
Correct |
266 ms |
51156 KB |
Output is correct |
56 |
Correct |
228 ms |
35344 KB |
Output is correct |
57 |
Correct |
319 ms |
45348 KB |
Output is correct |
58 |
Correct |
453 ms |
39100 KB |
Output is correct |
59 |
Correct |
426 ms |
41920 KB |
Output is correct |