답안 #487575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
487575 2021-11-16T08:53:51 Z KazamaHoang Klasika (COCI20_klasika) C++14
110 / 110
2102 ms 424280 KB
#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

struct Node {
    Node* nxt[2];
    set<int> s;
};

Node* create() {
    Node* res = new Node();
    for (int i = 0; i < 2; ++ i)
        res->nxt[i] = NULL;
    res->s.clear();
    return res;
}
    
int n = 1;
int numQuery;
pair<bool, pair<int, int>> qry[200005];
vector<int> adj[200005];
int in[200005], out[200005], timer = 0, Xor[200005];

Node* root = create();

inline int getbit(int x, int i) {
    return x >> i & 1;
}

void dfs(int u, int par = -1) {
    in[u] = ++ timer;
    for (int& v : adj[u])
        if (v != par) dfs(v, u);
    out[u] = timer;
}

void trie_insert(int x, int pos) {
    Node* tmp = root;
    for (int i = 29; i >= 0; -- i) {
        int c = getbit(x, i);
        if (root->nxt[c] == NULL) 
            root->nxt[c] = create();
        root = root->nxt[c];
        root->s.insert(pos);
    }
    root = tmp;
}

int trie_get(int x, int u) {
    int res = 0;
    Node* tmp = root;
    for (int i = 29; i >= 0; -- i) {
        int lf = 0, rt = 1;
        if (getbit(x, i)) swap(lf, rt);
        if (root->nxt[rt] == NULL) { 
            root = root->nxt[lf];
        } else {

            auto it = root->nxt[rt]->s.lower_bound(in[u]);
            if (it != root->nxt[rt]->s.end() && *it <= out[u]) {
                res += (1 << i);
                root = root->nxt[rt];
            } else {
                root = root->nxt[lf];
            }
        }
    }
    root = tmp;
    return res;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> numQuery;
    for (int i = 1; i <= numQuery; ++ i) {
        string op;
        cin >> op >> qry[i].S.F >> qry[i].S.S;
        if (op == "Add") {
            qry[i].F = 0;
            int u = qry[i].S.F;
            int w = qry[i].S.S;
            int v = ++ n;
            Xor[v] = Xor[u] ^ w;
            adj[u].push_back(v);
            qry[i].S.F = v;
        } else qry[i].F = 1;
    }
    dfs(1);
    trie_insert(0, in[1]);
    for (int i = 1; i <= numQuery; ++ i) {
        if (!qry[i].F) {
            trie_insert(Xor[qry[i].S.F], in[qry[i].S.F]);
        } else {
            cout << trie_get(Xor[qry[i].S.F], qry[i].S.S) << "\n";
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5200 KB Output is correct
2 Correct 2 ms 5288 KB Output is correct
3 Correct 3 ms 5456 KB Output is correct
4 Correct 3 ms 5536 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 2 ms 5200 KB Output is correct
7 Correct 3 ms 5336 KB Output is correct
8 Correct 3 ms 5540 KB Output is correct
9 Correct 3 ms 5088 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5320 KB Output is correct
12 Correct 3 ms 5456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5200 KB Output is correct
2 Correct 2 ms 5288 KB Output is correct
3 Correct 3 ms 5456 KB Output is correct
4 Correct 3 ms 5536 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 2 ms 5200 KB Output is correct
7 Correct 3 ms 5336 KB Output is correct
8 Correct 3 ms 5540 KB Output is correct
9 Correct 3 ms 5088 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5320 KB Output is correct
12 Correct 3 ms 5456 KB Output is correct
13 Correct 5 ms 6352 KB Output is correct
14 Correct 6 ms 7596 KB Output is correct
15 Correct 8 ms 8912 KB Output is correct
16 Correct 8 ms 10064 KB Output is correct
17 Correct 5 ms 6352 KB Output is correct
18 Correct 7 ms 7504 KB Output is correct
19 Correct 7 ms 8784 KB Output is correct
20 Correct 9 ms 9936 KB Output is correct
21 Correct 5 ms 6352 KB Output is correct
22 Correct 6 ms 7604 KB Output is correct
23 Correct 7 ms 8784 KB Output is correct
24 Correct 9 ms 9920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 527 ms 120812 KB Output is correct
2 Correct 936 ms 224584 KB Output is correct
3 Correct 1285 ms 323968 KB Output is correct
4 Correct 1741 ms 423836 KB Output is correct
5 Correct 540 ms 118784 KB Output is correct
6 Correct 939 ms 220076 KB Output is correct
7 Correct 1376 ms 317300 KB Output is correct
8 Correct 1835 ms 414656 KB Output is correct
9 Correct 531 ms 118448 KB Output is correct
10 Correct 920 ms 221252 KB Output is correct
11 Correct 1337 ms 319944 KB Output is correct
12 Correct 1836 ms 416832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5200 KB Output is correct
2 Correct 2 ms 5288 KB Output is correct
3 Correct 3 ms 5456 KB Output is correct
4 Correct 3 ms 5536 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 2 ms 5200 KB Output is correct
7 Correct 3 ms 5336 KB Output is correct
8 Correct 3 ms 5540 KB Output is correct
9 Correct 3 ms 5088 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5320 KB Output is correct
12 Correct 3 ms 5456 KB Output is correct
13 Correct 5 ms 6352 KB Output is correct
14 Correct 6 ms 7596 KB Output is correct
15 Correct 8 ms 8912 KB Output is correct
16 Correct 8 ms 10064 KB Output is correct
17 Correct 5 ms 6352 KB Output is correct
18 Correct 7 ms 7504 KB Output is correct
19 Correct 7 ms 8784 KB Output is correct
20 Correct 9 ms 9936 KB Output is correct
21 Correct 5 ms 6352 KB Output is correct
22 Correct 6 ms 7604 KB Output is correct
23 Correct 7 ms 8784 KB Output is correct
24 Correct 9 ms 9920 KB Output is correct
25 Correct 527 ms 120812 KB Output is correct
26 Correct 936 ms 224584 KB Output is correct
27 Correct 1285 ms 323968 KB Output is correct
28 Correct 1741 ms 423836 KB Output is correct
29 Correct 540 ms 118784 KB Output is correct
30 Correct 939 ms 220076 KB Output is correct
31 Correct 1376 ms 317300 KB Output is correct
32 Correct 1835 ms 414656 KB Output is correct
33 Correct 531 ms 118448 KB Output is correct
34 Correct 920 ms 221252 KB Output is correct
35 Correct 1337 ms 319944 KB Output is correct
36 Correct 1836 ms 416832 KB Output is correct
37 Correct 1016 ms 121864 KB Output is correct
38 Correct 1443 ms 224560 KB Output is correct
39 Correct 1778 ms 326520 KB Output is correct
40 Correct 2023 ms 424280 KB Output is correct
41 Correct 1110 ms 119108 KB Output is correct
42 Correct 1505 ms 220268 KB Output is correct
43 Correct 1830 ms 317536 KB Output is correct
44 Correct 2088 ms 413780 KB Output is correct
45 Correct 1152 ms 118848 KB Output is correct
46 Correct 1621 ms 221364 KB Output is correct
47 Correct 1888 ms 318728 KB Output is correct
48 Correct 2102 ms 416928 KB Output is correct