#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define piii tuple<int, int, int>
#define F first
#define S second
using namespace std;
int N, K, pre[120005][20], vis[120005], sz[120005], head[120005][20], ts;
pii io[120005], bin[120005][20], e[120005];
piii Q[120005];
vector<piii> update[120005];
vector<pii> E[120005];
struct BIT
{
vector<int> bit;
vector<int> vals;
int id(int i, int p = 0)
{
return max(0, (int)(lower_bound(vals.begin(), vals.end(), i, greater<>()) - vals.begin()) + p);
}
void init(vector<int> val)
{
val.emplace_back(N + 8);
val.emplace_back(N);
val.emplace_back(-7);
val.emplace_back(-8);
sort(val.begin(), val.end(), greater<>());
vals = val;
bit.resize(vals.size(), 0);
}
int query(int k, int p)
{
int ans = 0;
for (int i = id(k, p); i; i -= (i & -i))
ans += bit[i];
return ans;
}
void modify(int k)
{
for (int i = id(k); i < (int)bit.size(); i += (i & -i))
bit[i]++;
}
} bit[120005];
void calcsz(int u)
{
vis[u] = sz[u] = 1;
for (auto [v, w] : E[u])
if (!vis[v])
{
calcsz(v);
sz[u] += sz[v];
}
vis[u] = 0;
}
void init(int u, int p, int t, int c, int l)
{
head[u][l] = c;
if (p < N)
update[p].emplace_back(piii(c, l, t));
vis[u] = 1;
for (auto [v, w] : E[u])
if (!vis[v])
init(v, (p < w ? w : N), t, c, l);
vis[u] = 0;
}
void cdec(int r, int l)
{
int c = r;
calcsz(r);
{
int u = r;
do
{
c = u;
for (auto [v, _] : E[u])
if (sz[u] > sz[v] && sz[v] > sz[r] / 2)
{
u = v;
break;
}
} while (u != c);
}
vector<int> v;
head[c][l] = c;
vis[c] = 1;
for (auto [u, w] : E[c])
if (!vis[u])
{
v.emplace_back(w);
init(u, w, w, c, l);
}
bit[c].init(v);
bit[c].modify(N);
for (auto [u, w] : E[c])
if (!vis[u])
cdec(u, l + 1);
}
const pii no = {-8, -8},
bad = {-2, -2};
pii merge(pii p, pii q)
{
if (p == no)
return q;
if (q == no)
return p;
if (p == bad || q == bad)
return bad;
if (p.F <= p.S && p.S <= q.F && q.F <= q.S)
return pii(p.F, q.S);
if (p.F >= p.S && p.S >= q.F && q.F >= q.S)
return pii(p.F, q.S);
return bad;
}
void dfs(int u)
{
io[u].F = ++ts;
for (auto [v, w] : E[u])
if (v != pre[u][0])
{
pre[v][0] = u;
bin[v][0] = pii(w, w);
for (int p = 0; p + 1 < 20; p++)
pre[v][p + 1] = pre[pre[v][p]][p],
bin[v][p + 1] = merge(bin[v][p], bin[pre[v][p]][p]);
dfs(v);
}
io[u].S = ts;
}
bool ischild(int r, int c)
{
return io[r].F <= io[c].F && io[c].S <= io[r].S;
}
pii getpath(int u, int v)
{
pii l = no, r = no;
for (int p = 19; p >= 0; p--)
if (!ischild(pre[u][p], v))
l = merge(l, bin[u][p]), u = pre[u][p];
if (!ischild(u, v))
l = merge(l, bin[u][0]), u = pre[u][0];
for (int p = 19; p >= 0; p--)
if (!ischild(pre[v][p], u))
r = merge(r, bin[v][p]), v = pre[v][p];
if (!ischild(v, u))
r = merge(r, bin[v][0]), v = pre[v][0];
swap(r.F, r.S);
return merge(l, r);
}
signed main()
{
cin >> N >> K;
for (int i = 0, j = 0; i + j < K + N - 1;)
{
char c;
int a, b = 0;
cin >> c >> a;
if (c != 'C')
cin >> b;
if (c == 'S')
{
j++;
e[j] = pii(a, b);
E[a].emplace_back(pii(b, j));
E[b].emplace_back(pii(a, j));
}
else
{
i++;
Q[i] = piii(j, a, b);
}
}
for (int p = 0; p < 20; p++)
pre[1][p] = 1, bin[1][p] = no;
dfs(1);
cdec(1, 1);
for (int i = 1, j = 0; i <= K; i++)
{
auto [t, a, b] = Q[i];
while (j < t)
{
j++;
for (auto [c, l, w] : update[j])
{
// cerr << "update " << j << ' ' << c << ' ' << w << '\n';
bit[c].modify(w);
}
}
if (b > 0)
{
int u = a, v = b;
pii l = no, r = no;
// cerr << merge(l, r).F << ' ' << merge(l, r).S << '\n';
l = merge(merge(pii(t, t), getpath(a, b)), pii(0, 0));
cout << (l != bad ? "yes\n" : "no\n");
}
else
{
int ans = 0;
for (int p = 1; head[a][p]; p++)
{
int c = head[a][p];
pii path = getpath(a, c);
// cerr << a << ' ' << c << ' ' << path.F << ' ' << path.S << '\n';
if (path == bad || path.F > path.S || path.S > t)
continue;
int k = path.S;
// cerr << '+' << bit[c].query(k, -1) << '\n';
ans += bit[c].query(k, -1);
}
cout << ans << '\n';
}
}
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:205:8: warning: unused variable 'u' [-Wunused-variable]
205 | int u = a, v = b;
| ^
servers.cpp:205:15: warning: unused variable 'v' [-Wunused-variable]
205 | int u = a, v = b;
| ^
servers.cpp:206:16: warning: variable 'r' set but not used [-Wunused-but-set-variable]
206 | pii l = no, r = no;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
13328 KB |
Output is correct |
2 |
Correct |
77 ms |
15248 KB |
Output is correct |
3 |
Correct |
70 ms |
15300 KB |
Output is correct |
4 |
Correct |
91 ms |
15308 KB |
Output is correct |
5 |
Correct |
82 ms |
15448 KB |
Output is correct |
6 |
Correct |
69 ms |
15264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
13328 KB |
Output is correct |
2 |
Correct |
77 ms |
15248 KB |
Output is correct |
3 |
Correct |
70 ms |
15300 KB |
Output is correct |
4 |
Correct |
91 ms |
15308 KB |
Output is correct |
5 |
Correct |
82 ms |
15448 KB |
Output is correct |
6 |
Correct |
69 ms |
15264 KB |
Output is correct |
7 |
Correct |
56 ms |
13392 KB |
Output is correct |
8 |
Correct |
102 ms |
16300 KB |
Output is correct |
9 |
Correct |
75 ms |
16468 KB |
Output is correct |
10 |
Correct |
146 ms |
16400 KB |
Output is correct |
11 |
Correct |
146 ms |
16564 KB |
Output is correct |
12 |
Correct |
67 ms |
16444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
13396 KB |
Output is correct |
2 |
Correct |
290 ms |
71240 KB |
Output is correct |
3 |
Correct |
274 ms |
71080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
13396 KB |
Output is correct |
2 |
Correct |
290 ms |
71240 KB |
Output is correct |
3 |
Correct |
274 ms |
71080 KB |
Output is correct |
4 |
Correct |
53 ms |
13404 KB |
Output is correct |
5 |
Correct |
277 ms |
71116 KB |
Output is correct |
6 |
Correct |
222 ms |
71356 KB |
Output is correct |
7 |
Correct |
235 ms |
71244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
13324 KB |
Output is correct |
2 |
Correct |
388 ms |
78468 KB |
Output is correct |
3 |
Correct |
394 ms |
78588 KB |
Output is correct |
4 |
Correct |
335 ms |
91600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
13324 KB |
Output is correct |
2 |
Correct |
388 ms |
78468 KB |
Output is correct |
3 |
Correct |
394 ms |
78588 KB |
Output is correct |
4 |
Correct |
335 ms |
91600 KB |
Output is correct |
5 |
Correct |
57 ms |
13320 KB |
Output is correct |
6 |
Correct |
632 ms |
81348 KB |
Output is correct |
7 |
Correct |
620 ms |
93748 KB |
Output is correct |
8 |
Correct |
866 ms |
81008 KB |
Output is correct |
9 |
Correct |
872 ms |
80844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
13388 KB |
Output is correct |
2 |
Correct |
300 ms |
78988 KB |
Output is correct |
3 |
Correct |
329 ms |
70416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
13388 KB |
Output is correct |
2 |
Correct |
300 ms |
78988 KB |
Output is correct |
3 |
Correct |
329 ms |
70416 KB |
Output is correct |
4 |
Correct |
58 ms |
13384 KB |
Output is correct |
5 |
Correct |
420 ms |
82016 KB |
Output is correct |
6 |
Correct |
444 ms |
73304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
13372 KB |
Output is correct |
2 |
Correct |
381 ms |
78428 KB |
Output is correct |
3 |
Correct |
385 ms |
78492 KB |
Output is correct |
4 |
Correct |
334 ms |
91640 KB |
Output is correct |
5 |
Correct |
55 ms |
13384 KB |
Output is correct |
6 |
Correct |
298 ms |
79036 KB |
Output is correct |
7 |
Correct |
324 ms |
70436 KB |
Output is correct |
8 |
Correct |
385 ms |
71892 KB |
Output is correct |
9 |
Correct |
422 ms |
71884 KB |
Output is correct |
10 |
Correct |
453 ms |
79468 KB |
Output is correct |
11 |
Correct |
470 ms |
79200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
13372 KB |
Output is correct |
2 |
Correct |
381 ms |
78428 KB |
Output is correct |
3 |
Correct |
385 ms |
78492 KB |
Output is correct |
4 |
Correct |
334 ms |
91640 KB |
Output is correct |
5 |
Correct |
55 ms |
13384 KB |
Output is correct |
6 |
Correct |
298 ms |
79036 KB |
Output is correct |
7 |
Correct |
324 ms |
70436 KB |
Output is correct |
8 |
Correct |
385 ms |
71892 KB |
Output is correct |
9 |
Correct |
422 ms |
71884 KB |
Output is correct |
10 |
Correct |
453 ms |
79468 KB |
Output is correct |
11 |
Correct |
470 ms |
79200 KB |
Output is correct |
12 |
Correct |
57 ms |
13296 KB |
Output is correct |
13 |
Correct |
634 ms |
81320 KB |
Output is correct |
14 |
Correct |
616 ms |
93724 KB |
Output is correct |
15 |
Correct |
855 ms |
80964 KB |
Output is correct |
16 |
Correct |
864 ms |
80844 KB |
Output is correct |
17 |
Correct |
57 ms |
14168 KB |
Output is correct |
18 |
Correct |
415 ms |
82084 KB |
Output is correct |
19 |
Correct |
450 ms |
73348 KB |
Output is correct |
20 |
Correct |
555 ms |
74644 KB |
Output is correct |
21 |
Correct |
506 ms |
74736 KB |
Output is correct |
22 |
Correct |
921 ms |
81852 KB |
Output is correct |
23 |
Correct |
830 ms |
88444 KB |
Output is correct |
24 |
Correct |
561 ms |
86988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13388 KB |
Output is correct |
2 |
Correct |
77 ms |
15216 KB |
Output is correct |
3 |
Correct |
72 ms |
15268 KB |
Output is correct |
4 |
Correct |
86 ms |
15352 KB |
Output is correct |
5 |
Correct |
87 ms |
15520 KB |
Output is correct |
6 |
Correct |
69 ms |
15340 KB |
Output is correct |
7 |
Correct |
54 ms |
13384 KB |
Output is correct |
8 |
Correct |
288 ms |
71188 KB |
Output is correct |
9 |
Correct |
283 ms |
71260 KB |
Output is correct |
10 |
Correct |
55 ms |
13312 KB |
Output is correct |
11 |
Correct |
371 ms |
78432 KB |
Output is correct |
12 |
Correct |
380 ms |
78488 KB |
Output is correct |
13 |
Correct |
340 ms |
91620 KB |
Output is correct |
14 |
Correct |
61 ms |
13480 KB |
Output is correct |
15 |
Correct |
303 ms |
78996 KB |
Output is correct |
16 |
Correct |
330 ms |
70404 KB |
Output is correct |
17 |
Correct |
410 ms |
71896 KB |
Output is correct |
18 |
Correct |
389 ms |
71952 KB |
Output is correct |
19 |
Correct |
452 ms |
79492 KB |
Output is correct |
20 |
Correct |
468 ms |
79248 KB |
Output is correct |
21 |
Correct |
310 ms |
70884 KB |
Output is correct |
22 |
Correct |
307 ms |
70736 KB |
Output is correct |
23 |
Correct |
335 ms |
71624 KB |
Output is correct |
24 |
Correct |
378 ms |
71900 KB |
Output is correct |
25 |
Correct |
449 ms |
76420 KB |
Output is correct |
26 |
Correct |
353 ms |
70148 KB |
Output is correct |
27 |
Correct |
337 ms |
70196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13388 KB |
Output is correct |
2 |
Correct |
77 ms |
15216 KB |
Output is correct |
3 |
Correct |
72 ms |
15268 KB |
Output is correct |
4 |
Correct |
86 ms |
15352 KB |
Output is correct |
5 |
Correct |
87 ms |
15520 KB |
Output is correct |
6 |
Correct |
69 ms |
15340 KB |
Output is correct |
7 |
Correct |
54 ms |
13384 KB |
Output is correct |
8 |
Correct |
288 ms |
71188 KB |
Output is correct |
9 |
Correct |
283 ms |
71260 KB |
Output is correct |
10 |
Correct |
55 ms |
13312 KB |
Output is correct |
11 |
Correct |
371 ms |
78432 KB |
Output is correct |
12 |
Correct |
380 ms |
78488 KB |
Output is correct |
13 |
Correct |
340 ms |
91620 KB |
Output is correct |
14 |
Correct |
61 ms |
13480 KB |
Output is correct |
15 |
Correct |
303 ms |
78996 KB |
Output is correct |
16 |
Correct |
330 ms |
70404 KB |
Output is correct |
17 |
Correct |
410 ms |
71896 KB |
Output is correct |
18 |
Correct |
389 ms |
71952 KB |
Output is correct |
19 |
Correct |
452 ms |
79492 KB |
Output is correct |
20 |
Correct |
468 ms |
79248 KB |
Output is correct |
21 |
Correct |
310 ms |
70884 KB |
Output is correct |
22 |
Correct |
307 ms |
70736 KB |
Output is correct |
23 |
Correct |
335 ms |
71624 KB |
Output is correct |
24 |
Correct |
378 ms |
71900 KB |
Output is correct |
25 |
Correct |
449 ms |
76420 KB |
Output is correct |
26 |
Correct |
353 ms |
70148 KB |
Output is correct |
27 |
Correct |
337 ms |
70196 KB |
Output is correct |
28 |
Correct |
56 ms |
13376 KB |
Output is correct |
29 |
Correct |
101 ms |
16340 KB |
Output is correct |
30 |
Correct |
80 ms |
16392 KB |
Output is correct |
31 |
Correct |
143 ms |
16364 KB |
Output is correct |
32 |
Correct |
145 ms |
16472 KB |
Output is correct |
33 |
Correct |
71 ms |
16440 KB |
Output is correct |
34 |
Correct |
54 ms |
14284 KB |
Output is correct |
35 |
Correct |
287 ms |
73904 KB |
Output is correct |
36 |
Correct |
239 ms |
73128 KB |
Output is correct |
37 |
Correct |
242 ms |
73128 KB |
Output is correct |
38 |
Correct |
56 ms |
14252 KB |
Output is correct |
39 |
Correct |
736 ms |
81340 KB |
Output is correct |
40 |
Correct |
678 ms |
93768 KB |
Output is correct |
41 |
Correct |
940 ms |
80888 KB |
Output is correct |
42 |
Correct |
1008 ms |
80940 KB |
Output is correct |
43 |
Correct |
58 ms |
14256 KB |
Output is correct |
44 |
Correct |
449 ms |
82000 KB |
Output is correct |
45 |
Correct |
461 ms |
73288 KB |
Output is correct |
46 |
Correct |
554 ms |
74716 KB |
Output is correct |
47 |
Correct |
575 ms |
74740 KB |
Output is correct |
48 |
Correct |
995 ms |
81864 KB |
Output is correct |
49 |
Correct |
879 ms |
88356 KB |
Output is correct |
50 |
Correct |
635 ms |
87024 KB |
Output is correct |
51 |
Correct |
289 ms |
73996 KB |
Output is correct |
52 |
Correct |
268 ms |
74024 KB |
Output is correct |
53 |
Correct |
269 ms |
73608 KB |
Output is correct |
54 |
Correct |
266 ms |
74276 KB |
Output is correct |
55 |
Correct |
275 ms |
73740 KB |
Output is correct |
56 |
Correct |
456 ms |
74764 KB |
Output is correct |
57 |
Correct |
567 ms |
79484 KB |
Output is correct |
58 |
Correct |
622 ms |
72952 KB |
Output is correct |
59 |
Correct |
427 ms |
73424 KB |
Output is correct |