Submission #1103729

# Submission time Handle Problem Language Result Execution time Memory
1103729 2024-10-21T15:15:59 Z FubuGold Klasika (COCI20_klasika) C++14
0 / 110
452 ms 120196 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) {
                if (cur->ch[c] == nullptr) return ans;
                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) {
                    if (cur->ch[c] == nullptr) return ans;
                    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:66:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Incorrect 2 ms 6740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Incorrect 2 ms 6740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 452 ms 120196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Incorrect 2 ms 6740 KB Output isn't correct
3 Halted 0 ms 0 KB -