Submission #697703

# Submission time Handle Problem Language Result Execution time Memory
697703 2023-02-10T19:37:04 Z gagik_2007 Klasika (COCI20_klasika) C++17
110 / 110
2915 ms 523032 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;

typedef long long ll;
typedef long double ld;

#define ff first
#define ss second

const ll K = 2;

struct Vertex {
    ll to[K];
    set<ll>inds;
    Vertex() {
        for (ll i = 0; i < K; i++) {
            to[i] = -1;
        }
    }
};

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 200007;
const ll LG = 30;
ll n, m, k;
vector<Vertex>p(1);
vector<pair<ll, ll>>q;
vector<pair<ll, ll>>g[N];
ll vl[N];
ll ind[N];
ll sz[N];
ll timer = 0;

void dfs(ll v, ll val = 0, ll par = -1) {
    sz[v] = 1;
    ind[v] = timer++;
    if (par != -1) {
        vl[v] = vl[par] ^ val;
    }
    for (ll i = 0; i < g[v].size(); i++) {
        ll to = g[v][i].ff;
        ll w = g[v][i].ss;
        if (to != par) {
            dfs(to, w, v);
            sz[v] += sz[to];
        }
    }
}

void add(ll x, ll i) {
    ll v = 0;
    p[v].inds.insert(i);
    for (ll j = LG; j >= 0; j--) {
        ll c = 0;
        if (x & (1ll << j)) {
            c = 1;
        }
        if (p[v].to[c] == -1) {
            p[v].to[c] = p.size();
            p.emplace_back();
        }
        p[p[v].to[c]].inds.insert(i);
        //cout << v << ": " << p[v].to[0] << " " << p[v].to[1] << endl;
        v = p[v].to[c];
    }
}

ll get_ans(ll x, ll i) {
    ll v = 0;
    ll ans = 0;
    for (ll j = LG; j >= 0; j--) {
        if (x & (1ll << j)) {
            if (p[v].to[0] != -1) {
                auto it = p[p[v].to[0]].inds.lower_bound(ind[i]);
                //cout << res << " 1 ";
                if ((it != p[p[v].to[0]].inds.end()) && (*it < ind[i] + sz[i])) {
                    v = p[v].to[0];
                    ans += (1ll << j);
                }
                else {
                    v = p[v].to[1];
                }
            }
            else {
                v = p[v].to[1];
            }
        }
        else {
            if (p[v].to[1] != -1) {
                auto it = p[p[v].to[1]].inds.lower_bound(ind[i]);
                //cout << res << " 2 ";
                if ((it != p[p[v].to[1]].inds.end()) && (*it < ind[i] + sz[i])) {
                    v = p[v].to[1];
                    ans += (1ll << j);
                }
                else {
                    v = p[v].to[0];
                }
            }
            else {
                v = p[v].to[0];
            }
        }
    }
    //cout << endl;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    ll cur = 2;
    vl[1] = 0;
    for (ll i = 0; i < n; i++) {
        ll x, y;
        string tp;
        cin >> tp >> x >> y;
        if (tp == "Add") {
            g[x].push_back({ cur,y });
            q.push_back({ -1,cur });
            cur++;
        }
        else {
            q.push_back({ x,y });
        }
    }
    dfs(1);
    /*for (ll i = 1; i <= n; i++) {
        cout << i << ": " << vl[i] << endl;
    }
    cout << endl;*/
    //cout << "HASA STE\n";
    add(vl[1], ind[1]);
    for (ll i = 0; i < n; i++) {
        if (q[i].ff == -1) {
            ll v = q[i].ss;
            //cout << "ADDING: " << v << endl;
            add(vl[v], ind[v]);
        }
        else {
            cout << get_ans(vl[q[i].ff], q[i].ss) << endl;
        }
    }
    return 0;
}

/// ---- - --------  ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - --------  ------ -------- -- - - -

Compilation message

