Submission #1342498

#TimeUsernameProblemLanguageResultExecution timeMemory
1342498po_rag526Klasika (COCI20_klasika)C++20
0 / 110
114 ms41228 KiB
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast, unroll-loops")
// #ifdef ONLINE_JUDGE
//     #define debug(...) ((void)0)
// #else
//     #include "debug.h"
// #endif
using namespace std;
using ll = long long;
#define endl '\n'

struct Trie{
    struct Node{
        Node *child[2];
        int freq, i;
        Node(){
            child[0] = child[1] = NULL;
            freq = 0;
            i = -1;
        }
    };
    Node *root;
    int bits;
    Trie(int x){
        root = new Node();
        bits = x;
    }
    void insert(ll val, int time){
        Node *current = root;
        for (int i = bits; i >= 0; i--){
            bool bit = (val >> i) & 1;
            if (current->child[bit] == NULL){
                current->child[bit] = new Node();
            }
            current = current->child[bit];
            current->freq++;
            current->i = max(current->i, time);
        }
    }
    ll mxXor(ll val, int l){
        Node *current = root;
        ll ret = 0;
        for (ll i = bits; i >= 0; i--){
            bool bit = (val >> i) & 1;
            if (current->child[bit ^ 1] && current->child[bit ^ 1]->i >= l){
                current = current->child[bit ^ 1];
                ret |= (1 << i);
            }else{
                current = current->child[bit];
            }

        }
        return ret;
    }
};

void SOLVE(){
    int q; cin >> q;
    int n = 1;
    vector<vector<int>> graph(n + 1);
    vector<array<int, 4>> qu(q);
    for(int i = 0; i < q; i++){
        string s; cin >> s;
        int t = (s == "Add" ? 1 : 0);
        if(t == 1){
            int x, y; cin >> x >> y;
            graph[x].push_back(++n);
            graph.push_back(vector<int>());
            qu[i] = {t, x, n, y};
        }else{
            int a, b; cin >> a >> b;
            qu[i] = {t, a, b, -1};
        }
    }

    vector<int> tin(n + 1), tout(n + 1), vertex(n + 1), sz(n + 1, 1), d(n + 1, 1);
    int timer = 0;
    function<int(int, int)> euler =[&](int u, int p){
        tin[u] = ++timer;
        vertex[tin[u]] = u;
        for(auto v : graph[u]) if(v != p){
            d[v] = d[u] + 1;
            sz[u] += euler(v, u);
        }
        tout[u] = timer;
        return sz[u];
    };
    euler(1, -1);

    
    vector<ll> prevxor(n + 1);
    vector<vector<array<ll, 4>>> queries(n + 1);
    int time = 1;
    for(auto &[t, a, b, w] : qu){
        if(t == 1){
            prevxor[b] = prevxor[a] ^ w;
            queries[tin[b]].push_back({1, prevxor[b], tin[b], time++}); // {type, val, L, time}
        }else{
            int val = prevxor[a];
            int L = tin[b], R = tout[b];
            queries[R].push_back({0, val, L, time++}); // {type, val, L, time}
        }   
    }

    Trie tree(30);
    tree.insert(0, time + 1);
    vector<ll> answer(time, -1);
    for(int i = 1; i <= n; i++){
        for(auto &[t, val, L, j] : queries[i]){
            if(t == 1){
                tree.insert(val, L);
            }else{
                answer[j] = tree.mxXor(val, L);
            }
        }
    }
    for(auto & v : answer) if(~v) cout << v << endl;
}
signed main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    // int o_o; cin >> o_o; while(o_o--)
    SOLVE(); return 0;
}

Compilation message (stderr)

klasika.cpp:2:43: warning: bad option '-f unroll-loops' to pragma 'optimize' [-Wpragmas]
    2 | #pragma GCC optimize("Ofast, unroll-loops")
      |                                           ^
klasika.cpp:16:14: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   16 |         Node(){
      |              ^
klasika.cpp:24:15: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   24 |     Trie(int x){
      |               ^
klasika.cpp:28:33: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   28 |     void insert(ll val, int time){
      |                                 ^
klasika.cpp:40:27: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   40 |     ll mxXor(ll val, int l){
      |                           ^
klasika.cpp:57:12: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   57 | void SOLVE(){
      |            ^
klasika.cpp: In function 'void SOLVE()':
klasika.cpp:78:52: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
   78 |     function<int(int, int)> euler =[&](int u, int p){
      |                                                    ^
klasika.cpp: At global scope:
klasika.cpp:119:13: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
  119 | signed main(){
      |             ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...