답안 #736135

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
736135 2023-05-05T09:01:11 Z marvinthang Klasika (COCI20_klasika) C++17
110 / 110
2329 ms 439752 KB
/******************************
*    author : @marvinthang    *
*    date : 17 / 11 / 2021    *
******************************/

#include <bits/stdc++.h>

using namespace std;

#define  superspeed  ios_base::sync_with_stdio(false);\
                     cin.tie(NULL);\
                     //cout.tie(NULL);
#define  file(name)  freopen(name".inp", "r", stdin);\
                     freopen(name".out", "w", stdout);

const       int MOD = 1e9 + 7; // 998244353;
const     double PI = 3.1415926535897932384626433832795; // acos((db)-1); atan(-1.0);
const      int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
const  long long oo = 1e18;
const       int MAX = 2e5 + 5;

int Q, N = 1, in[MAX], out[MAX], timeDfs, dist[MAX], A[MAX], B[MAX];
vector <pair <int, int>> adj[MAX];
string type[MAX];

void dfs(int u, int par) {
    in[u] = ++timeDfs;
    for (auto &x: adj[u]) {
        int v = x.second;
        int w = x.first;
        if (v == par) continue;
        dist[v] = dist[u] ^ w;
        dfs(v, u);
    }
    out[u] = timeDfs;
}

struct TrieNode {
    TrieNode *child[2];
    set <int> s;
    TrieNode() {
        child[0] = child[1] = nullptr;
        s.clear();
    }
};
const int LOG = 31;

TrieNode *root = new TrieNode();

void TrieInsert(int val, int pos) {
    TrieNode *p = root;
    for (int i = LOG - 1; i >= 0; --i) {
        int bit = (val >> i) & 1;
        if (p -> child[bit] == nullptr) p -> child[bit] = new TrieNode();
        p = p -> child[bit];
        p -> s.insert(pos);
    }
}

int TrieMaxXOR(int val, int l, int r) {
    TrieNode *p = root;
    int res = 0;
    for (int i = LOG - 1; i >= 0; --i) {
        int bit = (val >> i) & 1;
        if (p -> child[bit ^ 1] != nullptr) {
            set <int> :: iterator x = p -> child[bit ^ 1] -> s.lower_bound(l);
            if (*x <= r && x != p -> child[bit ^ 1] -> s.end()) {
                p = p -> child[bit ^ 1];
                res |= (1 << i);
            } else p = p -> child[bit];
        } else p = p -> child[bit];
    }
    return res;
}

