Submission #1042539

#TimeUsernameProblemLanguageResultExecution timeMemory
1042539sleepntsheepInside information (BOI21_servers)C11
50 / 100
3587 ms171088 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...