Submission #770162

# Submission time Handle Problem Language Result Execution time Memory
770162 2023-06-30T21:29:45 Z taher Klasika (COCI20_klasika) C++17
110 / 110
2249 ms 431452 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 340 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 596 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 340 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 596 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 4308 KB Output is correct
16 Correct 6 ms 5460 KB Output is correct
17 Correct 3 ms 1620 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 5 ms 4180 KB Output is correct
20 Correct 6 ms 5332 KB Output is correct
21 Correct 4 ms 1620 KB Output is correct
22 Correct 5 ms 2900 KB Output is correct
23 Correct 5 ms 4180 KB Output is correct
24 Correct 6 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 562 ms 122540 KB Output is correct
2 Correct 910 ms 228152 KB Output is correct
3 Correct 1243 ms 329480 KB Output is correct
4 Correct 1590 ms 431184 KB Output is correct
5 Correct 530 ms 119788 KB Output is correct
6 Correct 1040 ms 222456 KB Output is correct
7 Correct 1581 ms 320928 KB Output is correct
8 Correct 1846 ms 419476 KB Output is correct
9 Correct 548 ms 119480 KB Output is correct
10 Correct 917 ms 223712 KB Output is correct
11 Correct 1225 ms 323868 KB Output is correct
12 Correct 1574 ms 422148 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 340 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 596 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 4308 KB Output is correct
16 Correct 6 ms 5460 KB Output is correct
17 Correct 3 ms 1620 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 5 ms 4180 KB Output is correct
20 Correct 6 ms 5332 KB Output is correct
21 Correct 4 ms 1620 KB Output is correct
22 Correct 5 ms 2900 KB Output is correct
23 Correct 5 ms 4180 KB Output is correct
24 Correct 6 ms 5332 KB Output is correct
25 Correct 562 ms 122540 KB Output is correct
26 Correct 910 ms 228152 KB Output is correct
27 Correct 1243 ms 329480 KB Output is correct
28 Correct 1590 ms 431184 KB Output is correct
29 Correct 530 ms 119788 KB Output is correct
30 Correct 1040 ms 222456 KB Output is correct
31 Correct 1581 ms 320928 KB Output is correct
32 Correct 1846 ms 419476 KB Output is correct
33 Correct 548 ms 119480 KB Output is correct
34 Correct 917 ms 223712 KB Output is correct
35 Correct 1225 ms 323868 KB Output is correct
36 Correct 1574 ms 422148 KB Output is correct
37 Correct 1099 ms 123164 KB Output is correct
38 Correct 1499 ms 227808 KB Output is correct
39 Correct 1788 ms 331808 KB Output is correct
40 Correct 1940 ms 431452 KB Output is correct
41 Correct 1077 ms 119768 KB Output is correct
42 Correct 1572 ms 222032 KB Output is correct
43 Correct 1871 ms 320848 KB Output is correct
44 Correct 2109 ms 418556 KB Output is correct
45 Correct 1304 ms 119480 KB Output is correct
46 Correct 1832 ms 223316 KB Output is correct
47 Correct 2105 ms 322260 KB Output is correct
48 Correct 2249 ms 422036 KB Output is correct