#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
void debug(auto l, auto r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010, inf = 1e9;
// res == -10 ? not a query
// res == -2 ? no, it doesn't have it
// res == -1 ? yes, it has it
// res >= 0 ? how many is it
int n, q, res[MAX_N];
// time, id
vector<pair<int,int>> edge[MAX_N];
// given time, id how many has this
vector<int> qcnt[MAX_N];
// given time, whether x has it
vector<pair<int,int>> qif[MAX_N];
bool ban[MAX_N];
int sz[MAX_N], bitv[MAX_N], in_time[MAX_N];
void add(int i, int d) {
for (;i <= n+q;i += i&-i)
bitv[i] += d;
}
int qry(int i) {
int res = 0;
for (;i;i ^= i&-i)
res += bitv[i];
return res;
}
void clean_edge(int x) {
int sz = 0;
for (auto [t, u] : edge[x]) if (!ban[u]) edge[x][sz++] = {t, u};
edge[x].resize(sz);
}
int dfs_sz(int x, int lst) {
sz[x] = 1;
for (auto [t, u] : edge[x]) if (!ban[u] && u != lst)
sz[x] += dfs_sz(u, x);
return sz[x];
}
int dfs_cen(int x, int allsz, int lst) {
for (auto [t, u] : edge[x]) if (!ban[u] && u != lst)
if (sz[u] * 2 > allsz) return dfs_cen(u, allsz, x);
return x;
}
void dfs_in(int x, int lst_v, int lst) {
in_time[x] = lst_v;
add(lst_v, 1);
for (auto [t, u] : edge[x]) if (u != lst && !ban[u])
if (t >= lst_v)
dfs_in(u, t, x);
}
void dfs_out(int x, int lst_v, int lst) {
in_time[x] = inf;
add(lst_v, -1);
for (auto [t, u] : edge[x]) if (u != lst && !ban[u])
if (t >= lst_v)
dfs_out(u, t, x);
}
void do_ans(int u) {
for (int t : qcnt[u]) {
DE(t, n + q);
res[t] += qry(t);
}
for (auto [t, x] : qif[u]) {
if (in_time[x] <= t)
res[t] = -1;
}
}
void dfs_solve(int x, int lst_v, int lst) {
do_ans(x);
for (auto [t, u] : edge[x]) if (!ban[u] && u != lst) {
if (t > lst_v) continue;
dfs_solve(u, t, x);
}
}
void solve(int o = 1) {
if (ban[o]) return;
dfs_sz(o, o);
int cen = dfs_cen(o, sz[o], o);
clean_edge(cen);
sort(AI(edge[cen]));
dfs_in(cen, 1, cen);
do_ans(cen);
for (auto [t, u] : edge[cen]) {
dfs_out(u, t, cen);
add(in_time[cen], -1);
add(in_time[cen] = t, 1);
dfs_solve(u, t, cen);
}
ban[cen] = true;
add(in_time[cen], -1);
in_time[cen] = inf;
for (auto [t, x] : edge[cen])
solve(x);
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> q;
for (int i = 1;i < n+q;++i) {
char c;
int a, b;
cin >> c;
if (c == 'C') {
cin >> a;
qcnt[a].pb(i);
res[i] = 0;
continue;
}
cin >> a >> b;
if (c == 'S') {
edge[a].pb(i, b);
edge[b].pb(i, a);
res[i] = -10;
}
if (c == 'Q') {
res[i] = -2;
qif[b].pb(i, a);
}
DE(i, res[i]);
}
fill(in_time, in_time + n + 1, inf);
solve();
for (int i = 1;i < n + q;++i) {
if (res[i] == -10) continue;
if (res[i] == -2) {
cout << "no\n";
continue;
}
if (res[i] == -1) {
cout << "yes\n";
continue;
}
cout << res[i] << '\n';
}
}
Compilation message
servers.cpp: In function 'void do_ans(int)':
servers.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
servers.cpp:80:3: note: in expansion of macro 'DE'
80 | DE(t, n + q);
| ^~
servers.cpp: In function 'int32_t main()':
servers.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
servers.cpp:145:3: note: in expansion of macro 'DE'
145 | DE(i, res[i]);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24152 KB |
Output is correct |
2 |
Correct |
53 ms |
24596 KB |
Output is correct |
3 |
Correct |
50 ms |
24392 KB |
Output is correct |
4 |
Correct |
51 ms |
24644 KB |
Output is correct |
5 |
Correct |
52 ms |
24416 KB |
Output is correct |
6 |
Correct |
50 ms |
24368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24152 KB |
Output is correct |
2 |
Correct |
53 ms |
24596 KB |
Output is correct |
3 |
Correct |
50 ms |
24392 KB |
Output is correct |
4 |
Correct |
51 ms |
24644 KB |
Output is correct |
5 |
Correct |
52 ms |
24416 KB |
Output is correct |
6 |
Correct |
50 ms |
24368 KB |
Output is correct |
7 |
Correct |
41 ms |
24168 KB |
Output is correct |
8 |
Correct |
54 ms |
24172 KB |
Output is correct |
9 |
Correct |
49 ms |
24104 KB |
Output is correct |
10 |
Correct |
55 ms |
24204 KB |
Output is correct |
11 |
Correct |
60 ms |
24008 KB |
Output is correct |
12 |
Correct |
50 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
24348 KB |
Output is correct |
2 |
Correct |
204 ms |
31768 KB |
Output is correct |
3 |
Correct |
197 ms |
31740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
24348 KB |
Output is correct |
2 |
Correct |
204 ms |
31768 KB |
Output is correct |
3 |
Correct |
197 ms |
31740 KB |
Output is correct |
4 |
Correct |
45 ms |
24148 KB |
Output is correct |
5 |
Correct |
222 ms |
34256 KB |
Output is correct |
6 |
Correct |
137 ms |
31928 KB |
Output is correct |
7 |
Correct |
147 ms |
32080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
24260 KB |
Output is correct |
2 |
Correct |
279 ms |
38452 KB |
Output is correct |
3 |
Correct |
259 ms |
38468 KB |
Output is correct |
4 |
Correct |
235 ms |
38332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
24260 KB |
Output is correct |
2 |
Correct |
279 ms |
38452 KB |
Output is correct |
3 |
Correct |
259 ms |
38468 KB |
Output is correct |
4 |
Correct |
235 ms |
38332 KB |
Output is correct |
5 |
Correct |
39 ms |
24216 KB |
Output is correct |
6 |
Correct |
303 ms |
41880 KB |
Output is correct |
7 |
Correct |
271 ms |
41884 KB |
Output is correct |
8 |
Correct |
289 ms |
40984 KB |
Output is correct |
9 |
Correct |
289 ms |
40932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24236 KB |
Output is correct |
2 |
Correct |
241 ms |
34988 KB |
Output is correct |
3 |
Correct |
234 ms |
35308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24236 KB |
Output is correct |
2 |
Correct |
241 ms |
34988 KB |
Output is correct |
3 |
Correct |
234 ms |
35308 KB |
Output is correct |
4 |
Correct |
40 ms |
24272 KB |
Output is correct |
5 |
Correct |
271 ms |
35120 KB |
Output is correct |
6 |
Correct |
278 ms |
35212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
24260 KB |
Output is correct |
2 |
Correct |
313 ms |
38500 KB |
Output is correct |
3 |
Correct |
265 ms |
38440 KB |
Output is correct |
4 |
Correct |
245 ms |
38260 KB |
Output is correct |
5 |
Correct |
38 ms |
24220 KB |
Output is correct |
6 |
Correct |
244 ms |
35028 KB |
Output is correct |
7 |
Correct |
249 ms |
35180 KB |
Output is correct |
8 |
Correct |
274 ms |
35600 KB |
Output is correct |
9 |
Correct |
306 ms |
35780 KB |
Output is correct |
10 |
Correct |
360 ms |
38900 KB |
Output is correct |
11 |
Correct |
355 ms |
37432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
24260 KB |
Output is correct |
2 |
Correct |
313 ms |
38500 KB |
Output is correct |
3 |
Correct |
265 ms |
38440 KB |
Output is correct |
4 |
Correct |
245 ms |
38260 KB |
Output is correct |
5 |
Correct |
38 ms |
24220 KB |
Output is correct |
6 |
Correct |
244 ms |
35028 KB |
Output is correct |
7 |
Correct |
249 ms |
35180 KB |
Output is correct |
8 |
Correct |
274 ms |
35600 KB |
Output is correct |
9 |
Correct |
306 ms |
35780 KB |
Output is correct |
10 |
Correct |
360 ms |
38900 KB |
Output is correct |
11 |
Correct |
355 ms |
37432 KB |
Output is correct |
12 |
Correct |
45 ms |
24284 KB |
Output is correct |
13 |
Correct |
315 ms |
41848 KB |
Output is correct |
14 |
Correct |
289 ms |
41824 KB |
Output is correct |
15 |
Correct |
314 ms |
41124 KB |
Output is correct |
16 |
Correct |
301 ms |
40984 KB |
Output is correct |
17 |
Correct |
41 ms |
25104 KB |
Output is correct |
18 |
Correct |
275 ms |
35120 KB |
Output is correct |
19 |
Correct |
269 ms |
35188 KB |
Output is correct |
20 |
Correct |
280 ms |
35624 KB |
Output is correct |
21 |
Correct |
301 ms |
36080 KB |
Output is correct |
22 |
Correct |
434 ms |
37412 KB |
Output is correct |
23 |
Correct |
512 ms |
38460 KB |
Output is correct |
24 |
Correct |
466 ms |
38020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24260 KB |
Output is correct |
2 |
Correct |
52 ms |
24608 KB |
Output is correct |
3 |
Correct |
61 ms |
24440 KB |
Output is correct |
4 |
Correct |
55 ms |
24660 KB |
Output is correct |
5 |
Correct |
53 ms |
24352 KB |
Output is correct |
6 |
Correct |
61 ms |
24328 KB |
Output is correct |
7 |
Correct |
42 ms |
24176 KB |
Output is correct |
8 |
Correct |
219 ms |
34476 KB |
Output is correct |
9 |
Correct |
222 ms |
34564 KB |
Output is correct |
10 |
Correct |
40 ms |
25040 KB |
Output is correct |
11 |
Correct |
304 ms |
41712 KB |
Output is correct |
12 |
Correct |
314 ms |
41772 KB |
Output is correct |
13 |
Correct |
249 ms |
41544 KB |
Output is correct |
14 |
Correct |
43 ms |
25096 KB |
Output is correct |
15 |
Correct |
247 ms |
34960 KB |
Output is correct |
16 |
Correct |
259 ms |
35152 KB |
Output is correct |
17 |
Correct |
286 ms |
35556 KB |
Output is correct |
18 |
Correct |
292 ms |
35764 KB |
Output is correct |
19 |
Correct |
367 ms |
38908 KB |
Output is correct |
20 |
Correct |
342 ms |
37500 KB |
Output is correct |
21 |
Correct |
250 ms |
35100 KB |
Output is correct |
22 |
Correct |
255 ms |
35108 KB |
Output is correct |
23 |
Correct |
261 ms |
35372 KB |
Output is correct |
24 |
Correct |
257 ms |
35452 KB |
Output is correct |
25 |
Correct |
345 ms |
38352 KB |
Output is correct |
26 |
Correct |
293 ms |
34420 KB |
Output is correct |
27 |
Correct |
279 ms |
34360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24260 KB |
Output is correct |
2 |
Correct |
52 ms |
24608 KB |
Output is correct |
3 |
Correct |
61 ms |
24440 KB |
Output is correct |
4 |
Correct |
55 ms |
24660 KB |
Output is correct |
5 |
Correct |
53 ms |
24352 KB |
Output is correct |
6 |
Correct |
61 ms |
24328 KB |
Output is correct |
7 |
Correct |
42 ms |
24176 KB |
Output is correct |
8 |
Correct |
219 ms |
34476 KB |
Output is correct |
9 |
Correct |
222 ms |
34564 KB |
Output is correct |
10 |
Correct |
40 ms |
25040 KB |
Output is correct |
11 |
Correct |
304 ms |
41712 KB |
Output is correct |
12 |
Correct |
314 ms |
41772 KB |
Output is correct |
13 |
Correct |
249 ms |
41544 KB |
Output is correct |
14 |
Correct |
43 ms |
25096 KB |
Output is correct |
15 |
Correct |
247 ms |
34960 KB |
Output is correct |
16 |
Correct |
259 ms |
35152 KB |
Output is correct |
17 |
Correct |
286 ms |
35556 KB |
Output is correct |
18 |
Correct |
292 ms |
35764 KB |
Output is correct |
19 |
Correct |
367 ms |
38908 KB |
Output is correct |
20 |
Correct |
342 ms |
37500 KB |
Output is correct |
21 |
Correct |
250 ms |
35100 KB |
Output is correct |
22 |
Correct |
255 ms |
35108 KB |
Output is correct |
23 |
Correct |
261 ms |
35372 KB |
Output is correct |
24 |
Correct |
257 ms |
35452 KB |
Output is correct |
25 |
Correct |
345 ms |
38352 KB |
Output is correct |
26 |
Correct |
293 ms |
34420 KB |
Output is correct |
27 |
Correct |
279 ms |
34360 KB |
Output is correct |
28 |
Correct |
44 ms |
24360 KB |
Output is correct |
29 |
Correct |
55 ms |
25208 KB |
Output is correct |
30 |
Correct |
51 ms |
25092 KB |
Output is correct |
31 |
Correct |
62 ms |
25324 KB |
Output is correct |
32 |
Correct |
57 ms |
25100 KB |
Output is correct |
33 |
Correct |
51 ms |
24992 KB |
Output is correct |
34 |
Correct |
43 ms |
25040 KB |
Output is correct |
35 |
Correct |
223 ms |
34228 KB |
Output is correct |
36 |
Correct |
145 ms |
31860 KB |
Output is correct |
37 |
Correct |
160 ms |
32076 KB |
Output is correct |
38 |
Correct |
41 ms |
25036 KB |
Output is correct |
39 |
Correct |
328 ms |
41784 KB |
Output is correct |
40 |
Correct |
284 ms |
42052 KB |
Output is correct |
41 |
Correct |
270 ms |
40984 KB |
Output is correct |
42 |
Correct |
268 ms |
41000 KB |
Output is correct |
43 |
Correct |
45 ms |
25112 KB |
Output is correct |
44 |
Correct |
266 ms |
35140 KB |
Output is correct |
45 |
Correct |
240 ms |
35172 KB |
Output is correct |
46 |
Correct |
258 ms |
35628 KB |
Output is correct |
47 |
Correct |
294 ms |
35988 KB |
Output is correct |
48 |
Correct |
385 ms |
37476 KB |
Output is correct |
49 |
Correct |
466 ms |
38464 KB |
Output is correct |
50 |
Correct |
426 ms |
38100 KB |
Output is correct |
51 |
Correct |
167 ms |
33244 KB |
Output is correct |
52 |
Correct |
149 ms |
32828 KB |
Output is correct |
53 |
Correct |
145 ms |
32440 KB |
Output is correct |
54 |
Correct |
143 ms |
33060 KB |
Output is correct |
55 |
Correct |
151 ms |
32816 KB |
Output is correct |
56 |
Correct |
227 ms |
34424 KB |
Output is correct |
57 |
Correct |
250 ms |
36052 KB |
Output is correct |
58 |
Correct |
353 ms |
34704 KB |
Output is correct |
59 |
Correct |
268 ms |
34364 KB |
Output is correct |