int main() {
    superspeed
    cin >> Q;
    for (int i = 1; i <= Q; ++i) {
        cin >> type[i] >> A[i] >> B[i];
        if (type[i] == "Add") {
            int u = A[i];
            int v = ++N;
            int w = B[i];
            A[i] = v;
            adj[u].push_back({w, v});
            adj[v].push_back({w, u});
        }
    }
    dfs(1, 0);
    TrieInsert(dist[1], in[1]);
    for (int i = 1; i <= Q; ++i) {
        if (type[i] == "Add") {
            TrieInsert(dist[A[i]], in[A[i]]);
        } else {
            cout << TrieMaxXOR(dist[A[i]], in[B[i]], out[B[i]]) << '\n';
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 8 ms 11476 KB Output is correct
3 Correct 6 ms 11604 KB Output is correct
4 Correct 7 ms 11732 KB Output is correct
5 Correct 6 ms 11348 KB Output is correct
6 Correct 6 ms 11476 KB Output is correct
7 Correct 6 ms 11676 KB Output is correct
8 Correct 7 ms 11732 KB Output is correct
9 Correct 6 ms 11348 KB Output is correct
10 Correct 8 ms 11612 KB Output is correct
11 Correct 7 ms 11732 KB Output is correct
12 Correct 8 ms 11784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 8 ms 11476 KB Output is correct
3 Correct 6 ms 11604 KB Output is correct
4 Correct 7 ms 11732 KB Output is correct
5 Correct 6 ms 11348 KB Output is correct
6 Correct 6 ms 11476 KB Output is correct
7 Correct 6 ms 11676 KB Output is correct
8 Correct 7 ms 11732 KB Output is correct
9 Correct 6 ms 11348 KB Output is correct
10 Correct 8 ms 11612 KB Output is correct
11 Correct 7 ms 11732 KB Output is correct
12 Correct 8 ms 11784 KB Output is correct
13 Correct 9 ms 12756 KB Output is correct
14 Correct 10 ms 14028 KB Output is correct
15 Correct 12 ms 15248 KB Output is correct
16 Correct 12 ms 16340 KB Output is correct
17 Correct 9 ms 12648 KB Output is correct
18 Correct 15 ms 13908 KB Output is correct
19 Correct 12 ms 15188 KB Output is correct
20 Correct 12 ms 16212 KB Output is correct
21 Correct 9 ms 12668 KB Output is correct
22 Correct 10 ms 13908 KB Output is correct
23 Correct 11 ms 15188 KB Output is correct
24 Correct 12 ms 16292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 591 ms 128876 KB Output is correct
2 Correct 1039 ms 235100 KB Output is correct
3 Correct 1502 ms 336828 KB Output is correct
4 Correct 1989 ms 439256 KB Output is correct
5 Correct 662 ms 126860 KB Output is correct
6 Correct 1191 ms 231116 KB Output is correct
7 Correct 1758 ms 330964 KB Output is correct
8 Correct 2227 ms 430840 KB Output is correct
9 Correct 638 ms 126572 KB Output is correct
10 Correct 1112 ms 231872 KB Output is correct
11 Correct 1543 ms 333408 KB Output is correct
12 Correct 1934 ms 432724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Correct 8 ms 11476 KB Output is correct
3 Correct 6 ms 11604 KB Output is correct
4 Correct 7 ms 11732 KB Output is correct
5 Correct 6 ms 11348 KB Output is correct
6 Correct 6 ms 11476 KB Output is correct
7 Correct 6 ms 11676 KB Output is correct
8 Correct 7 ms 11732 KB Output is correct
9 Correct 6 ms 11348 KB Output is correct
10 Correct 8 ms 11612 KB Output is correct
11 Correct 7 ms 11732 KB Output is correct
12 Correct 8 ms 11784 KB Output is correct
13 Correct 9 ms 12756 KB Output is correct
14 Correct 10 ms 14028 KB Output is correct
15 Correct 12 ms 15248 KB Output is correct
16 Correct 12 ms 16340 KB Output is correct
17 Correct 9 ms 12648 KB Output is correct
18 Correct 15 ms 13908 KB Output is correct
19 Correct 12 ms 15188 KB Output is correct
20 Correct 12 ms 16212 KB Output is correct
21 Correct 9 ms 12668 KB Output is correct
22 Correct 10 ms 13908 KB Output is correct
23 Correct 11 ms 15188 KB Output is correct
24 Correct 12 ms 16292 KB Output is correct
25 Correct 591 ms 128876 KB Output is correct
26 Correct 1039 ms 235100 KB Output is correct
27 Correct 1502 ms 336828 KB Output is correct
28 Correct 1989 ms 439256 KB Output is correct
29 Correct 662 ms 126860 KB Output is correct
30 Correct 1191 ms 231116 KB Output is correct
31 Correct 1758 ms 330964 KB Output is correct
32 Correct 2227 ms 430840 KB Output is correct
33 Correct 638 ms 126572 KB Output is correct
34 Correct 1112 ms 231872 KB Output is correct
35 Correct 1543 ms 333408 KB Output is correct
36 Correct 1934 ms 432724 KB Output is correct
37 Correct 993 ms 130036 KB Output is correct
38 Correct 1436 ms 235120 KB Output is correct
39 Correct 1748 ms 339676 KB Output is correct
40 Correct 2027 ms 439752 KB Output is correct
41 Correct 1160 ms 127620 KB Output is correct
42 Correct 1634 ms 231240 KB Output is correct
43 Correct 1923 ms 331156 KB Output is correct
44 Correct 2283 ms 430324 KB Output is correct
45 Correct 1260 ms 126948 KB Output is correct
46 Correct 1668 ms 231936 KB Output is correct
47 Correct 2152 ms 332008 KB Output is correct
48 Correct 2329 ms 432744 KB Output is correct