답안 #844581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844581 2023-09-05T14:16:58 Z vjudge1 Klasika (COCI20_klasika) C++17
110 / 110
2028 ms 422612 KB
// author: erray
#include <bits/stdc++.h>

#ifdef DEBUG
  #include "debug.h"
#else
  #define debug(...) void(37)
#endif

using namespace std;

mt19937 rng((uint32_t) chrono::steady_clock::now().time_since_epoch().count());

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int Q;
  cin >> Q;
  vector<vector<int>> g(1);
  vector<int> V(1, 0);
  vector<array<int, 2>> E(Q);
  for (int i = 0; i < Q; ++i) {
    string T;
    int X, Y;
    cin >> T >> X >> Y;
    if (T == "Add") {
      --X;
      int v = int(g.size());
      V.push_back(V[X] ^ Y);
      g.emplace_back(); 
      g[X].push_back(v);
      E[i] = {-1, v};
    } else {
      --X, --Y;
      E[i] = {X, Y};     
    }
  }
  int N = int(g.size());
  vector<int> tin(N), tout(N);
  int t = 0;
  function<void(int)> Dfs = [&](int v) {
    tin[v] = t++;
    for (auto u : g[v]) {
      Dfs(u);
    }
    tout[v] = t - 1;
  };
  Dfs(0);
  debug(tin, tout, E, V);
  struct node {
    set<int> ids;
    array<node*, 2> to = {NULL, NULL};
  };
  auto Exists = [&](node* v, int l, int r) {
    return (v != NULL && (v->ids.lower_bound(l) != v->ids.lower_bound(r + 1)));
  };
  const int BIT = 30;
  node* trie = new node();

  auto Add = [&](int x) {
    int id = tin[x];
    int add = V[x];     
    node* cur = trie;
    for (int b = BIT - 1; b >= 0; --b) {
      int go = (add >> b) % 2;
      if (cur->to[go] == NULL) {
        cur->to[go] = new node(); 
      }
      cur = cur->to[go];
      cur->ids.insert(id);
    }
  };
  Add(0);
  for (int i = 0; i < Q; ++i) {
    debug(i);
    if (E[i][0] == -1) {
      Add(E[i][1]);              
    } else {
      node* cur = trie;
      auto[A, B] = E[i];
      int other = 0;
      for (int b = BIT - 1; b >= 0; --b) {
        int go = 1 ^ ((V[A] >> b) % 2);
        if (!Exists(cur->to[go], tin[B], tout[B])) {
          go ^= 1;  
        }  
        cur = cur->to[go];
        other |= go << b;
      }
      cout << (other ^ V[A]) << '\n';
    }
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 896 KB Output is correct
4 Correct 1 ms 856 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 1112 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 896 KB Output is correct
4 Correct 1 ms 856 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 1112 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
13 Correct 3 ms 1628 KB Output is correct
14 Correct 4 ms 2908 KB Output is correct
15 Correct 4 ms 4188 KB Output is correct
16 Correct 5 ms 5300 KB Output is correct
17 Correct 3 ms 1628 KB Output is correct
18 Correct 4 ms 2808 KB Output is correct
19 Correct 5 ms 4188 KB Output is correct
20 Correct 5 ms 5212 KB Output is correct
21 Correct 3 ms 1692 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
23 Correct 6 ms 4188 KB Output is correct
24 Correct 5 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 662 ms 114220 KB Output is correct
2 Correct 1020 ms 220160 KB Output is correct
3 Correct 1341 ms 319832 KB Output is correct
4 Correct 1608 ms 420932 KB Output is correct
5 Correct 573 ms 111368 KB Output is correct
6 Correct 1004 ms 213404 KB Output is correct
7 Correct 1436 ms 311612 KB Output is correct
8 Correct 1778 ms 410628 KB Output is correct
9 Correct 660 ms 111196 KB Output is correct
10 Correct 948 ms 214680 KB Output is correct
11 Correct 1334 ms 314364 KB Output is correct
12 Correct 1692 ms 412004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 896 KB Output is correct
4 Correct 1 ms 856 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 1112 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
13 Correct 3 ms 1628 KB Output is correct
14 Correct 4 ms 2908 KB Output is correct
15 Correct 4 ms 4188 KB Output is correct
16 Correct 5 ms 5300 KB Output is correct
17 Correct 3 ms 1628 KB Output is correct
18 Correct 4 ms 2808 KB Output is correct
19 Correct 5 ms 4188 KB Output is correct
20 Correct 5 ms 5212 KB Output is correct
21 Correct 3 ms 1692 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
23 Correct 6 ms 4188 KB Output is correct
24 Correct 5 ms 5212 KB Output is correct
25 Correct 662 ms 114220 KB Output is correct
26 Correct 1020 ms 220160 KB Output is correct
27 Correct 1341 ms 319832 KB Output is correct
28 Correct 1608 ms 420932 KB Output is correct
29 Correct 573 ms 111368 KB Output is correct
30 Correct 1004 ms 213404 KB Output is correct
31 Correct 1436 ms 311612 KB Output is correct
32 Correct 1778 ms 410628 KB Output is correct
33 Correct 660 ms 111196 KB Output is correct
34 Correct 948 ms 214680 KB Output is correct
35 Correct 1334 ms 314364 KB Output is correct
36 Correct 1692 ms 412004 KB Output is correct
37 Correct 1141 ms 114568 KB Output is correct
38 Correct 1661 ms 219780 KB Output is correct
39 Correct 1872 ms 322392 KB Output is correct
40 Correct 1951 ms 422612 KB Output is correct
41 Correct 1062 ms 111336 KB Output is correct
42 Correct 1524 ms 213180 KB Output is correct
43 Correct 1840 ms 311108 KB Output is correct
44 Correct 2028 ms 408984 KB Output is correct
45 Correct 1218 ms 111104 KB Output is correct
46 Correct 1640 ms 214248 KB Output is correct
47 Correct 1950 ms 313108 KB Output is correct
48 Correct 2025 ms 413764 KB Output is correct