답안 #1103728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103728 2024-10-21T15:14:35 Z FubuGold Klasika (COCI20_klasika) C++14
0 / 110
59 ms 25164 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200000;
const int LG = __lg(MAXN);

struct Trie {
    struct Node {
        Node *ch[2];
        set<int> id;
        Node() {
            ch[0] = ch[1] = nullptr;
        }
    };
    Node *head = new Node();
    void add(int x,int id) {
        Node *cur = head;
        for (int i=30;i>=0;i--) {
            int c = (x >> i) & 1;
            if (cur->ch[c] == nullptr) cur->ch[c] = new Node();
            cur = cur->ch[c];
            cur->id.insert(id);
        }
    }
    int query(int l,int r,int x) {
        Node *cur = head;
        int ans = 0;
//        cerr << l << ' ' << r << ' ' << x << '\n';
        for (int i=30;i>=0;i--) {
            int c = (x >> i) & 1;
            if (cur->ch[c ^ 1] == nullptr) cur = cur->ch[c];
            else {
                auto tmp = cur->ch[c^1]->id.lower_bound(l);
                if (tmp == cur->ch[c^1]->id.end() || (*tmp) > r) cur = cur->ch[c];
                else {
//                    cerr << i << ' ' << *tmp << '\n';
                    ans ^= 1 << i;
                    cur = cur->ch[c ^ 1];
                }
            }
        }
        return ans;
    }
} trie;

struct Query {
    int type;
    int x,y;
    Query(){};
} query[MAXN+1];

vector<int> adj[MAXN+1];
int xor_p[MAXN+1];
int tin[MAXN+1],tout[MAXN+1];
int timeDFS = 0;

void dfs(int u) {
    tin[u] = ++timeDFS;
    for (int i=0;i<adj[u].size();i++) {
        int v = adj[u][i];
        dfs(v);
    }
    tout[u] = timeDFS;
}

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int q; cin >> q;
    int cnt = 1;
    for (int i=1;i<=q;i++) {
        string s; cin >> s >> query[i].x >> query[i].y;
        if (s == "Add") {
            cnt++;
            adj[query[i].x].push_back(cnt);
            xor_p[cnt] = xor_p[query[i].x] ^ query[i].y;
        }
        else {
            query[i].type = 1;
        }
    }
    dfs(1);
    cnt = 1;
    for (int i=1;i<=q;i++) {
        if (query[i].type == 0) {
            cnt++;
            trie.add(xor_p[cnt],tin[cnt]);
        }
        else {
//            cerr << query[i].y << ' ' << tin[query[i].y] << ' ' << tout[query[i].y] << '\n';
            cout << trie.query(tin[query[i].y],tout[query[i].y],xor_p[query[i].x]) << '\n';
        }
    }
    return 0;
}

Compilation message

klasika.cpp: In function 'void dfs(int)':
klasika.cpp:60:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 10 ms 12884 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 10 ms 12884 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 59 ms 25164 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 10 ms 12884 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -