#include <stdio.h>
#include <stdlib.h>
int max(int i, int j) { return i > j ? i : j; }
/*
* Try merge small 2 large, with persistent ds -> O(nlg^2n)
* How to maintain "frequency" of each data chunk ? ??
*
*/
#define N 120001
#define B 200
#define M 30000000
int n, q, L[M], R[M], A[M], alc, root[N], temp, dp[N], counted[N];
int p2(int l, int r, int x) {
int p = ++alc;
A[p] = A[L[p]=l] + A[R[p]=r] + x;
return p;
}
int pbuild(int l, int r) {
return l == r ? ++alc : p2(pbuild(l, (l+r)/2), pbuild((l+r)/2+1, r), 0);
}
int pupd(int v, int l, int r, int p) {
if (l == r) return p2(0, 0, 1);
if (p <= (l+r)/2) return p2(pupd(L[v], l, (l+r)/2, p), R[v], 0);
return p2(L[v], pupd(R[v], (l+r)/2+1, r, p), 0);
}
int pqry(int v, int l, int r, int k) {
if (l == r) return l;
if (A[L[v]] < k) return pqry(R[v], (l+r)/2+1, r, k - A[L[v]]);
return pqry(L[v], l, (l+r)/2, k);
}
int ppt(int v, int l, int r, int p) {
if (l == r) return A[v];
return p <= (l+r)/2 ? ppt(L[v], l, (l+r)/2, p) : ppt(R[v], (l+r)/2+1, r, p);
}
int l[N], r[N], k, ds[N], k2;
int find(int i) { return ds[i] == i ? i : (ds[i] = find(ds[i])); }
void run() {
for (int i = 1; i <= n; ++i) dp[i] = 1;
for (int i = k; i < k2; ++i)
ds[find(l[i])] = find(r[i]);
for (int i = k2 - 1; i >= 0; --i) {
int y = dp[r[i]];
dp[r[i]] += dp[l[i]];
dp[l[i]] += y;
}
k = k2;
}
int main() {
scanf("%d%d", &n, &q);
root[0] = pbuild(1, n);
for (int i = 1; i <= n; ++i) root[i] = pupd(root[0], 1, n, i),
dp[i] = 1, ds[i] = i;
q = n + q - 1;
for (int i = 1; i <= q; ++i) {
char type[2];
int a, b;
scanf(" %s%d", type, &a);
if ('S' == type[0]) {
scanf("%d", &b);
if (A[root[a]] > A[root[b]])
temp = a, a = b, b = temp;
int tot = A[root[a]], at = 0;
while (at < tot)
root[b] = pupd(root[b], 1, n, pqry(root[a], 1, n, ++at));
root[a] = root[b];
l[k2] = a, r[k2] = b;
if ((++k2) - k == B)
run(), k = 0;
} else if ('Q' == type[0]) {
scanf("%d", &b);
puts(ppt(root[a], 1, n, b) ? "yes" : "no");
} else {
int z = dp[a];
for (int j = k; j < k2; ++j) {
int x[2] = { l[j], r[j] };
for (int u = 0; u < 2; ++u)
if (counted[x[u]] != i && find(x[u]) != find(a) &&
ppt(root[x[u]], 1, n, a))
++z, counted[x[u]] = i;
}
printf("%d\n", z);
}
}
}
Compilation message
servers.c: In function 'main':
servers.c:55:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
servers.c:65:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf(" %s%d", type, &a);
| ^~~~~~~~~~~~~~~~~~~~~~~~
servers.c:67:7: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d", &b);
| ^~~~~~~~~~~~~~~
servers.c:78:7: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &b);
| ^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
26 ms |
2312 KB |
Output is correct |
3 |
Correct |
25 ms |
2640 KB |
Output is correct |
4 |
Correct |
25 ms |
2392 KB |
Output is correct |
5 |
Correct |
24 ms |
2368 KB |
Output is correct |
6 |
Correct |
30 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
26 ms |
2312 KB |
Output is correct |
3 |
Correct |
25 ms |
2640 KB |
Output is correct |
4 |
Correct |
25 ms |
2392 KB |
Output is correct |
5 |
Correct |
24 ms |
2368 KB |
Output is correct |
6 |
Correct |
30 ms |
2132 KB |
Output is correct |
7 |
Correct |
21 ms |
604 KB |
Output is correct |
8 |
Execution timed out |
3536 ms |
2384 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
856 KB |
Output is correct |
2 |
Correct |
213 ms |
56392 KB |
Output is correct |
3 |
Correct |
217 ms |
56400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
856 KB |
Output is correct |
2 |
Correct |
213 ms |
56392 KB |
Output is correct |
3 |
Correct |
217 ms |
56400 KB |
Output is correct |
4 |
Correct |
21 ms |
860 KB |
Output is correct |
5 |
Execution timed out |
3569 ms |
36948 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
680 KB |
Output is correct |
2 |
Correct |
181 ms |
66216 KB |
Output is correct |
3 |
Correct |
178 ms |
66192 KB |
Output is correct |
4 |
Correct |
110 ms |
56660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
680 KB |
Output is correct |
2 |
Correct |
181 ms |
66216 KB |
Output is correct |
3 |
Correct |
178 ms |
66192 KB |
Output is correct |
4 |
Correct |
110 ms |
56660 KB |
Output is correct |
5 |
Correct |
19 ms |
604 KB |
Output is correct |
6 |
Execution timed out |
3562 ms |
34176 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
604 KB |
Output is correct |
2 |
Correct |
238 ms |
145868 KB |
Output is correct |
3 |
Correct |
229 ms |
69160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
604 KB |
Output is correct |
2 |
Correct |
238 ms |
145868 KB |
Output is correct |
3 |
Correct |
229 ms |
69160 KB |
Output is correct |
4 |
Correct |
21 ms |
660 KB |
Output is correct |
5 |
Execution timed out |
3532 ms |
35880 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
604 KB |
Output is correct |
2 |
Correct |
190 ms |
66056 KB |
Output is correct |
3 |
Correct |
185 ms |
66156 KB |
Output is correct |
4 |
Correct |
110 ms |
56632 KB |
Output is correct |
5 |
Correct |
16 ms |
600 KB |
Output is correct |
6 |
Correct |
242 ms |
146008 KB |
Output is correct |
7 |
Correct |
200 ms |
68876 KB |
Output is correct |
8 |
Correct |
230 ms |
69712 KB |
Output is correct |
9 |
Correct |
228 ms |
69812 KB |
Output is correct |
10 |
Correct |
258 ms |
78416 KB |
Output is correct |
11 |
Correct |
261 ms |
80252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
604 KB |
Output is correct |
2 |
Correct |
190 ms |
66056 KB |
Output is correct |
3 |
Correct |
185 ms |
66156 KB |
Output is correct |
4 |
Correct |
110 ms |
56632 KB |
Output is correct |
5 |
Correct |
16 ms |
600 KB |
Output is correct |
6 |
Correct |
242 ms |
146008 KB |
Output is correct |
7 |
Correct |
200 ms |
68876 KB |
Output is correct |
8 |
Correct |
230 ms |
69712 KB |
Output is correct |
9 |
Correct |
228 ms |
69812 KB |
Output is correct |
10 |
Correct |
258 ms |
78416 KB |
Output is correct |
11 |
Correct |
261 ms |
80252 KB |
Output is correct |
12 |
Correct |
19 ms |
600 KB |
Output is correct |
13 |
Execution timed out |
3587 ms |
34200 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
28 ms |
2352 KB |
Output is correct |
3 |
Correct |
25 ms |
2708 KB |
Output is correct |
4 |
Correct |
25 ms |
2396 KB |
Output is correct |
5 |
Correct |
24 ms |
2320 KB |
Output is correct |
6 |
Correct |
28 ms |
2184 KB |
Output is correct |
7 |
Correct |
16 ms |
860 KB |
Output is correct |
8 |
Correct |
201 ms |
56404 KB |
Output is correct |
9 |
Correct |
185 ms |
56392 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
11 |
Correct |
169 ms |
66132 KB |
Output is correct |
12 |
Correct |
165 ms |
66240 KB |
Output is correct |
13 |
Correct |
104 ms |
56660 KB |
Output is correct |
14 |
Correct |
16 ms |
604 KB |
Output is correct |
15 |
Correct |
233 ms |
145872 KB |
Output is correct |
16 |
Correct |
209 ms |
68948 KB |
Output is correct |
17 |
Correct |
220 ms |
69732 KB |
Output is correct |
18 |
Correct |
214 ms |
69640 KB |
Output is correct |
19 |
Correct |
270 ms |
78420 KB |
Output is correct |
20 |
Correct |
268 ms |
80208 KB |
Output is correct |
21 |
Correct |
226 ms |
60500 KB |
Output is correct |
22 |
Correct |
247 ms |
68132 KB |
Output is correct |
23 |
Correct |
236 ms |
72020 KB |
Output is correct |
24 |
Correct |
246 ms |
71760 KB |
Output is correct |
25 |
Correct |
202 ms |
61264 KB |
Output is correct |
26 |
Correct |
375 ms |
161104 KB |
Output is correct |
27 |
Correct |
389 ms |
171088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
28 ms |
2352 KB |
Output is correct |
3 |
Correct |
25 ms |
2708 KB |
Output is correct |
4 |
Correct |
25 ms |
2396 KB |
Output is correct |
5 |
Correct |
24 ms |
2320 KB |
Output is correct |
6 |
Correct |
28 ms |
2184 KB |
Output is correct |
7 |
Correct |
16 ms |
860 KB |
Output is correct |
8 |
Correct |
201 ms |
56404 KB |
Output is correct |
9 |
Correct |
185 ms |
56392 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
11 |
Correct |
169 ms |
66132 KB |
Output is correct |
12 |
Correct |
165 ms |
66240 KB |
Output is correct |
13 |
Correct |
104 ms |
56660 KB |
Output is correct |
14 |
Correct |
16 ms |
604 KB |
Output is correct |
15 |
Correct |
233 ms |
145872 KB |
Output is correct |
16 |
Correct |
209 ms |
68948 KB |
Output is correct |
17 |
Correct |
220 ms |
69732 KB |
Output is correct |
18 |
Correct |
214 ms |
69640 KB |
Output is correct |
19 |
Correct |
270 ms |
78420 KB |
Output is correct |
20 |
Correct |
268 ms |
80208 KB |
Output is correct |
21 |
Correct |
226 ms |
60500 KB |
Output is correct |
22 |
Correct |
247 ms |
68132 KB |
Output is correct |
23 |
Correct |
236 ms |
72020 KB |
Output is correct |
24 |
Correct |
246 ms |
71760 KB |
Output is correct |
25 |
Correct |
202 ms |
61264 KB |
Output is correct |
26 |
Correct |
375 ms |
161104 KB |
Output is correct |
27 |
Correct |
389 ms |
171088 KB |
Output is correct |
28 |
Correct |
19 ms |
604 KB |
Output is correct |
29 |
Execution timed out |
3566 ms |
2292 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |