Submission #1109099

# Submission time Handle Problem Language Result Execution time Memory
1109099 2024-11-06T03:12:20 Z ind1v Klasika (COCI20_klasika) C++11
0 / 110
2764 ms 85792 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;
  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[lca(rev[j], 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[lca(rev[j], 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[lca(rev[j], x[i])] ^ f[rev[j]]);
          }
        }
      }
      cout << res << '\n';
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 72528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 72528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2764 ms 85792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 72528 KB Output isn't correct
2 Halted 0 ms 0 KB -