답안 #1103732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103732 2024-10-21T15:21:12 Z FubuGold Klasika (COCI20_klasika) C++14
0 / 110
50 ms 22344 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;
        }
        bool check(int l,int r) {
            auto it = id.lower_bound(l);
            return it != id.end() && *it <= r;
        }
    };
    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 = 1 - ((x >> i) & 1);
            if (cur->ch[c] == nullptr) c = 1 - c;
            if (!cur->ch[c]->check(l,r)) c = 1 - c;
            if (cur->ch[c] == nullptr) return ans ^ x;
            ans |= c << i;
            cur = cur->ch[c];
        }
        return ans ^ x;
    }
} 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:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 12 ms 13136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 12 ms 13136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 50 ms 22344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 12 ms 13136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -