Submission #770114

# Submission time Handle Problem Language Result Execution time Memory
770114 2023-06-30T19:00:27 Z vjudge1 Klasika (COCI20_klasika) C++17
110 / 110
2131 ms 435044 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 {
  Trie* nxt[2];
  set<int> st;

  Trie() {
    nxt[0] = nullptr;
    nxt[1] = nullptr;
  }
};

Trie *root;

void add(int x, int ins) {
  Trie *t = root;

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

    if (t->nxt[bit] == nullptr) {
      t->nxt[bit] = new Trie();
    }
    t = t->nxt[bit];
    t->st.insert(ins);
  }
}

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

  auto isBad = [&](int bit) {
    if (t->nxt[bit] == nullptr) {
      return true;
    }
    Trie *p = t;
    p = t->nxt[bit];
    if (p->st.empty()) {
      return true;
    }
    auto ptr = p->st.lower_bound(low);
    if (ptr != p->st.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 = t->nxt[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);

  root = new Trie();
  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];

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

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

      cout << get(pref[a], in[b], out[b]) << '\n';
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 3 ms 1748 KB Output is correct
14 Correct 4 ms 3028 KB Output is correct
15 Correct 5 ms 4320 KB Output is correct
16 Correct 5 ms 5460 KB Output is correct
17 Correct 3 ms 1620 KB Output is correct
18 Correct 4 ms 2968 KB Output is correct
19 Correct 5 ms 4180 KB Output is correct
20 Correct 6 ms 5332 KB Output is correct
21 Correct 3 ms 1620 KB Output is correct
22 Correct 4 ms 2956 KB Output is correct
23 Correct 6 ms 4180 KB Output is correct
24 Correct 7 ms 5252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 511 ms 122532 KB Output is correct
2 Correct 879 ms 228068 KB Output is correct
3 Correct 1296 ms 329324 KB Output is correct
4 Correct 1581 ms 435044 KB Output is correct
5 Correct 596 ms 122532 KB Output is correct
6 Correct 984 ms 225720 KB Output is correct
7 Correct 1415 ms 324320 KB Output is correct
8 Correct 1885 ms 423052 KB Output is correct
9 Correct 533 ms 122332 KB Output is correct
10 Correct 917 ms 226936 KB Output is correct
11 Correct 1267 ms 327448 KB Output is correct
12 Correct 1537 ms 425892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 3 ms 1748 KB Output is correct
14 Correct 4 ms 3028 KB Output is correct
15 Correct 5 ms 4320 KB Output is correct
16 Correct 5 ms 5460 KB Output is correct
17 Correct 3 ms 1620 KB Output is correct
18 Correct 4 ms 2968 KB Output is correct
19 Correct 5 ms 4180 KB Output is correct
20 Correct 6 ms 5332 KB Output is correct
21 Correct 3 ms 1620 KB Output is correct
22 Correct 4 ms 2956 KB Output is correct
23 Correct 6 ms 4180 KB Output is correct
24 Correct 7 ms 5252 KB Output is correct
25 Correct 511 ms 122532 KB Output is correct
26 Correct 879 ms 228068 KB Output is correct
27 Correct 1296 ms 329324 KB Output is correct
28 Correct 1581 ms 435044 KB Output is correct
29 Correct 596 ms 122532 KB Output is correct
30 Correct 984 ms 225720 KB Output is correct
31 Correct 1415 ms 324320 KB Output is correct
32 Correct 1885 ms 423052 KB Output is correct
33 Correct 533 ms 122332 KB Output is correct
34 Correct 917 ms 226936 KB Output is correct
35 Correct 1267 ms 327448 KB Output is correct
36 Correct 1537 ms 425892 KB Output is correct
37 Correct 1150 ms 124716 KB Output is correct
38 Correct 1476 ms 230208 KB Output is correct
39 Correct 1764 ms 334144 KB Output is correct
40 Correct 1901 ms 433892 KB Output is correct
41 Correct 1066 ms 122104 KB Output is correct
42 Correct 1541 ms 225532 KB Output is correct
43 Correct 1826 ms 324572 KB Output is correct
44 Correct 2131 ms 422460 KB Output is correct
45 Correct 1256 ms 122880 KB Output is correct
46 Correct 1763 ms 226956 KB Output is correct
47 Correct 1976 ms 326012 KB Output is correct
48 Correct 2063 ms 425900 KB Output is correct