Submission #660594

# Submission time Handle Problem Language Result Execution time Memory
660594 2022-11-22T14:17:51 Z longvu Klasika (COCI20_klasika) C++14
0 / 110
585 ms 132508 KB
/**
 *    author:  longvu
 *    created: 11/22/22 20:04:54
**/
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = numeric_limits<int>::max();
const int nax = (int)(200901);
const int mod = 1e9 + 7;

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

struct Node {
    Node *node_child[2] = {nullptr};
    set<int> memo_idx;
};

#define get_bit(mask, x) (((mask) >> (x)) & 1)

void insert(Node *root, int u, int c) {
    for (int i = 30; i >= 0; --i) {
        if (root -> node_child[get_bit(c, i)] == nullptr) {
            root -> node_child[get_bit(c, i)] = new Node;
        }
        root -> node_child[get_bit(c, i)] -> memo_idx.insert(u);
        root = root -> node_child[get_bit(c, i)];
    }
}

int get(Node *root, int L, int R, int c) {
    int res = 0;
    for (int i = 30; i >= 0; --i) {
        if (root -> node_child[!get_bit(c, i)] != nullptr) {
            set<int>::iterator l = root -> node_child[!get_bit(c, i)] -> memo_idx.lower_bound(L);
            if (*l >= L && *l <= R) {
                res |= (1 << i);
                root = root -> node_child[!get_bit(c, i)];
                continue;
            }
        }
        if (root -> node_child[get_bit(c, i)] != nullptr) {
            set<int>::iterator l = root -> node_child[get_bit(c, i)] -> memo_idx.lower_bound(L);
            if (*l >= L && *l <= R) {
                root = root -> node_child[get_bit(c, i)];
                continue;
            }
        }
        break;
    }
    return res;
}

#define Fi first
#define Se second

string TYPE[nax];
int U[nax], V[nax], C[nax], euler[nax], m = 0, fir[nax], subsz[nax], cost[nax];
vector<pair<int, int>> adj[nax];
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int q;
    cin >> q;
    int n = 1;
    for (int i = 1; i <= q; ++i) {
        cin >> TYPE[i] >> U[i] >> C[i];
        if (TYPE[i] == "Add") {
            n++;
            V[i] = n;
            adj[U[i]].push_back({V[i], C[i]});
            adj[V[i]].push_back({U[i], C[i]});
        }
    }
    function<void(int, int)> dfs = [&](int u, int pa) {
        euler[++m] = u;
        fir[u] = m;
        subsz[u] = 1;
        for (auto v : adj[u]) {
            if (v.Fi == pa) {
                continue;
            }
            cost[v.Fi] = v.Se ^ cost[u];
            dfs(v.Fi, u);
            subsz[u] += subsz[v.Fi];
        }
    };
    dfs(1, -1);
    // for (int i = 1; i <= n; ++i) {
    //     cout << euler[i] << " ";
    // }
    // cout << '\n';
    Node *root = new Node();
    for (int i = 1; i <= q; ++i) {
        if (TYPE[i] == "Add") {
            insert(root, fir[V[i]],  cost[V[i]]);
        } else {
            cout << get(root, fir[C[i]], fir[C[i]] + subsz[C[i]] - 1, cost[U[i]]) << '\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Incorrect 6 ms 11604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Incorrect 6 ms 11604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 585 ms 132508 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11476 KB Output is correct
2 Incorrect 6 ms 11604 KB Output isn't correct
3 Halted 0 ms 0 KB -