klasika.cpp: In function 'void dfs(ll, ll, ll)':
klasika.cpp:61:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (ll i = 0; i < g[v].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5416 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 3 ms 5152 KB Output is correct
6 Correct 3 ms 5280 KB Output is correct
7 Correct 4 ms 5456 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 5 ms 5460 KB Output is correct
12 Correct 3 ms 5552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5416 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 3 ms 5152 KB Output is correct
6 Correct 3 ms 5280 KB Output is correct
7 Correct 4 ms 5456 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 5 ms 5460 KB Output is correct
12 Correct 3 ms 5552 KB Output is correct
13 Correct 8 ms 6736 KB Output is correct
14 Correct 10 ms 8360 KB Output is correct
15 Correct 10 ms 8736 KB Output is correct
16 Correct 12 ms 11840 KB Output is correct
17 Correct 8 ms 6628 KB Output is correct
18 Correct 10 ms 8348 KB Output is correct
19 Correct 10 ms 8604 KB Output is correct
20 Correct 11 ms 9540 KB Output is correct
21 Correct 9 ms 6696 KB Output is correct
22 Correct 11 ms 8264 KB Output is correct
23 Correct 11 ms 8540 KB Output is correct
24 Correct 16 ms 9552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 837 ms 129420 KB Output is correct
2 Correct 1378 ms 259292 KB Output is correct
3 Correct 1892 ms 314472 KB Output is correct
4 Correct 2482 ms 522824 KB Output is correct
5 Correct 986 ms 128872 KB Output is correct
6 Correct 1555 ms 252660 KB Output is correct
7 Correct 2078 ms 305388 KB Output is correct
8 Correct 2738 ms 509308 KB Output is correct
9 Correct 956 ms 129524 KB Output is correct
10 Correct 1511 ms 253852 KB Output is correct
11 Correct 1948 ms 308096 KB Output is correct
12 Correct 2568 ms 511548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5416 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 3 ms 5152 KB Output is correct
6 Correct 3 ms 5280 KB Output is correct
7 Correct 4 ms 5456 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 5 ms 5460 KB Output is correct
12 Correct 3 ms 5552 KB Output is correct
13 Correct 8 ms 6736 KB Output is correct
14 Correct 10 ms 8360 KB Output is correct
15 Correct 10 ms 8736 KB Output is correct
16 Correct 12 ms 11840 KB Output is correct
17 Correct 8 ms 6628 KB Output is correct
18 Correct 10 ms 8348 KB Output is correct
19 Correct 10 ms 8604 KB Output is correct
20 Correct 11 ms 9540 KB Output is correct
21 Correct 9 ms 6696 KB Output is correct
22 Correct 11 ms 8264 KB Output is correct
23 Correct 11 ms 8540 KB Output is correct
24 Correct 16 ms 9552 KB Output is correct
25 Correct 837 ms 129420 KB Output is correct
26 Correct 1378 ms 259292 KB Output is correct
27 Correct 1892 ms 314472 KB Output is correct
28 Correct 2482 ms 522824 KB Output is correct
29 Correct 986 ms 128872 KB Output is correct
30 Correct 1555 ms 252660 KB Output is correct
31 Correct 2078 ms 305388 KB Output is correct
32 Correct 2738 ms 509308 KB Output is correct
33 Correct 956 ms 129524 KB Output is correct
34 Correct 1511 ms 253852 KB Output is correct
35 Correct 1948 ms 308096 KB Output is correct
36 Correct 2568 ms 511548 KB Output is correct
37 Correct 1478 ms 132828 KB Output is correct
38 Correct 2017 ms 259828 KB Output is correct
39 Correct 2357 ms 317128 KB Output is correct
40 Correct 2723 ms 523032 KB Output is correct
41 Correct 1660 ms 129572 KB Output is correct
42 Correct 2214 ms 253136 KB Output is correct
43 Correct 2551 ms 305476 KB Output is correct
44 Correct 2915 ms 509660 KB Output is correct
45 Correct 1642 ms 130100 KB Output is correct
46 Correct 2201 ms 254192 KB Output is correct
47 Correct 2437 ms 307000 KB Output is correct
48 Correct 2781 ms 511828 KB Output is correct