Submission #733465

#TimeUsernameProblemLanguageResultExecution timeMemory
733465rainboyTraffickers (RMI18_traffickers)C11
0 / 100
440 ms11972 KiB
#include <stdio.h> #include <stdlib.h> #define N 30000 #define D 20 int *ej[N], eo[N], n; void append(int i, int j) { int o = eo[i]++; if (o >= 2 && (o & o - 1) == 0) ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]); ej[i][o] = j; } int dd[N], pp[N], qq[N]; int dfs1(int p, int i, int d) { int o, s, k_, j_; dd[i] = d, pp[i] = p; s = 1, k_ = 0, j_ = -1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { int k = dfs1(i, j, d + 1); s += k; if (k_ < k) k_ = k, j_ = j; } } qq[i] = j_; return s; } int ta[N], tb[N]; void dfs2(int p, int i, int q) { static int time; int o, j_; ta[i] = time++; j_ = qq[i], qq[i] = q; if (j_ != -1) dfs2(i, j_, q); for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p && j != j_) dfs2(i, j, j); } tb[i] = time; } int lca(int i, int j) { while (qq[i] != qq[j]) if (dd[qq[i]] > dd[qq[j]]) i = pp[qq[i]]; else j = pp[qq[j]]; return dd[i] < dd[j] ? i : j; } void update(int ft[][D * 2], int i, int j_, int n, int m, int x) { int j; while (i < n) { j = j_; while (j < m) { ft[i][j] += x; j |= j + 1; } i |= i + 1; } } int query(int ft[][D * 2], int i, int j_) { int j, x; x = 0; while (i >= 0) { j = j_; while (j >= 0) { x += ft[i][j]; j &= j + 1, j--; } i &= i + 1, i--; } return x; } int ft[D + 1][N][D * 2]; void update_(int i, int j, int x) { static int qu[D + 1]; int a, d, u, h; a = lca(i, j), d = dd[i] + dd[j] - dd[a] * 2; u = i; while (u != a) qu[dd[i] - dd[u]] = u, u = pp[u]; qu[dd[i] - dd[a]] = a; u = j; while (u != a) qu[dd[i] - dd[a] * 2 + dd[u]] = u, u = pp[u]; if (d == 0) { update(ft[1], ta[i], 0, n, 2, x), update(ft[1], tb[i], 0, n, 2, -x); update(ft[1], ta[i], 1, n, 2, x), update(ft[1], tb[i], 1, n, 2, -x); } else for (h = 0; h <= d; h++) { i = qu[h]; update(ft[1], ta[i], h, n, d * 2, x), update(ft[1], tb[i], h, n, d * 2, -x); if (h > 0 && h < d) update(ft[1], ta[i], d * 2 - h, n, d * 2, x), update(ft[1], tb[i], d * 2 - h, n, d * 2, -x); } } long long query_(int i, int j, int t) { int a, d, r; long long x; a = lca(i, j); x = 0; for (d = 1; d <= D; d++) { x += (long long) (query(ft[d], ta[i], d * 2 - 1) + query(ft[d], ta[j], d * 2 - 1) - query(ft[d], ta[a], d * 2 - 1) - (pp[a] == -1 ? 0 : query(ft[d], ta[pp[a]], d * 2 - 1))) * (t / (d * 2)); r = t % (d * 2); x += (query(ft[d], ta[i], r - 1) + query(ft[d], ta[j], r - 1) - query(ft[d], ta[a], r - 1) - (pp[a] == -1 ? 0 : query(ft[d], ta[pp[a]], r - 1))); } return x; } int main() { int k, q, h, i, j; scanf("%d", &n); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); for (h = 0; h < n - 1; h++) { scanf("%d%d", &i, &j), i--, j--; append(i, j), append(j, i); } dfs1(-1, 0, 0); dfs2(-1, 0, 0); scanf("%d", &k); while (k--) { scanf("%d%d", &i, &j), i--, j--; update_(i, j, 1); } scanf("%d", &q); while (q--) { int t, l, r; scanf("%d%d%d", &t, &i, &j), i--, j--; if (t == 1) update_(i, j, 1); else if (t == 2) update_(i, j, -1); else { scanf("%d%d", &l, &r), r++; printf("%lld\n", query_(i, j, r) - query_(i, j, l)); } } return 0; }

Compilation message (stderr)

traffickers.c: In function 'append':
traffickers.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
traffickers.c: In function 'main':
traffickers.c:138:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
traffickers.c:142:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
traffickers.c:147:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |  scanf("%d", &k);
      |  ^~~~~~~~~~~~~~~
traffickers.c:149:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
traffickers.c:152:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
traffickers.c:156:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |   scanf("%d%d%d", &t, &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
traffickers.c:162:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |    scanf("%d%d", &l, &r), r++;
      |    ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...