Submission #875189

# Submission time Handle Problem Language Result Execution time Memory
875189 2023-11-18T18:45:36 Z vjudge1 Klasika (COCI20_klasika) C++11
110 / 110
1887 ms 451944 KB
/***   Coding for Fun ^^ وَمَا تَوْفِيقِي إِلَّا بِاللَّهِ ۚ عَلَيْهِ تَوَكَّلْتُ وَإِلَيْهِ أُنِيبُ    ***/
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned ll
#define ld long double
#define ed '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fixed(x) fixed<<setprecision(x)
#define memo(dp,x) memset(dp,x,sizeof(dp))
#define sumOf(a) (ll)((-1 + sqrt(8LL * a + 1)) / 2)
#define debug cerr
#define Good_Bay return

using namespace std;
using namespace __gnu_pbds;
std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());

template < typename T > using ordered_set = tree< T, null_type, less_equal< T >, rb_tree_tag, tree_order_statistics_node_update >;

template < typename T > istream& operator >> (istream& in, vector < T >& v) {
    for (auto& x : v) in >> x;
    return in;
}

template < typename T > ostream& operator << (ostream& out, const vector < T >& v) {
    for (const T& x : v) out << x << ' ';
    return out;
}


void Accept() { // for VS code
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #else
        // freopen("darwin.in", "r", stdin);
        // freopen("visitfj.out", "w", stdout);
    // #endif
}

constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 1e6 + 5;
const double pi = acos(-1), EPS = 1e-7;
constexpr ll OO = 0x3f3f3f3f3f3f3f3f;

constexpr int dy[] = { 0 , 1 , -1 , 0  , 1 , -1 , 1  , -1 };
constexpr int dx[] = { 1 , 0 ,  0 , -1 , 1 , -1 , -1 ,  1 };

vector<int>adj[N];
vector<int>in(N), out(N), preXor(N);
int id = 1;

void dfs(int u) {
    in[u] = id++;
    for (auto v : adj[u])dfs(v);
    out[u] = id - 1;
}

class Trie {
    struct Node {
        Node* child[2];
        set<int>ids;
        Node() {
            child[0] = child[1] = NULL;
        }
    };
    Node* root;
    int LOG;
public:
    Trie(int _LOG) :LOG(_LOG) { root = new Node(); }
    void insert(int u) {
        Node* v = root;
        int x = preXor[u];
        for (int bit = LOG - 1; bit >= 0; --bit) {
            int to = ((1 << bit) & x ? 1 : 0);
            if (v->child[to] == NULL) {
                v->child[to] = new Node();
            }
            v = v->child[to];
            v->ids.insert(in[u]);
        }
    }
    int query_max(int x, int u) {
        Node* v = root;
        int sum = 0;
        for (int bit = LOG - 1; bit >= 0; bit--) {
            int to = ((1 << bit) & x ? 0 : 1);
            if (v->child[to] != NULL and
                v->child[to]->ids.lower_bound(in[u]) != v->child[to]->ids.lower_bound(out[u] + 1)) {
                v = v->child[to], sum += to * (1 << bit);
            }
            else v = v->child[1 - to], sum += (1 - to) * (1 << bit);
        }
        return x ^ sum;
    }
    int query_min(int x) {
        return INT16_MAX ^ query_max(INT16_MAX ^ x, 0);
    }
};


void doWork(int T) {
    int q, n = 1;
    cin >> q;
    vector<pair<string, pair<int, int>>>Q(q);
    for (auto& i : Q) {
        cin >> i.first >> i.second.first >> i.second.second;
        if (i.first == "Add") {
            adj[i.second.first].push_back(++n);
            preXor[n] = preXor[i.second.first] ^ i.second.second;
        }
    }
    dfs(1);
    Trie tr(30);
    n = 1;
    tr.insert(n++);
    for (auto i : Q) {
        if (i.first == "Add") tr.insert(n++);
        else cout << tr.query_max(preXor[i.second.first], i.second.second) << ed;
    }
}

