답안 #770011

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770011 2023-06-30T16:33:13 Z vjudge1 Klasika (COCI20_klasika) C++17
66 / 110
5000 ms 176896 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif

const int lg = 31;

struct Trie {
  public:
  vector<vector<int>> trie;


  Trie() {
    trie.push_back(vector<int> (2, -1));
  };

  void add(int x) {
    int t = 0;
    for (int i = lg - 1; i >= 0; i--) {
      int bit = (x >> i) & 1;

      if (trie[t][bit] == -1) {
        trie[t][bit] = (int) trie.size();
        trie.emplace_back(2, -1);
      }
      t = trie[t][bit];
    }
  }

  int get(int x) {
    int t = 0;
    int ret = 0;

    for (int i = lg - 1; i >= 0; i--) {
      int bit = (x >> i) & 1;
      bit ^= 1;

      if (trie[t][bit] == -1) {
        bit ^= 1;
      } else {
        ret |= (1 << i);
      }

      if (trie[t][bit] == -1) {
        return 0;
      }
      t = trie[t][bit];
    }
    return ret;
  };
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int q;
  cin >> q;

  vector<vector<int>> adj(q + 1);
  vector<int> pref(q + 1);

  Trie t;
  t.add(0);

  auto Add = [&](int x, int y, int w) {
    adj[x].push_back(y);
    pref[y] = pref[x] ^ w;
    t.add(pref[y]);
  };

  int cnt = 1;

  for (int i = 0; i < q; i++) {
    string type;
    cin >> type;

    if (type == "Add") {
      int x, w;
      cin >> x >> w;
      --x;
      Add(x, cnt, w);
      ++cnt;
    } else {
      int a, b;
      cin >> a >> b;
      --a, --b;

      if (b == 0) {
        int res = t.get(pref[a]);
        cout << res << '\n';
      } else {
        int res = 0;
        
        function<void(int, bool)> Dfs = [&](int u, bool insideSubtree) {
          bool isOk = insideSubtree;
          if (u == b) {
            isOk = true;
          }

          if (isOk) {
            res = max(res, pref[a] ^ pref[u]);
          }
          for (auto v : adj[u]) {
            Dfs(v, isOk);
          }
        };

        Dfs(0, false);
        cout << res << '\n';
      }
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 5 ms 980 KB Output is correct
14 Correct 7 ms 1680 KB Output is correct
15 Correct 8 ms 1804 KB Output is correct
16 Correct 7 ms 3084 KB Output is correct
17 Correct 4 ms 988 KB Output is correct
18 Correct 5 ms 1680 KB Output is correct
19 Correct 6 ms 1756 KB Output is correct
20 Correct 6 ms 2188 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 9 ms 1680 KB Output is correct
23 Correct 7 ms 1804 KB Output is correct
24 Correct 5 ms 2188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 49248 KB Output is correct
2 Correct 269 ms 92600 KB Output is correct
3 Correct 297 ms 105392 KB Output is correct
4 Correct 357 ms 176896 KB Output is correct
5 Correct 213 ms 50136 KB Output is correct
6 Correct 304 ms 91724 KB Output is correct
7 Correct 342 ms 103996 KB Output is correct
8 Correct 397 ms 174728 KB Output is correct
9 Correct 225 ms 50456 KB Output is correct
10 Correct 273 ms 91800 KB Output is correct
11 Correct 314 ms 104848 KB Output is correct
12 Correct 364 ms 175468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 5 ms 980 KB Output is correct
14 Correct 7 ms 1680 KB Output is correct
15 Correct 8 ms 1804 KB Output is correct
16 Correct 7 ms 3084 KB Output is correct
17 Correct 4 ms 988 KB Output is correct
18 Correct 5 ms 1680 KB Output is correct
19 Correct 6 ms 1756 KB Output is correct
20 Correct 6 ms 2188 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 9 ms 1680 KB Output is correct
23 Correct 7 ms 1804 KB Output is correct
24 Correct 5 ms 2188 KB Output is correct
25 Correct 185 ms 49248 KB Output is correct
26 Correct 269 ms 92600 KB Output is correct
27 Correct 297 ms 105392 KB Output is correct
28 Correct 357 ms 176896 KB Output is correct
29 Correct 213 ms 50136 KB Output is correct
30 Correct 304 ms 91724 KB Output is correct
31 Correct 342 ms 103996 KB Output is correct
32 Correct 397 ms 174728 KB Output is correct
33 Correct 225 ms 50456 KB Output is correct
34 Correct 273 ms 91800 KB Output is correct
35 Correct 314 ms 104848 KB Output is correct
36 Correct 364 ms 175468 KB Output is correct
37 Execution timed out 5035 ms 19628 KB Time limit exceeded
38 Halted 0 ms 0 KB -