Submission #1042523

# Submission time Handle Problem Language Result Execution time Memory
1042523 2024-08-03T06:13:01 Z sleepntsheep Inside information (BOI21_servers) C
52.5 / 100
3500 ms 171344 KB
#include <stdio.h>
#include <stdlib.h>

int max(int i, int j) { return i > j ? i : j; }
/*
 * For "Q" queries, use LCA + prefix sum on tree ~ 3O(lgn) [for finding lca and almost-lca]
 *
 * Every B queries of any type, do naive dp on tree (reverse edge order)
 * to find answer for "C" queries  ~ O(n)
 *
 * On "C" queries, use cached answer from above and count all edges that appeared after that (by "Q")
 * ~ O(B lgn)
 *
 *
 * Time complexity = O(N^2 / B) + O(N lgn) + O(N B lgn)
 *
 * Let B = sqrt(N / lgN)
 *
 * Should be able to optimize to O(N^1.5) using https://codeforces.com/blog/entry/71567?mobile=true&locale=en 
 */

#define N 120001
#define B 100
#define M 30000000

int n, q, L[M], R[M], A[M], alc, root[N], temp;
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 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);

  q = n + q - 1;
  for (int j = 0, i = 0; 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) {
        int p = pqry(root[a], 1, n, ++at);
        root[b] = pupd(root[b], 1, n, p);
      }
      root[a] = root[b];
    } else if ('Q' == type[0]) {
      scanf("%d", &b);
      puts(ppt(root[a], 1, n, b) ? "yes" : "no");
    } else {
      int z=0;for(int i=1;i<=n;++i)z+=ppt(root[i],1,n,a);
      printf("%d\n", z);
    }
  }
}

Compilation message

