답안 #492810

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492810 2021-12-09T02:49:20 Z paulomiranda98 Klasika (COCI20_klasika) C++17
110 / 110
4527 ms 65276 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define endl '\n'
 
using namespace std;
 
using ll = long long;
using pii = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007;
const int dx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
const int dy[] = {-1, 1,  0, 0, 1, -1, -1,  1};
 
struct Vertex {
  int next[2];
  Vertex() {
    next[0] = next[1] = -1;
  }
};
struct XorTrie{
  vector<Vertex> trie;
  XorTrie(){
    trie.emplace_back();
  }
  void add(int x) {
    int v = 0;
    for(int i=30; i>=0; i--){
      int c = (x>>i)&1;
      if (trie[v].next[c] == -1) {
        trie[v].next[c] = trie.size();
        trie.emplace_back();
      }
      v = trie[v].next[c];
    }
  }
  int max(int x) {
    int v = 0, ans = 0;
    for(int i=30; i>=0; i--){
      int c = (x>>i)&1;
      if(trie[v].next[!c] != -1){
        ans |= (1<<i);
        v = trie[v].next[!c];
      }else if(trie[v].next[c] != -1){
        v = trie[v].next[c];
      }else{
        return 0;
      }
    }
    return ans;
  }
};
 

struct SqrtDecomposition{
	const int sqrtLen = 800;
	vector<XorTrie> block;
	vector<int> v;
	SqrtDecomposition(int sz){
		int n = sz;
		v.resize(n, -1);
		block.resize(sqrtLen + 5);
	}
	//0-indexed
	void update(int idx, int new_value){
		v[idx] = new_value;
		block[idx / sqrtLen].add(new_value);
	}
	//0-indexed [l, r]
	int query(int l, int r, int x){
		int ans = 0;
		int c_l = l / sqrtLen, c_r = r / sqrtLen;
		if (c_l == c_r){
			for (int i = l; i <= r; i++){
				if(v[i] != -1)
          ans = max(ans, x^v[i]);
      }
		}else{
			for (int i = l, end = (c_l + 1) * sqrtLen - 1; i <= end; i++){
        if(v[i] != -1)
          ans = max(ans, x ^ v[i]);
      }
			for (int i = c_l + 1; i <= c_r - 1; i++)
				ans = max(ans, block[i].max(x));
			for (int i = c_r * sqrtLen; i <= r; i++){
				if(v[i] != -1)
          ans = max(ans, x^v[i]);
      }
		}
		return ans;
	}
};

const int MAXN = 200010;
 
vector<int> adj[MAXN];
int in[MAXN], out[MAXN];
int v[MAXN], T;
 
void build(int u, int x){
  in[u] = T++;
 
  v[u] = x;
 
  for(int to: adj[u])
    build(to, x ^ v[to]);
 
  out[u] = T-1;
}
 
int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int q;
  cin >> q;
  
  vector<tuple<string, int, int>> queries;
  
  int last = 1;
  
  for(int i=0; i<q; i++){
    string op;
    int a, b;
    cin >> op >> a >> b;
    if(op == "Add"){
      last++;
      adj[a].push_back(last);
      v[last] = b;
      queries.emplace_back(op, last, -1);
    }else{
      queries.emplace_back(op, a, b);
    }
  }
  
  build(1, 0);
  SqrtDecomposition st(last);
  st.update(in[1], v[1]);
  
  for(auto [op, a, b]: queries){
    if(op == "Add"){
      st.update(in[a], v[a]);
    }else{
      cout << st.query(in[b], out[b], v[a]) << endl;
    }
  }
  
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5016 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Correct 3 ms 5144 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 6 ms 5048 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 4 ms 5144 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5016 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Correct 3 ms 5144 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 6 ms 5048 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 4 ms 5144 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 5 ms 5452 KB Output is correct
14 Correct 5 ms 5580 KB Output is correct
15 Correct 6 ms 5584 KB Output is correct
16 Correct 4 ms 5836 KB Output is correct
17 Correct 4 ms 5324 KB Output is correct
18 Correct 4 ms 5580 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 4 ms 5796 KB Output is correct
21 Correct 5 ms 5324 KB Output is correct
22 Correct 4 ms 5536 KB Output is correct
23 Correct 4 ms 5580 KB Output is correct
24 Correct 6 ms 5676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1542 ms 26500 KB Output is correct
2 Correct 2508 ms 36924 KB Output is correct
3 Correct 2746 ms 46640 KB Output is correct
4 Correct 2045 ms 59696 KB Output is correct
5 Correct 2476 ms 29840 KB Output is correct
6 Correct 4255 ms 41976 KB Output is correct
7 Correct 4527 ms 53336 KB Output is correct
8 Correct 3499 ms 65276 KB Output is correct
9 Correct 1365 ms 26616 KB Output is correct
10 Correct 2445 ms 34996 KB Output is correct
11 Correct 2833 ms 43828 KB Output is correct
12 Correct 2241 ms 52288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5016 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Correct 3 ms 5144 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 6 ms 5048 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 4 ms 5144 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 5 ms 5452 KB Output is correct
14 Correct 5 ms 5580 KB Output is correct
15 Correct 6 ms 5584 KB Output is correct
16 Correct 4 ms 5836 KB Output is correct
17 Correct 4 ms 5324 KB Output is correct
18 Correct 4 ms 5580 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 4 ms 5796 KB Output is correct
21 Correct 5 ms 5324 KB Output is correct
22 Correct 4 ms 5536 KB Output is correct
23 Correct 4 ms 5580 KB Output is correct
24 Correct 6 ms 5676 KB Output is correct
25 Correct 1542 ms 26500 KB Output is correct
26 Correct 2508 ms 36924 KB Output is correct
27 Correct 2746 ms 46640 KB Output is correct
28 Correct 2045 ms 59696 KB Output is correct
29 Correct 2476 ms 29840 KB Output is correct
30 Correct 4255 ms 41976 KB Output is correct
31 Correct 4527 ms 53336 KB Output is correct
32 Correct 3499 ms 65276 KB Output is correct
33 Correct 1365 ms 26616 KB Output is correct
34 Correct 2445 ms 34996 KB Output is correct
35 Correct 2833 ms 43828 KB Output is correct
36 Correct 2241 ms 52288 KB Output is correct
37 Correct 952 ms 28800 KB Output is correct
38 Correct 1653 ms 39524 KB Output is correct
39 Correct 1442 ms 49912 KB Output is correct
40 Correct 1048 ms 59952 KB Output is correct
41 Correct 108 ms 29956 KB Output is correct
42 Correct 148 ms 41536 KB Output is correct
43 Correct 178 ms 53416 KB Output is correct
44 Correct 203 ms 64324 KB Output is correct
45 Correct 251 ms 27388 KB Output is correct
46 Correct 308 ms 35832 KB Output is correct
47 Correct 355 ms 43988 KB Output is correct
48 Correct 281 ms 52404 KB Output is correct