Submission #624693

# Submission time Handle Problem Language Result Execution time Memory
624693 2022-08-08T15:44:13 Z MilosMilutinovic Klasika (COCI20_klasika) C++14
33 / 110
1660 ms 524288 KB
/**
 *    author:  wxhtzdy
 *    created: 08.08.2022 17:17:05
**/
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200005 * 30;

set<int> ids[MAXN];
int trie[MAXN][2];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int q;
  cin >> q;
  vector<string> op(q);
  vector<int> x(q);
  vector<int> y(q);
  int n = 1;
  vector<vector<pair<int, int>>> g(1);
  for (int i = 0; i < q; i++) {
    cin >> op[i] >> x[i] >> y[i];
    if (op[i] == "Add") {
      --x[i];
      g.push_back({});
      g[x[i]].emplace_back(n, y[i]);  
      n += 1;
    } else {
      --x[i]; --y[i];
    }
  }
  vector<int> dep(n);
  vector<int> tin(n);
  vector<int> tout(n);
  int T = -1;
  function<void(int, int)> Dfs = [&](int v, int pr) {
    tin[v] = ++T;
    for (auto& p : g[v]) {
      int u = p.first;
      int w = p.second;
      if (u != pr) {
        dep[u] = dep[v] ^ w;
        Dfs(u, v);
      }
    }
    tout[v] = T;
  };
  Dfs(0, 0);
  int idx = 1;
  for (int i = 0; i < MAXN; i++) {
    trie[i][0] = trie[i][1] = -1;
  }
  int ii = 1;
  auto Add = [&](int v, int id) {
    int p = 0;
    for (int i = 30; i >= 0; i--) {
      int bit = (v >> i & 1);
      if (trie[p][bit] == -1) {
        trie[p][bit] = ii++;
      }    
      p = trie[p][bit];
      ids[p].insert(id);     
    }
  };
  Add(dep[0], tin[0]);
  for (int i = 0; i < q; i++) {
    if (op[i] == "Add") {
      Add(dep[idx], tin[idx]);
      idx += 1;  
    } else {
      int p = 0;
      int ans = 0;
      for (int j = 30; j >= 0; j--) {
        int bit = (dep[x[i]] >> j & 1);
        if (trie[p][bit ^ 1] != -1) {
          auto it = ids[trie[p][bit ^ 1]].lower_bound(tin[y[i]]);
          if (it != ids[trie[p][bit ^ 1]].end() && *it <= tout[y[i]]) {
            ans += (1 << j);
            p = trie[p][bit ^ 1];
          } else {
            p = trie[p][bit];
          }
        } else {
          p = trie[p][bit];
        }
      }
      cout << ans << '\n';
    }  
  }                         
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 156 ms 329100 KB Output is correct
2 Correct 138 ms 329180 KB Output is correct
3 Correct 162 ms 329168 KB Output is correct
4 Correct 147 ms 329220 KB Output is correct
5 Correct 182 ms 329204 KB Output is correct
6 Correct 140 ms 329320 KB Output is correct
7 Correct 155 ms 329200 KB Output is correct
8 Correct 146 ms 329336 KB Output is correct
9 Correct 138 ms 329044 KB Output is correct
10 Correct 142 ms 329224 KB Output is correct
11 Correct 153 ms 329228 KB Output is correct
12 Correct 153 ms 329296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 329100 KB Output is correct
2 Correct 138 ms 329180 KB Output is correct
3 Correct 162 ms 329168 KB Output is correct
4 Correct 147 ms 329220 KB Output is correct
5 Correct 182 ms 329204 KB Output is correct
6 Correct 140 ms 329320 KB Output is correct
7 Correct 155 ms 329200 KB Output is correct
8 Correct 146 ms 329336 KB Output is correct
9 Correct 138 ms 329044 KB Output is correct
10 Correct 142 ms 329224 KB Output is correct
11 Correct 153 ms 329228 KB Output is correct
12 Correct 153 ms 329296 KB Output is correct
13 Correct 140 ms 329812 KB Output is correct
14 Correct 139 ms 330416 KB Output is correct
15 Correct 165 ms 331212 KB Output is correct
16 Correct 165 ms 331772 KB Output is correct
17 Correct 159 ms 329748 KB Output is correct
18 Correct 151 ms 330316 KB Output is correct
19 Correct 169 ms 330968 KB Output is correct
20 Correct 177 ms 331584 KB Output is correct
21 Correct 155 ms 329776 KB Output is correct
22 Correct 154 ms 330388 KB Output is correct
23 Correct 170 ms 331044 KB Output is correct
24 Correct 157 ms 331508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 942 ms 405748 KB Output is correct
2 Correct 1419 ms 470628 KB Output is correct
3 Runtime error 1660 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 156 ms 329100 KB Output is correct
2 Correct 138 ms 329180 KB Output is correct
3 Correct 162 ms 329168 KB Output is correct
4 Correct 147 ms 329220 KB Output is correct
5 Correct 182 ms 329204 KB Output is correct
6 Correct 140 ms 329320 KB Output is correct
7 Correct 155 ms 329200 KB Output is correct
8 Correct 146 ms 329336 KB Output is correct
9 Correct 138 ms 329044 KB Output is correct
10 Correct 142 ms 329224 KB Output is correct
11 Correct 153 ms 329228 KB Output is correct
12 Correct 153 ms 329296 KB Output is correct
13 Correct 140 ms 329812 KB Output is correct
14 Correct 139 ms 330416 KB Output is correct
15 Correct 165 ms 331212 KB Output is correct
16 Correct 165 ms 331772 KB Output is correct
17 Correct 159 ms 329748 KB Output is correct
18 Correct 151 ms 330316 KB Output is correct
19 Correct 169 ms 330968 KB Output is correct
20 Correct 177 ms 331584 KB Output is correct
21 Correct 155 ms 329776 KB Output is correct
22 Correct 154 ms 330388 KB Output is correct
23 Correct 170 ms 331044 KB Output is correct
24 Correct 157 ms 331508 KB Output is correct
25 Correct 942 ms 405748 KB Output is correct
26 Correct 1419 ms 470628 KB Output is correct
27 Runtime error 1660 ms 524288 KB Execution killed with signal 9
28 Halted 0 ms 0 KB -