int main() {
    debug << "M3L4 El-Code MYTFHM4\n";
    Accept();
    int _ = 1;
    // cin >> _;
    for (int __ = 1;__ <= _;__++) {
        // cout << "Case " << __ << ": ";
        doWork(__);
        if (__ < _)cout << '\n';
        // cout << '\n';
    }
    Good_Bay 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35676 KB Output is correct
2 Correct 10 ms 35952 KB Output is correct
3 Correct 10 ms 35928 KB Output is correct
4 Correct 13 ms 36056 KB Output is correct
5 Correct 11 ms 35676 KB Output is correct
6 Correct 11 ms 35932 KB Output is correct
7 Correct 11 ms 36084 KB Output is correct
8 Correct 10 ms 36188 KB Output is correct
9 Correct 10 ms 35676 KB Output is correct
10 Correct 10 ms 35932 KB Output is correct
11 Correct 10 ms 35932 KB Output is correct
12 Correct 10 ms 36188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35676 KB Output is correct
2 Correct 10 ms 35952 KB Output is correct
3 Correct 10 ms 35928 KB Output is correct
4 Correct 13 ms 36056 KB Output is correct
5 Correct 11 ms 35676 KB Output is correct
6 Correct 11 ms 35932 KB Output is correct
7 Correct 11 ms 36084 KB Output is correct
8 Correct 10 ms 36188 KB Output is correct
9 Correct 10 ms 35676 KB Output is correct
10 Correct 10 ms 35932 KB Output is correct
11 Correct 10 ms 35932 KB Output is correct
12 Correct 10 ms 36188 KB Output is correct
13 Correct 12 ms 36956 KB Output is correct
14 Correct 13 ms 38236 KB Output is correct
15 Correct 14 ms 39524 KB Output is correct
16 Correct 14 ms 40552 KB Output is correct
17 Correct 12 ms 36956 KB Output is correct
18 Correct 13 ms 38268 KB Output is correct
19 Correct 14 ms 39516 KB Output is correct
20 Correct 17 ms 40540 KB Output is correct
21 Correct 14 ms 36956 KB Output is correct
22 Correct 15 ms 38232 KB Output is correct
23 Correct 14 ms 39492 KB Output is correct
24 Correct 20 ms 40540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 633 ms 152916 KB Output is correct
2 Correct 906 ms 255056 KB Output is correct
3 Correct 1173 ms 352980 KB Output is correct
4 Correct 1501 ms 451160 KB Output is correct
5 Correct 523 ms 151376 KB Output is correct
6 Correct 884 ms 252240 KB Output is correct
7 Correct 1282 ms 348540 KB Output is correct
8 Correct 1705 ms 445192 KB Output is correct
9 Correct 549 ms 151036 KB Output is correct
10 Correct 941 ms 252860 KB Output is correct
11 Correct 1310 ms 350672 KB Output is correct
12 Correct 1692 ms 446812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35676 KB Output is correct
2 Correct 10 ms 35952 KB Output is correct
3 Correct 10 ms 35928 KB Output is correct
4 Correct 13 ms 36056 KB Output is correct
5 Correct 11 ms 35676 KB Output is correct
6 Correct 11 ms 35932 KB Output is correct
7 Correct 11 ms 36084 KB Output is correct
8 Correct 10 ms 36188 KB Output is correct
9 Correct 10 ms 35676 KB Output is correct
10 Correct 10 ms 35932 KB Output is correct
11 Correct 10 ms 35932 KB Output is correct
12 Correct 10 ms 36188 KB Output is correct
13 Correct 12 ms 36956 KB Output is correct
14 Correct 13 ms 38236 KB Output is correct
15 Correct 14 ms 39524 KB Output is correct
16 Correct 14 ms 40552 KB Output is correct
17 Correct 12 ms 36956 KB Output is correct
18 Correct 13 ms 38268 KB Output is correct
19 Correct 14 ms 39516 KB Output is correct
20 Correct 17 ms 40540 KB Output is correct
21 Correct 14 ms 36956 KB Output is correct
22 Correct 15 ms 38232 KB Output is correct
23 Correct 14 ms 39492 KB Output is correct
24 Correct 20 ms 40540 KB Output is correct
25 Correct 633 ms 152916 KB Output is correct
26 Correct 906 ms 255056 KB Output is correct
27 Correct 1173 ms 352980 KB Output is correct
28 Correct 1501 ms 451160 KB Output is correct
29 Correct 523 ms 151376 KB Output is correct
30 Correct 884 ms 252240 KB Output is correct
31 Correct 1282 ms 348540 KB Output is correct
32 Correct 1705 ms 445192 KB Output is correct
33 Correct 549 ms 151036 KB Output is correct
34 Correct 941 ms 252860 KB Output is correct
35 Correct 1310 ms 350672 KB Output is correct
36 Correct 1692 ms 446812 KB Output is correct
37 Correct 1052 ms 153476 KB Output is correct
38 Correct 1418 ms 254528 KB Output is correct
39 Correct 1594 ms 355300 KB Output is correct
40 Correct 1778 ms 451944 KB Output is correct
41 Correct 1084 ms 151520 KB Output is correct
42 Correct 1382 ms 251732 KB Output is correct
43 Correct 1651 ms 348500 KB Output is correct
44 Correct 1854 ms 444244 KB Output is correct
45 Correct 1092 ms 150860 KB Output is correct
46 Correct 1585 ms 252664 KB Output is correct
47 Correct 1769 ms 349276 KB Output is correct
48 Correct 1887 ms 446584 KB Output is correct