제출 #1331761

#제출 시각아이디문제언어결과실행 시간메모리
1331761_nagehKlasika (COCI20_klasika)C++20
110 / 110
2336 ms441092 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define cin(a) for (auto &i : a) cin >> i;

const int OO = 2e18 ,LOG = 30, MOD = 1e9+7 , N = 2e5+5;
int q,tin[N],out[N],prexor[N],nodes=1,timer=0;
vector<pair<int,int>> adj[N];
struct Query
{
    int tp,x,y;
};
struct BinaryTrie {
    struct Node {
        Node* nxt[2];
        set<int> st;   
        Node() {
            nxt[0] = nxt[1] = nullptr;
        }
    };
    Node *root=new Node();
    void insert(int val,int pos)
    {
        Node* cur = root;
        for(int i = LOG; i >= 0; i--) {
            int bit = (val >> i) & 1;
            if(!cur->nxt[bit])
                cur->nxt[bit] = new Node();
            cur = cur->nxt[bit];
            cur->st.insert(pos);
        }
    }
    bool can(Node* node, int l, int r) {
        auto it = node->st.lower_bound(l);
        return it != node->st.end() and *it < r;
    }
    int query(int val, int l, int r) {
        Node* cur = root;
        int ans = 0;
        for(int i = LOG; i >= 0; i--) {
            int bit = (val >> i) & 1;
            int want = bit ^ 1;
            if(cur->nxt[want] and can(cur->nxt[want], l, r)) {
                ans |= (1LL << i);
                cur = cur->nxt[want];
            }
            else if(cur->nxt[bit] and can(cur->nxt[bit], l, r)) {
                cur = cur->nxt[bit];
            }
        }
        return ans;
    }
};
void nageh(){
    cin>>q;
    vector<Query>queries(q);
    for(int i = 0; i < q; i++) {
        string s;
        cin >> s;
        int x, y;
        cin >> x >> y;
        if(s == "Add") {
            queries[i] = {1, x, y};
            ++nodes;
            adj[x].push_back({nodes, y});
            adj[nodes].push_back({x, y});
        }
        else {
            queries[i] = {0, x, y};
        }
    }
    function<void(int,int)> dfs = [&](int node,int p)
    {
        tin[node] = timer++;
        for(auto [it,w]:adj[node])if(it!=p)
        {
            prexor[it] = prexor[node]^w;
            dfs(it,node);
        }
        out[node]=timer;
    };
    dfs(1,0);
    BinaryTrie bt;bt.insert(0,tin[1]);
    nodes=1;
    for(auto [tp,x,y]:queries)
    {
        if(tp==1)
        {
            nodes++;
            bt.insert(prexor[nodes],tin[nodes]);
        }else{
            cout<<bt.query(prexor[x],tin[y],out[y])<<'\n';
        }
    }
}

signed main() {
//إِنَّ اللَّهَ وَمَلَائِكَتَهُ يُصَلُّونَ عَلَى النَّبِيِّ ۚ يَا أَيُّهَا الَّذِينَ آمَنُوا صَلُّوا عَلَيْهِ وَسَلِّمُوا تَسْلِيمًا
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    //cin >> t;
    while(t--) nageh();
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...