Submission #965551

# Submission time Handle Problem Language Result Execution time Memory
965551 2024-04-18T20:57:18 Z dosts Klasika (COCI20_klasika) C++17
110 / 110
2099 ms 436600 KB
//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops,Ofast")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " << 
#define vi vector<int>
const int N = 2e5+100,inf = 2e9,MOD = 1e9+7,B = 30;


struct Trie {
    Trie* children[2] = {NULL,NULL};
    set<int> bas;
    void insert(int x,int p) {
        Trie* cur = this;
        for (int i=30;i>=-1;i--) {
            cur->bas.insert(p);
            if (i > -1) {
                bool go = x&(1<<i);
                if (!cur->children[go]) cur->children[go] = new Trie;
                cur = cur->children[go];
            }
        }
    }

    int query(int x,int l,int r) {
        if (bas.empty()) return -inf;
        Trie* cur = this;
        int ans = 0;
        for (int i=30;i>=0;i--) {
            bool go = !(x&(1<<i));
            if (cur->children[go]) {
                auto it = cur->children[go]->bas.lower_bound(l);
                if (it != cur->children[go]->bas.end()) {
                    if (*it <= r) {
                        ans+=(1<<i);
                        cur = cur->children[go];
                        continue;
                    }
                }
            } 
            if (!cur->children[!go]) cur->children[!go] = new Trie;
            cur = cur->children[!go];
        }
        return ans;
    }

};


vi tin(N),tout(N),d(N),edges[N];
int timer = 1;
void dfs(int node) {
    tin[node] = timer++;
    for (auto it : edges[node]) dfs(it);
    tout[node] = timer-1;
}

void solve() {
    d[1] = 0;
    int curnode = 1;
    int q;
    cin >> q;
    vector<pair<int,pii>> qs(q+1);
    for (int i=1;i<=q;i++) {
        string s;
        cin >> s;
        int node,v;
        cin >> node >> v;
        if (s == "Add") {
            edges[node].push_back(++curnode);
            d[curnode] = d[node]^v; 
        }
        qs[i] = (pair<int,pii>{(s != "Add"), pii{node,v}});
    }
    dfs(1);
    Trie* root = new Trie;
    root->insert(0,1);
    int curry = 1;
    for (int i=1;i<=q;i++) {
        int type = qs[i].ff;
        if (!type) {
            ++curry;
            root->insert(d[curry],tin[curry]);
        }
        else {
            auto [a,b] = qs[i].ss;
            cout << root->query(d[a],tin[b],tout[b]) << '\n';
        }
    }
}
                 
                             
signed main() { 
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Dodi
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int t = 1;
    //cin >> t; 
    while (t --> 0) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7772 KB Output is correct
3 Correct 3 ms 7784 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 8028 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7772 KB Output is correct
11 Correct 3 ms 7772 KB Output is correct
12 Correct 3 ms 8024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7772 KB Output is correct
3 Correct 3 ms 7784 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 8028 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7772 KB Output is correct
11 Correct 3 ms 7772 KB Output is correct
12 Correct 3 ms 8024 KB Output is correct
13 Correct 5 ms 8796 KB Output is correct
14 Correct 7 ms 10076 KB Output is correct
15 Correct 7 ms 11352 KB Output is correct
16 Correct 8 ms 12636 KB Output is correct
17 Correct 5 ms 8688 KB Output is correct
18 Correct 8 ms 10076 KB Output is correct
19 Correct 8 ms 11252 KB Output is correct
20 Correct 9 ms 12632 KB Output is correct
21 Correct 5 ms 8796 KB Output is correct
22 Correct 6 ms 10076 KB Output is correct
23 Correct 7 ms 11352 KB Output is correct
24 Correct 9 ms 12376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 459 ms 125780 KB Output is correct
2 Correct 786 ms 232012 KB Output is correct
3 Correct 1128 ms 333928 KB Output is correct
4 Correct 1514 ms 436040 KB Output is correct
5 Correct 529 ms 124400 KB Output is correct
6 Correct 971 ms 229136 KB Output is correct
7 Correct 1453 ms 329552 KB Output is correct
8 Correct 1806 ms 430072 KB Output is correct
9 Correct 506 ms 123932 KB Output is correct
10 Correct 838 ms 229976 KB Output is correct
11 Correct 1274 ms 331860 KB Output is correct
12 Correct 1623 ms 431956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7772 KB Output is correct
3 Correct 3 ms 7784 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 8028 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7772 KB Output is correct
11 Correct 3 ms 7772 KB Output is correct
12 Correct 3 ms 8024 KB Output is correct
13 Correct 5 ms 8796 KB Output is correct
14 Correct 7 ms 10076 KB Output is correct
15 Correct 7 ms 11352 KB Output is correct
16 Correct 8 ms 12636 KB Output is correct
17 Correct 5 ms 8688 KB Output is correct
18 Correct 8 ms 10076 KB Output is correct
19 Correct 8 ms 11252 KB Output is correct
20 Correct 9 ms 12632 KB Output is correct
21 Correct 5 ms 8796 KB Output is correct
22 Correct 6 ms 10076 KB Output is correct
23 Correct 7 ms 11352 KB Output is correct
24 Correct 9 ms 12376 KB Output is correct
25 Correct 459 ms 125780 KB Output is correct
26 Correct 786 ms 232012 KB Output is correct
27 Correct 1128 ms 333928 KB Output is correct
28 Correct 1514 ms 436040 KB Output is correct
29 Correct 529 ms 124400 KB Output is correct
30 Correct 971 ms 229136 KB Output is correct
31 Correct 1453 ms 329552 KB Output is correct
32 Correct 1806 ms 430072 KB Output is correct
33 Correct 506 ms 123932 KB Output is correct
34 Correct 838 ms 229976 KB Output is correct
35 Correct 1274 ms 331860 KB Output is correct
36 Correct 1623 ms 431956 KB Output is correct
37 Correct 859 ms 126880 KB Output is correct
38 Correct 1262 ms 232128 KB Output is correct
39 Correct 1577 ms 336424 KB Output is correct
40 Correct 1791 ms 436600 KB Output is correct
41 Correct 942 ms 124944 KB Output is correct
42 Correct 1481 ms 229228 KB Output is correct
43 Correct 1730 ms 329924 KB Output is correct
44 Correct 2099 ms 429304 KB Output is correct
45 Correct 1027 ms 124404 KB Output is correct
46 Correct 1545 ms 230008 KB Output is correct
47 Correct 1767 ms 330644 KB Output is correct
48 Correct 1963 ms 431812 KB Output is correct