Submission #1090919

# Submission time Handle Problem Language Result Execution time Memory
1090919 2024-09-19T07:18:32 Z fryingduc Klasika (COCI20_klasika) C++17
33 / 110
740 ms 524288 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif

const int maxn = 2e5 + 5;
const int lg = 19;
int q;
int n;
int tin[maxn], tout[maxn], timer;
int op[maxn], qx[maxn], qy[maxn];
int par[maxn];
vector<pair<int, int>> g[maxn];
int dep[maxn], h[maxn];

void pre_dfs(int u) {
  tin[u] = ++timer;
  for(auto e:g[u]) {
    int v = e.first, w = e.second;
    dep[v] = dep[u] ^ w;
    h[v] = h[u] + 1;
    pre_dfs(v);
  }
  tout[u] = timer;
}
struct trie {
  const static int lg = 30;
  struct node {
    node *child[2];
    bool cnt;
    node() {
      child[0] = child[1] = nullptr;
      cnt = 0;
    }
  } *root;
  trie() {
    root = new node();
  }
  void add(int x) {
    node *p = root;
    for(int i = lg; i >= 0; --i) {
      int c = (x >> i & 1);
      if(p->child[c] == nullptr) {
        p->child[c] = new node();
      }
      p = p->child[c];
      p->cnt = 1;
    }
  }
  int get(int x) {
    node *p = root;
    int ans = 0;
    for(int i = lg; i >= 0; --i) {
      int c = (x >> i & 1);
      if(p->child[c ^ 1] and p->child[c ^ 1]->cnt) {
        ans |= (1 << i);
        p = p->child[c ^ 1];
      }
      else {
        if(p->child[c] == nullptr) {
          return ans;
        }
        p = p->child[c];
      }
    }
    return ans;
  }
} tree[maxn * 4];
struct segment_tree {
  int n;
  segment_tree() {}
  segment_tree(int n) : n(n) {}
  void update(int ind, int l, int r, int pos, int val) {
    tree[ind].add(val);
    if(l == r) {
      return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) {
      update(ind << 1, l, mid, pos, val);
    }
    else {
      update(ind << 1 | 1, mid + 1, r, pos, val);
    }
  }
  int get(int ind, int l, int r, int x, int y, int val) {
    if(l > y || r < x) return 0;
    if(x <= l and r <= y) {
      return tree[ind].get(val);
    }
    int mid = (l + r) >> 1;
    return max(get(ind << 1, l, mid, x, y, val), get(ind << 1 | 1, mid + 1, r, x, y, val));
  }
  void update(int pos, int val) {
    update(1, 1, n, pos, val);
  }
  int get(int x, int y , int val) {
    return get(1, 1, n, x, y, val);
  }
} st;
void solve() {
  cin >> q;
  string s;
  n = 1;
  for(int i = 1; i <= q; ++i) {
    cin >> s >> qx[i] >> qy[i]; 
    if(s[0] == 'Q') {
      op[i] = 1;
    }
    
    if(!op[i]) {
      ++n;
      par[n] = qx[i];
      g[qx[i]].push_back({n, qy[i]});
      qx[i] = n;
    }
  }
  pre_dfs(1);
  st = segment_tree(n);
  st.update(1, 0);
  
  for(int i = 1; i <= q; ++i) {
    if(!op[i]) {
      st.update(tin[qx[i]], dep[qx[i]]);
    }
    else {
      int u = qx[i], v = qy[i];
      debug(tin[v], tout[v], dep[u]);
      cout << st.get(tin[v], tout[v], dep[u]) << '\n';
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  solve();

  return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 23 ms 36676 KB Output is correct
2 Correct 23 ms 36888 KB Output is correct
3 Correct 42 ms 37200 KB Output is correct
4 Correct 24 ms 37460 KB Output is correct
5 Correct 23 ms 36692 KB Output is correct
6 Correct 22 ms 36968 KB Output is correct
7 Correct 41 ms 37200 KB Output is correct
8 Correct 23 ms 37468 KB Output is correct
9 Correct 23 ms 36552 KB Output is correct
10 Correct 25 ms 36956 KB Output is correct
11 Correct 23 ms 37204 KB Output is correct
12 Correct 24 ms 37560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 36676 KB Output is correct
2 Correct 23 ms 36888 KB Output is correct
3 Correct 42 ms 37200 KB Output is correct
4 Correct 24 ms 37460 KB Output is correct
5 Correct 23 ms 36692 KB Output is correct
6 Correct 22 ms 36968 KB Output is correct
7 Correct 41 ms 37200 KB Output is correct
8 Correct 23 ms 37468 KB Output is correct
9 Correct 23 ms 36552 KB Output is correct
10 Correct 25 ms 36956 KB Output is correct
11 Correct 23 ms 37204 KB Output is correct
12 Correct 24 ms 37560 KB Output is correct
13 Correct 46 ms 40020 KB Output is correct
14 Correct 50 ms 43872 KB Output is correct
15 Correct 34 ms 47980 KB Output is correct
16 Correct 39 ms 51792 KB Output is correct
17 Correct 27 ms 39776 KB Output is correct
18 Correct 30 ms 43604 KB Output is correct
19 Correct 36 ms 47696 KB Output is correct
20 Correct 43 ms 51564 KB Output is correct
21 Correct 30 ms 39792 KB Output is correct
22 Correct 32 ms 43600 KB Output is correct
23 Correct 40 ms 47700 KB Output is correct
24 Correct 68 ms 51568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 740 ms 524288 KB Output is correct
2 Runtime error 627 ms 524288 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 23 ms 36676 KB Output is correct
2 Correct 23 ms 36888 KB Output is correct
3 Correct 42 ms 37200 KB Output is correct
4 Correct 24 ms 37460 KB Output is correct
5 Correct 23 ms 36692 KB Output is correct
6 Correct 22 ms 36968 KB Output is correct
7 Correct 41 ms 37200 KB Output is correct
8 Correct 23 ms 37468 KB Output is correct
9 Correct 23 ms 36552 KB Output is correct
10 Correct 25 ms 36956 KB Output is correct
11 Correct 23 ms 37204 KB Output is correct
12 Correct 24 ms 37560 KB Output is correct
13 Correct 46 ms 40020 KB Output is correct
14 Correct 50 ms 43872 KB Output is correct
15 Correct 34 ms 47980 KB Output is correct
16 Correct 39 ms 51792 KB Output is correct
17 Correct 27 ms 39776 KB Output is correct
18 Correct 30 ms 43604 KB Output is correct
19 Correct 36 ms 47696 KB Output is correct
20 Correct 43 ms 51564 KB Output is correct
21 Correct 30 ms 39792 KB Output is correct
22 Correct 32 ms 43600 KB Output is correct
23 Correct 40 ms 47700 KB Output is correct
24 Correct 68 ms 51568 KB Output is correct
25 Correct 740 ms 524288 KB Output is correct
26 Runtime error 627 ms 524288 KB Execution killed with signal 9
27 Halted 0 ms 0 KB -