답안 #770109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770109 2023-06-30T18:52:46 Z vjudge1 Klasika (COCI20_klasika) C++17
33 / 110
2304 ms 524288 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;
  vector<set<int>> st;

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

  void add(int x, int ins) {
    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);
        st.push_back(set<int> ());
        st.push_back(set<int> ());
      }
      t = trie[t][bit];
      st[t].insert(ins);
    }
  }

  int get(int x, int low, int high) {
    int t = 0;
    int ret = 0;

    auto isBad = [&](int bit) {
      if (trie[t][bit] == -1) {
        return true;
      }
      if (st[trie[t][bit]].empty()) {
        return true;
      }
      auto ptr = st[trie[t][bit]].lower_bound(low);
      if (ptr != st[trie[t][bit]].end() && *ptr <= high) {
        return false;
      }
      return true;
    };

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

      if (isBad(bit)) {
        bit ^= 1;
      } else {
        ret |= (1 << i);
      }

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

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

  int q;
  cin >> q;

  int n = q + 1;

  vector<vector<int>> adj(n);
  vector<int> pref(n);

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

  int cnt = 1;

  vector<array<int, 3>> queries;

  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);
      queries.push_back({1, cnt, 0});
      ++cnt;

    } else {
      int a, b;
      cin >> a >> b;
      --a, --b;

      queries.push_back({2, a, b});
    }
  }

  vector<int> in(n);
  vector<int> out(n);
  vector<int> euler_tour;

  function<void(int)> Dfs = [&](int u) {
    in[u] = (int) euler_tour.size();
    euler_tour.push_back(u);

    for (auto v : adj[u]) {
      Dfs(v);
    }

    out[u] = (int) euler_tour.size() - 1;
  };

  Dfs(0);

  Trie T;
  T.add(0, 0);

  for (int i = 0; i < (int) queries.size(); i++) {
    int type = queries[i][0];
    
    if (type == 1) {
      int u = queries[i][1];

      T.add(pref[u], in[u]);

    } else {
      int a = queries[i][1];
      int b = queries[i][2];

      cout << T.get(pref[a], in[b], out[b]) << '\n';
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1040 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 1040 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 1040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1040 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 1040 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 1040 KB Output is correct
13 Correct 4 ms 3084 KB Output is correct
14 Correct 7 ms 5896 KB Output is correct
15 Correct 8 ms 6148 KB Output is correct
16 Correct 12 ms 11588 KB Output is correct
17 Correct 4 ms 3084 KB Output is correct
18 Correct 7 ms 5868 KB Output is correct
19 Correct 8 ms 6020 KB Output is correct
20 Correct 12 ms 7540 KB Output is correct
21 Correct 4 ms 3072 KB Output is correct
22 Correct 7 ms 5896 KB Output is correct
23 Correct 8 ms 6020 KB Output is correct
24 Correct 10 ms 7524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 825 ms 201868 KB Output is correct
2 Correct 1621 ms 399308 KB Output is correct
3 Correct 2304 ms 449144 KB Output is correct
4 Runtime error 1937 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1040 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 1040 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 1040 KB Output is correct
13 Correct 4 ms 3084 KB Output is correct
14 Correct 7 ms 5896 KB Output is correct
15 Correct 8 ms 6148 KB Output is correct
16 Correct 12 ms 11588 KB Output is correct
17 Correct 4 ms 3084 KB Output is correct
18 Correct 7 ms 5868 KB Output is correct
19 Correct 8 ms 6020 KB Output is correct
20 Correct 12 ms 7540 KB Output is correct
21 Correct 4 ms 3072 KB Output is correct
22 Correct 7 ms 5896 KB Output is correct
23 Correct 8 ms 6020 KB Output is correct
24 Correct 10 ms 7524 KB Output is correct
25 Correct 825 ms 201868 KB Output is correct
26 Correct 1621 ms 399308 KB Output is correct
27 Correct 2304 ms 449144 KB Output is correct
28 Runtime error 1937 ms 524288 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -