#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();
} 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);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
600 KB |
Output is correct |
2 |
Correct |
27 ms |
2340 KB |
Output is correct |
3 |
Correct |
26 ms |
2688 KB |
Output is correct |
4 |
Correct |
27 ms |
2396 KB |
Output is correct |
5 |
Correct |
23 ms |
2384 KB |
Output is correct |
6 |
Correct |
27 ms |
2192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
600 KB |
Output is correct |
2 |
Correct |
27 ms |
2340 KB |
Output is correct |
3 |
Correct |
26 ms |
2688 KB |
Output is correct |
4 |
Correct |
27 ms |
2396 KB |
Output is correct |
5 |
Correct |
23 ms |
2384 KB |
Output is correct |
6 |
Correct |
27 ms |
2192 KB |
Output is correct |
7 |
Correct |
19 ms |
604 KB |
Output is correct |
8 |
Correct |
221 ms |
2488 KB |
Output is correct |
9 |
Correct |
195 ms |
2424 KB |
Output is correct |
10 |
Correct |
211 ms |
2388 KB |
Output is correct |
11 |
Correct |
361 ms |
2768 KB |
Output is correct |
12 |
Correct |
184 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
292 ms |
56404 KB |
Output is correct |
3 |
Correct |
285 ms |
56508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
292 ms |
56404 KB |
Output is correct |
3 |
Correct |
285 ms |
56508 KB |
Output is correct |
4 |
Correct |
19 ms |
604 KB |
Output is correct |
5 |
Correct |
371 ms |
56884 KB |
Output is correct |
6 |
Correct |
504 ms |
57172 KB |
Output is correct |
7 |
Correct |
570 ms |
57184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
259 ms |
66160 KB |
Output is correct |
3 |
Correct |
303 ms |
66232 KB |
Output is correct |
4 |
Correct |
189 ms |
56660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
604 KB |
Output is correct |
2 |
Correct |
259 ms |
66160 KB |
Output is correct |
3 |
Correct |
303 ms |
66232 KB |
Output is correct |
4 |
Correct |
189 ms |
56660 KB |
Output is correct |
5 |
Correct |
20 ms |
604 KB |
Output is correct |
6 |
Correct |
532 ms |
66428 KB |
Output is correct |
7 |
Correct |
411 ms |
60500 KB |
Output is correct |
8 |
Correct |
766 ms |
66388 KB |
Output is correct |
9 |
Correct |
770 ms |
66384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
600 KB |
Output is correct |
2 |
Correct |
306 ms |
145912 KB |
Output is correct |
3 |
Correct |
300 ms |
68852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
600 KB |
Output is correct |
2 |
Correct |
306 ms |
145912 KB |
Output is correct |
3 |
Correct |
300 ms |
68852 KB |
Output is correct |
4 |
Correct |
20 ms |
604 KB |
Output is correct |
5 |
Correct |
557 ms |
146256 KB |
Output is correct |
6 |
Correct |
603 ms |
69196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
604 KB |
Output is correct |
2 |
Correct |
271 ms |
66260 KB |
Output is correct |
3 |
Correct |
254 ms |
66052 KB |
Output is correct |
4 |
Correct |
163 ms |
56592 KB |
Output is correct |
5 |
Correct |
16 ms |
604 KB |
Output is correct |
6 |
Correct |
301 ms |
145784 KB |
Output is correct |
7 |
Correct |
328 ms |
68948 KB |
Output is correct |
8 |
Correct |
305 ms |
69716 KB |
Output is correct |
9 |
Correct |
305 ms |
69560 KB |
Output is correct |
10 |
Correct |
323 ms |
78488 KB |
Output is correct |
11 |
Correct |
353 ms |
80180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
604 KB |
Output is correct |
2 |
Correct |
271 ms |
66260 KB |
Output is correct |
3 |
Correct |
254 ms |
66052 KB |
Output is correct |
4 |
Correct |
163 ms |
56592 KB |
Output is correct |
5 |
Correct |
16 ms |
604 KB |
Output is correct |
6 |
Correct |
301 ms |
145784 KB |
Output is correct |
7 |
Correct |
328 ms |
68948 KB |
Output is correct |
8 |
Correct |
305 ms |
69716 KB |
Output is correct |
9 |
Correct |
305 ms |
69560 KB |
Output is correct |
10 |
Correct |
323 ms |
78488 KB |
Output is correct |
11 |
Correct |
353 ms |
80180 KB |
Output is correct |
12 |
Correct |
20 ms |
836 KB |
Output is correct |
13 |
Correct |
523 ms |
66500 KB |
Output is correct |
14 |
Correct |
380 ms |
60468 KB |
Output is correct |
15 |
Correct |
753 ms |
66308 KB |
Output is correct |
16 |
Correct |
763 ms |
66492 KB |
Output is correct |
17 |
Correct |
19 ms |
604 KB |
Output is correct |
18 |
Correct |
536 ms |
146204 KB |
Output is correct |
19 |
Correct |
606 ms |
69200 KB |
Output is correct |
20 |
Correct |
783 ms |
69972 KB |
Output is correct |
21 |
Correct |
634 ms |
69968 KB |
Output is correct |
22 |
Correct |
748 ms |
78512 KB |
Output is correct |
23 |
Correct |
765 ms |
63036 KB |
Output is correct |
24 |
Correct |
423 ms |
64340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
856 KB |
Output is correct |
2 |
Correct |
26 ms |
2440 KB |
Output is correct |
3 |
Correct |
26 ms |
2700 KB |
Output is correct |
4 |
Correct |
25 ms |
2388 KB |
Output is correct |
5 |
Correct |
27 ms |
2396 KB |
Output is correct |
6 |
Correct |
28 ms |
2136 KB |
Output is correct |
7 |
Correct |
16 ms |
860 KB |
Output is correct |
8 |
Correct |
273 ms |
56396 KB |
Output is correct |
9 |
Correct |
289 ms |
56404 KB |
Output is correct |
10 |
Correct |
16 ms |
600 KB |
Output is correct |
11 |
Correct |
263 ms |
66288 KB |
Output is correct |
12 |
Correct |
263 ms |
66176 KB |
Output is correct |
13 |
Correct |
180 ms |
56556 KB |
Output is correct |
14 |
Correct |
16 ms |
604 KB |
Output is correct |
15 |
Correct |
320 ms |
145836 KB |
Output is correct |
16 |
Correct |
308 ms |
68860 KB |
Output is correct |
17 |
Correct |
315 ms |
69512 KB |
Output is correct |
18 |
Correct |
305 ms |
69876 KB |
Output is correct |
19 |
Correct |
338 ms |
78464 KB |
Output is correct |
20 |
Correct |
372 ms |
80212 KB |
Output is correct |
21 |
Correct |
313 ms |
60388 KB |
Output is correct |
22 |
Correct |
316 ms |
68112 KB |
Output is correct |
23 |
Correct |
307 ms |
72016 KB |
Output is correct |
24 |
Correct |
342 ms |
71712 KB |
Output is correct |
25 |
Correct |
298 ms |
61188 KB |
Output is correct |
26 |
Correct |
480 ms |
160812 KB |
Output is correct |
27 |
Correct |
471 ms |
171184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
856 KB |
Output is correct |
2 |
Correct |
26 ms |
2440 KB |
Output is correct |
3 |
Correct |
26 ms |
2700 KB |
Output is correct |
4 |
Correct |
25 ms |
2388 KB |
Output is correct |
5 |
Correct |
27 ms |
2396 KB |
Output is correct |
6 |
Correct |
28 ms |
2136 KB |
Output is correct |
7 |
Correct |
16 ms |
860 KB |
Output is correct |
8 |
Correct |
273 ms |
56396 KB |
Output is correct |
9 |
Correct |
289 ms |
56404 KB |
Output is correct |
10 |
Correct |
16 ms |
600 KB |
Output is correct |
11 |
Correct |
263 ms |
66288 KB |
Output is correct |
12 |
Correct |
263 ms |
66176 KB |
Output is correct |
13 |
Correct |
180 ms |
56556 KB |
Output is correct |
14 |
Correct |
16 ms |
604 KB |
Output is correct |
15 |
Correct |
320 ms |
145836 KB |
Output is correct |
16 |
Correct |
308 ms |
68860 KB |
Output is correct |
17 |
Correct |
315 ms |
69512 KB |
Output is correct |
18 |
Correct |
305 ms |
69876 KB |
Output is correct |
19 |
Correct |
338 ms |
78464 KB |
Output is correct |
20 |
Correct |
372 ms |
80212 KB |
Output is correct |
21 |
Correct |
313 ms |
60388 KB |
Output is correct |
22 |
Correct |
316 ms |
68112 KB |
Output is correct |
23 |
Correct |
307 ms |
72016 KB |
Output is correct |
24 |
Correct |
342 ms |
71712 KB |
Output is correct |
25 |
Correct |
298 ms |
61188 KB |
Output is correct |
26 |
Correct |
480 ms |
160812 KB |
Output is correct |
27 |
Correct |
471 ms |
171184 KB |
Output is correct |
28 |
Correct |
19 ms |
604 KB |
Output is correct |
29 |
Correct |
220 ms |
2348 KB |
Output is correct |
30 |
Correct |
186 ms |
2648 KB |
Output is correct |
31 |
Correct |
208 ms |
2388 KB |
Output is correct |
32 |
Correct |
373 ms |
2384 KB |
Output is correct |
33 |
Correct |
180 ms |
2140 KB |
Output is correct |
34 |
Correct |
18 ms |
860 KB |
Output is correct |
35 |
Correct |
373 ms |
56824 KB |
Output is correct |
36 |
Correct |
497 ms |
57168 KB |
Output is correct |
37 |
Correct |
552 ms |
56916 KB |
Output is correct |
38 |
Correct |
20 ms |
668 KB |
Output is correct |
39 |
Correct |
527 ms |
66384 KB |
Output is correct |
40 |
Correct |
367 ms |
60512 KB |
Output is correct |
41 |
Correct |
759 ms |
66484 KB |
Output is correct |
42 |
Correct |
768 ms |
66420 KB |
Output is correct |
43 |
Correct |
25 ms |
604 KB |
Output is correct |
44 |
Correct |
549 ms |
146176 KB |
Output is correct |
45 |
Correct |
610 ms |
69264 KB |
Output is correct |
46 |
Correct |
795 ms |
70032 KB |
Output is correct |
47 |
Correct |
655 ms |
69968 KB |
Output is correct |
48 |
Correct |
752 ms |
78552 KB |
Output is correct |
49 |
Correct |
730 ms |
63060 KB |
Output is correct |
50 |
Correct |
424 ms |
64340 KB |
Output is correct |
51 |
Correct |
564 ms |
58192 KB |
Output is correct |
52 |
Correct |
529 ms |
56992 KB |
Output is correct |
53 |
Correct |
618 ms |
56912 KB |
Output is correct |
54 |
Correct |
533 ms |
57104 KB |
Output is correct |
55 |
Correct |
769 ms |
68952 KB |
Output is correct |
56 |
Correct |
655 ms |
71508 KB |
Output is correct |
57 |
Correct |
829 ms |
61780 KB |
Output is correct |
58 |
Correct |
826 ms |
137900 KB |
Output is correct |
59 |
Correct |
547 ms |
177484 KB |
Output is correct |