Submission #1109101

# Submission time Handle Problem Language Result Execution time Memory
1109101 2024-11-06T03:21:56 Z ind1v Klasika (COCI20_klasika) C++11
33 / 110
1114 ms 86608 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int L = 20;
const int B = 2500;

int n, q;
vector<pair<int, int>> g[N];
int tin[N], tout[N];
int cur = 1;
string que[N];
int x[N], y[N];
int time_dfs = 0, time_euler = 0;
int fst[N], lv[N], f[N];
int et[2 * N], lg2[2 * N];
pair<int, int> sp[L][2 * N];
int bl[N];
bool on[N];
int rev[N];

struct trie {
  struct node {
    int nxt[2];
    
    node() {
      memset(nxt, -1, sizeof(nxt));
    }
  } t[30 * (B + 1)];
  
  int cur = 1;
  
  void add(int u) {
    int p = 1;
    for (int i = 29; i >= 0; i--) {
      int j = (u >> i & 1);
      if (t[p].nxt[j] == -1) {
        t[p].nxt[j] = ++cur;
      }
      p = t[p].nxt[j];
    }
  }
  
  int get(int u) {
    int p = 1;
    int res = 0;
    for (int i = 29; i >= 0; i--) {
      int j = (u >> i & 1);
      if (t[p].nxt[1 ^ j] != -1) {
        res += (1 << j);
        p = t[p].nxt[1 ^ j];
      } else if (t[p].nxt[j] != -1) {
        p = t[p].nxt[j];
      } else {
        return -1;
      }
    }
    return res;
  }
} tr[N / B + 1];

void dfs(int u) {
  tin[u] = ++time_dfs;
  rev[tin[u]] = u;
  fst[u] = ++time_euler;
  et[time_euler] = u;
  for (pair<int, int> &p : g[u]) {
    lv[p.first] = lv[u] + 1;
    f[p.first] = p.second ^ f[u];
    dfs(p.first);
    et[++time_euler] = u;
  }
  tout[u] = time_dfs;
}

void build() {
  lg2[1] = 0;
  for (int i = 2; i < 2 * N; i++) {
    lg2[i] = lg2[i >> 1] + 1;
  }
  for (int j = 0; j < L; j++) {
    for (int i = 1; i + (1 << j) - 1 <= time_euler; i++) {
      sp[j][i] = (j == 0 ? pair<int, int>{lv[et[i]], et[i]} : min(sp[j - 1][i], sp[j - 1][i + (1 << (j - 1))]));
    }
  }
}

int lca(int u, int v) {
  if (fst[u] > fst[v]) {
    swap(u, v);
  }
  int w = lg2[fst[v] - fst[u] + 1];
  return min(sp[w][fst[u]], sp[w][fst[v] - (1 << w) + 1]).second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> que[i] >> x[i] >> y[i];
    if (que[i] == "Add") {
      cur++;
      g[x[i]].emplace_back(cur, y[i]);
    }
  }
  n = cur;
  dfs(1);
  build();
  for (int i = 1; i <= n; i++) {
    bl[i] = (i + B - 1) / B;
  }
  int cur = 1;
  on[1] = true;
  for (int i = 1; i <= q; i++) {
    if (que[i] == "Add") {
      cur++;
      on[cur] = true;
      tr[bl[tin[cur]]].add(f[cur]);
    } else if (que[i] == "Query") {
      int in = tin[y[i]];
      int out = tout[y[i]];
      int res = 0;
      if (bl[in] == bl[out]) {
        for (int j = in; j <= out; j++) {
          if (on[rev[j]]) {
            res = max(res, f[x[i]] ^ f[rev[j]]);
          }
        }
      } else {
        for (int j = in; j <= min(n, B * bl[in]); j++) {
          if (on[rev[j]]) {
            res = max(res, f[x[i]] ^ f[rev[j]]);
          }
        }
        for (int j = bl[in] + 1; j <= bl[out] - 1; j++) {
          res = max(res, tr[j].get(f[x[i]]));
        }
        for (int j = B * (bl[out] - 1) + 1; j <= out; j++) {
          if (on[rev[j]]) {
            res = max(res, f[x[i]] ^ f[rev[j]]);
          }
        }
      }
      cout << res << '\n';
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 82512 KB Output is correct
2 Correct 16 ms 84560 KB Output is correct
3 Correct 12 ms 84560 KB Output is correct
4 Correct 15 ms 86608 KB Output is correct
5 Correct 13 ms 82648 KB Output is correct
6 Correct 14 ms 84728 KB Output is correct
7 Correct 17 ms 84560 KB Output is correct
8 Correct 12 ms 76624 KB Output is correct
9 Correct 12 ms 78416 KB Output is correct
10 Correct 11 ms 76624 KB Output is correct
11 Correct 11 ms 74576 KB Output is correct
12 Correct 12 ms 76624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 82512 KB Output is correct
2 Correct 16 ms 84560 KB Output is correct
3 Correct 12 ms 84560 KB Output is correct
4 Correct 15 ms 86608 KB Output is correct
5 Correct 13 ms 82648 KB Output is correct
6 Correct 14 ms 84728 KB Output is correct
7 Correct 17 ms 84560 KB Output is correct
8 Correct 12 ms 76624 KB Output is correct
9 Correct 12 ms 78416 KB Output is correct
10 Correct 11 ms 76624 KB Output is correct
11 Correct 11 ms 74576 KB Output is correct
12 Correct 12 ms 76624 KB Output is correct
13 Correct 17 ms 65872 KB Output is correct
14 Correct 14 ms 64336 KB Output is correct
15 Correct 17 ms 80976 KB Output is correct
16 Correct 13 ms 80976 KB Output is correct
17 Correct 13 ms 76624 KB Output is correct
18 Correct 12 ms 76624 KB Output is correct
19 Correct 13 ms 74576 KB Output is correct
20 Correct 12 ms 68176 KB Output is correct
21 Correct 11 ms 64080 KB Output is correct
22 Correct 12 ms 69712 KB Output is correct
23 Correct 12 ms 70552 KB Output is correct
24 Correct 12 ms 66128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1114 ms 83784 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 82512 KB Output is correct
2 Correct 16 ms 84560 KB Output is correct
3 Correct 12 ms 84560 KB Output is correct
4 Correct 15 ms 86608 KB Output is correct
5 Correct 13 ms 82648 KB Output is correct
6 Correct 14 ms 84728 KB Output is correct
7 Correct 17 ms 84560 KB Output is correct
8 Correct 12 ms 76624 KB Output is correct
9 Correct 12 ms 78416 KB Output is correct
10 Correct 11 ms 76624 KB Output is correct
11 Correct 11 ms 74576 KB Output is correct
12 Correct 12 ms 76624 KB Output is correct
13 Correct 17 ms 65872 KB Output is correct
14 Correct 14 ms 64336 KB Output is correct
15 Correct 17 ms 80976 KB Output is correct
16 Correct 13 ms 80976 KB Output is correct
17 Correct 13 ms 76624 KB Output is correct
18 Correct 12 ms 76624 KB Output is correct
19 Correct 13 ms 74576 KB Output is correct
20 Correct 12 ms 68176 KB Output is correct
21 Correct 11 ms 64080 KB Output is correct
22 Correct 12 ms 69712 KB Output is correct
23 Correct 12 ms 70552 KB Output is correct
24 Correct 12 ms 66128 KB Output is correct
25 Incorrect 1114 ms 83784 KB Output isn't correct
26 Halted 0 ms 0 KB -