Submission #1245204

#TimeUsernameProblemLanguageResultExecution timeMemory
1245204huydayyKlasika (COCI20_klasika)C++20
110 / 110
1475 ms439368 KiB
#include <bits/stdc++.h>
#define TASK "coci1920_r4_klasika"
#define int long long
#define double long double
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<ii>
#define reset(f, x) memset(f, x, sizeof(f))
#define all(x) x.begin(), x.end()
#define sz(x) (long long)x.size()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define FORV(v, H) for (auto &v: H)
#define __builtin_popcount __builtin_popcountll
#define BIT(mask, i) ((mask >> i) & 1ll)
#define MASK(i) (1ll << (i))
#define ONBIT(mask, i) (mask  (1ll << (i)))
#define OFFBIT(mask, i) (mask &~ (1ll << (i)))
#define mid(l, r) ((l + r) >> 1)
#define left(id) (id << 1)
#define right(id) ((id << 1)  1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define UNIQUE(x) sort(all(x)); x.resize(distance(x.begin(), unique(all(x))))
#define BUG1(a) cout << a << '\n'
#define BUG2(a, b) cout << a << " " << b << '\n'
#define BUG3(a, b, c) cout << a << " " << b << " " << c << '\n'
#define BUG4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << '\n'
#define BUG5(a, b, c, d, e) cout << a << " " << b << " " << c << " " << d << " " << e << '\n'
#define BUG6(a, b, c, d, e, f) cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << '\n'

using namespace std;

template <class X, class Y> bool maximize(X &A, const Y &B){
    if (A < B) return A = B, true;
    return false;
}

template <class X, class Y> bool minimize(X &A, const Y &B){
    if (A > B) return A = B, true;
    return false;
}

const int oo  = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int LOG = 16;
const int base = 311;
const int BLOCK = 317;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

int Q, timedfs = 0;
int h[MAXN], in[MAXN], out[MAXN];
vii G[MAXN];

struct DATA{
    string type;
    int x, y;
} a[MAXN];

void DFS(int u, int parent){
    in[u] = ++timedfs;
    FORV(V, G[u]){
        int v = V.fi, c = V.se;
        if (v ==  parent) continue;
        h[v] = h[u] ^ c;
        DFS(v, u);
    }
    out[u] = timedfs;
}

struct Node{
    set <int> st;
    Node *child[2];
};

struct Trie{

    Node *root = new Node();

    void Add(int u){
        int X = h[u];
        Node *p = root;
        FORD(i, 30, 0){
            int k = BIT(X, i);
            if (p -> child[k] == nullptr) p -> child[k] = new Node();
            p = p -> child[k];
            p -> st.insert(in[u]);
        }
    }

    int Get(int X, int u){
        int ans = 0;
        Node *p = root;
        FORD(i, 30, 0){
            int k = BIT(X, i);
            if (p -> child[k ^ 1] != nullptr){
                auto pos = p -> child[k ^ 1] -> st.lower_bound(in[u]);
                if (pos == p -> child[k ^ 1] -> st.end() || *pos > out[u]){
                    if (p -> child[k] != nullptr) p = p -> child[k];
                    else return ans;
                } else{
                    p = p -> child[k ^ 1];
                    ans += MASK(i);
                }
            } else if (p -> child[k] != nullptr) p = p -> child[k];
            else return ans;
        }
        return ans;
    }

} Trie;

main(){
    if (fopen(TASK".inp", "r")){
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> Q;
    int t = 1;
    FOR(i, 1, Q){
        cin >> a[i].type >> a[i].x >> a[i].y;
        if (a[i].type == "Add"){
            ++t;
            G[a[i].x].push_back({t, a[i].y});
            G[t].push_back({a[i].x, a[i].y});
            a[i].x = t;
        }
    }
    DFS(1, 0);
    Trie.Add(1);
    FOR(i, 1, Q){
        if (a[i].type == "Add") Trie.Add(a[i].x);
        else cout << Trie.Get(h[a[i].x], a[i].y) << '\n';
    }
    return 0;
}


















































/*
     ___  __    ___  _______   ___  ___          ________  ___  ________          ___  ___  ___  ___      ___    ___
    \  \\  \ |\  \|\  ___ \ |\  \|\  \        |\   ____\|\  \|\   __  \        |\  \|\  \|\  \|\  \    |\  \  / / |
    \ \  \/  /|\ \  \ \   __/|\ \  \\\  \       \ \  \___|\ \  \ \  \|\  \       \ \  \\\  \ \  \\\  \   \ \  \/ / /
     \ \   ___  \ \  \ \  \_|/_\ \  \\\  \       \ \  \  __\ \  \ \   __  \       \ \   __  \ \  \\\  \   \ \   / /
      \ \  \\ \  \ \  \ \  \_|\ \ \  \\\  \       \ \  \|\  \ \  \ \  \ \  \       \ \  \ \  \ \  \\\  \   \/  / /
       \ \__\\ \__\ \__\ \_______\ \_______\       \ \_______\ \__\ \__\ \__\       \ \__\ \__\ \_______\__/  / /
        \|__| \|__|\|__|\|_______|\|_______|        \|_______|\|__|\|__|\|__|        \|__|\|__|\|_______|\___/ /
                                                                                                        \|___|/
    Author: Kieu Gia Huy a.k.a kiwi
    From: C.H.V with luv <3
    ...

*/

Compilation message (stderr)

klasika.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  118 | main(){
      | ^~~~
klasika.cpp: In function 'int main()':
klasika.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
klasika.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...