servers.c: In function 'main':
servers.c:61:12: warning: unused variable 'j' [-Wunused-variable]
   61 |   for (int j = 0, i = 0; i < q; ++i) {
      |            ^
servers.c:54:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |   scanf("%d%d", &n, &q);
      |   ^~~~~~~~~~~~~~~~~~~~~
servers.c:64:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     scanf(" %s%d", type, &a);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
servers.c:66:7: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |       scanf("%d", &b);
      |       ^~~~~~~~~~~~~~~
servers.c:76:7: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |       scanf("%d", &b);
      |       ^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 4952 KB Output is correct
2 Correct 31 ms 7252 KB Output is correct
3 Correct 29 ms 7512 KB Output is correct
4 Correct 25 ms 7256 KB Output is correct
5 Correct 27 ms 7260 KB Output is correct
6 Correct 28 ms 7256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 4952 KB Output is correct
2 Correct 31 ms 7252 KB Output is correct
3 Correct 29 ms 7512 KB Output is correct
4 Correct 25 ms 7256 KB Output is correct
5 Correct 27 ms 7260 KB Output is correct
6 Correct 28 ms 7256 KB Output is correct
7 Correct 19 ms 4832 KB Output is correct
8 Correct 2995 ms 7348 KB Output is correct
9 Correct 3267 ms 7508 KB Output is correct
10 Correct 2994 ms 7344 KB Output is correct
11 Correct 2769 ms 7300 KB Output is correct
12 Correct 2832 ms 7344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4764 KB Output is correct
2 Correct 199 ms 59296 KB Output is correct
3 Correct 172 ms 56404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4764 KB Output is correct
2 Correct 199 ms 59296 KB Output is correct
3 Correct 172 ms 56404 KB Output is correct
4 Correct 18 ms 4696 KB Output is correct
5 Execution timed out 3550 ms 32688 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4700 KB Output is correct
2 Correct 177 ms 66256 KB Output is correct
3 Correct 156 ms 66388 KB Output is correct
4 Correct 128 ms 56916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4700 KB Output is correct
2 Correct 177 ms 66256 KB Output is correct
3 Correct 156 ms 66388 KB Output is correct
4 Correct 128 ms 56916 KB Output is correct
5 Correct 18 ms 4696 KB Output is correct
6 Execution timed out 3600 ms 31568 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4696 KB Output is correct
2 Correct 234 ms 145968 KB Output is correct
3 Correct 207 ms 69020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4696 KB Output is correct
2 Correct 234 ms 145968 KB Output is correct
3 Correct 207 ms 69020 KB Output is correct
4 Correct 17 ms 4744 KB Output is correct
5 Execution timed out 3594 ms 32296 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 4700 KB Output is correct
2 Correct 166 ms 66388 KB Output is correct
3 Correct 169 ms 66436 KB Output is correct
4 Correct 108 ms 56792 KB Output is correct
5 Correct 19 ms 4700 KB Output is correct
6 Correct 242 ms 146004 KB Output is correct
7 Correct 199 ms 68948 KB Output is correct
8 Correct 232 ms 69896 KB Output is correct
9 Correct 193 ms 69740 KB Output is correct
10 Correct 219 ms 78676 KB Output is correct
11 Correct 273 ms 80472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 4700 KB Output is correct
2 Correct 166 ms 66388 KB Output is correct
3 Correct 169 ms 66436 KB Output is correct
4 Correct 108 ms 56792 KB Output is correct
5 Correct 19 ms 4700 KB Output is correct
6 Correct 242 ms 146004 KB Output is correct
7 Correct 199 ms 68948 KB Output is correct
8 Correct 232 ms 69896 KB Output is correct
9 Correct 193 ms 69740 KB Output is correct
10 Correct 219 ms 78676 KB Output is correct
11 Correct 273 ms 80472 KB Output is correct
12 Correct 18 ms 4696 KB Output is correct
13 Execution timed out 3514 ms 31688 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 4700 KB Output is correct
2 Correct 29 ms 7260 KB Output is correct
3 Correct 27 ms 7468 KB Output is correct
4 Correct 26 ms 7260 KB Output is correct
5 Correct 27 ms 7256 KB Output is correct
6 Correct 29 ms 7260 KB Output is correct
7 Correct 16 ms 4696 KB Output is correct
8 Correct 190 ms 56524 KB Output is correct
9 Correct 185 ms 56404 KB Output is correct
10 Correct 16 ms 4696 KB Output is correct
11 Correct 177 ms 66404 KB Output is correct
12 Correct 163 ms 66388 KB Output is correct
13 Correct 104 ms 56916 KB Output is correct
14 Correct 16 ms 4700 KB Output is correct
15 Correct 234 ms 146080 KB Output is correct
16 Correct 198 ms 68944 KB Output is correct
17 Correct 214 ms 69716 KB Output is correct
18 Correct 205 ms 69968 KB Output is correct
19 Correct 238 ms 78584 KB Output is correct
20 Correct 267 ms 80388 KB Output is correct
21 Correct 223 ms 60496 KB Output is correct
22 Correct 220 ms 68436 KB Output is correct
23 Correct 215 ms 72272 KB Output is correct
24 Correct 220 ms 71888 KB Output is correct
25 Correct 186 ms 61520 KB Output is correct
26 Correct 388 ms 161404 KB Output is correct
27 Correct 399 ms 171344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 4700 KB Output is correct
2 Correct 29 ms 7260 KB Output is correct
3 Correct 27 ms 7468 KB Output is correct
4 Correct 26 ms 7260 KB Output is correct
5 Correct 27 ms 7256 KB Output is correct
6 Correct 29 ms 7260 KB Output is correct
7 Correct 16 ms 4696 KB Output is correct
8 Correct 190 ms 56524 KB Output is correct
9 Correct 185 ms 56404 KB Output is correct
10 Correct 16 ms 4696 KB Output is correct
11 Correct 177 ms 66404 KB Output is correct
12 Correct 163 ms 66388 KB Output is correct
13 Correct 104 ms 56916 KB Output is correct
14 Correct 16 ms 4700 KB Output is correct
15 Correct 234 ms 146080 KB Output is correct
16 Correct 198 ms 68944 KB Output is correct
17 Correct 214 ms 69716 KB Output is correct
18 Correct 205 ms 69968 KB Output is correct
19 Correct 238 ms 78584 KB Output is correct
20 Correct 267 ms 80388 KB Output is correct
21 Correct 223 ms 60496 KB Output is correct
22 Correct 220 ms 68436 KB Output is correct
23 Correct 215 ms 72272 KB Output is correct
24 Correct 220 ms 71888 KB Output is correct
25 Correct 186 ms 61520 KB Output is correct
26 Correct 388 ms 161404 KB Output is correct
27 Correct 399 ms 171344 KB Output is correct
28 Correct 17 ms 4732 KB Output is correct
29 Correct 3031 ms 7344 KB Output is correct
30 Correct 3240 ms 7500 KB Output is correct
31 Correct 2990 ms 7336 KB Output is correct
32 Correct 2837 ms 7164 KB Output is correct
33 Correct 2883 ms 7504 KB Output is correct
34 Correct 17 ms 4696 KB Output is correct
35 Execution timed out 3537 ms 32596 KB Time limit exceeded
36 Halted 0 ms 0 KB -