Submission #875179

# Submission time Handle Problem Language Result Execution time Memory
875179 2023-11-18T18:30:55 Z Dr_rabi3 Klasika (COCI20_klasika) C++14
33 / 110
1927 ms 524288 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++;
}

struct binary_trie {
    vector<array<int, 2>> trie;
    vector<array<set<int>, 2>>tin;
    int LOG;
    binary_trie(int _LOG) : LOG(_LOG) {
        trie = { array<int, 2>{-1, -1} };
        tin = { array<set<int>, 2>{} };
    }

    void insert(int u) {
        int x = preXor[u];
        int v = 0;
        for (int bit = LOG - 1; bit >= 0; --bit) {
            int to = ((1 << bit) & x ? 1 : 0);
            if (~trie[v][to]) {
                tin[v][to].insert(in[u]);
                v = trie[v][to];
            }
            else {
                tin[v][to].insert(in[u]);
                v = (trie[v][to] = (int)trie.size());
                trie.push_back(array<int, 2>{-1, -1});
                tin.push_back(array<set<int>, 2>{});
            }
        }
    }

    int query_max(int x, int u) {
        int v = 0, sum = 0;
        for (int bit = LOG - 1; bit >= 0; bit--) {
            int to = ((1 << bit) & x ? 0 : 1);
            if (~trie[v][to] and
                tin[v][to].lower_bound(in[u]) != tin[v][to].lower_bound(out[u] + 1)) {
                v = trie[v][to], sum += to * (1 << bit);
            }
            else v = trie[v][1 - to], sum += (1 - to) * (1 << bit);
        }
        return x ^ sum;
    }
};


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);
    binary_trie tr(32);
    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";
    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 11 ms 35928 KB Output is correct
2 Correct 11 ms 35932 KB Output is correct
3 Correct 14 ms 36188 KB Output is correct
4 Correct 11 ms 36500 KB Output is correct
5 Correct 11 ms 35672 KB Output is correct
6 Correct 11 ms 35932 KB Output is correct
7 Correct 11 ms 36136 KB Output is correct
8 Correct 11 ms 36248 KB Output is correct
9 Correct 11 ms 35952 KB Output is correct
10 Correct 11 ms 36188 KB Output is correct
11 Correct 11 ms 36184 KB Output is correct
12 Correct 12 ms 36280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 35928 KB Output is correct
2 Correct 11 ms 35932 KB Output is correct
3 Correct 14 ms 36188 KB Output is correct
4 Correct 11 ms 36500 KB Output is correct
5 Correct 11 ms 35672 KB Output is correct
6 Correct 11 ms 35932 KB Output is correct
7 Correct 11 ms 36136 KB Output is correct
8 Correct 11 ms 36248 KB Output is correct
9 Correct 11 ms 35952 KB Output is correct
10 Correct 11 ms 36188 KB Output is correct
11 Correct 11 ms 36184 KB Output is correct
12 Correct 12 ms 36280 KB Output is correct
13 Correct 16 ms 37908 KB Output is correct
14 Correct 16 ms 41740 KB Output is correct
15 Correct 17 ms 40976 KB Output is correct
16 Correct 19 ms 46708 KB Output is correct
17 Correct 14 ms 37908 KB Output is correct
18 Correct 19 ms 41488 KB Output is correct
19 Correct 19 ms 41764 KB Output is correct
20 Correct 19 ms 41792 KB Output is correct
21 Correct 15 ms 37800 KB Output is correct
22 Correct 22 ms 40076 KB Output is correct
23 Correct 17 ms 40204 KB Output is correct
24 Correct 19 ms 41480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 883 ms 203916 KB Output is correct
2 Correct 1440 ms 369924 KB Output is correct
3 Correct 1927 ms 407744 KB Output is correct
4 Runtime error 1544 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 35928 KB Output is correct
2 Correct 11 ms 35932 KB Output is correct
3 Correct 14 ms 36188 KB Output is correct
4 Correct 11 ms 36500 KB Output is correct
5 Correct 11 ms 35672 KB Output is correct
6 Correct 11 ms 35932 KB Output is correct
7 Correct 11 ms 36136 KB Output is correct
8 Correct 11 ms 36248 KB Output is correct
9 Correct 11 ms 35952 KB Output is correct
10 Correct 11 ms 36188 KB Output is correct
11 Correct 11 ms 36184 KB Output is correct
12 Correct 12 ms 36280 KB Output is correct
13 Correct 16 ms 37908 KB Output is correct
14 Correct 16 ms 41740 KB Output is correct
15 Correct 17 ms 40976 KB Output is correct
16 Correct 19 ms 46708 KB Output is correct
17 Correct 14 ms 37908 KB Output is correct
18 Correct 19 ms 41488 KB Output is correct
19 Correct 19 ms 41764 KB Output is correct
20 Correct 19 ms 41792 KB Output is correct
21 Correct 15 ms 37800 KB Output is correct
22 Correct 22 ms 40076 KB Output is correct
23 Correct 17 ms 40204 KB Output is correct
24 Correct 19 ms 41480 KB Output is correct
25 Correct 883 ms 203916 KB Output is correct
26 Correct 1440 ms 369924 KB Output is correct
27 Correct 1927 ms 407744 KB Output is correct
28 Runtime error 1544 ms 524288 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -