Submission #1084771

# Submission time Handle Problem Language Result Execution time Memory
1084771 2024-09-07T00:35:48 Z tfgs Klasika (COCI20_klasika) C++17
110 / 110
1772 ms 430856 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

#define f first
#define s second
template<class T> using V = vector<T>; 
using vi = V<int>;

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) 
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }

constexpr int p2(int x) { return (int)1 << x; }

const int LOGX = 30, MAX_N = 2e5, MAX_NODES = 1e6;

struct Node {
    Node* sons[2];
    set<int> tins;
    Node() {
        sons[0] = sons[1] = NULL;
        tins = {};
    }
} *root = new Node();
int trie_siz = 1;

void add_str(int str, int time) {
    Node* u = root;
    for (int i = LOGX-1; i >= 0; i--) {
        u->tins.insert(time);
        bool nxt = str&p2(i);
        Node* c = u->sons[nxt];
        if (c != NULL) {
            u = c;
        } else {
            u->sons[nxt] = new Node();
            u = u->sons[nxt];
        }
    }
    u->tins.insert(time);
}

int best_match(int pattern, int l, int r) {
    int match = 0;
    Node* u = root;
    for (int i = LOGX-1; i >= 0; i--) {
        bool nxt = !(pattern & p2(i));
        Node* c = u->sons[nxt];
        if (c != NULL && c->tins.lb(l) != c->tins.end() && *c->tins.lb(l) < r) {
            u = u->sons[nxt];
            match += p2(i)*nxt;
        } else {
            u = u->sons[1-nxt];
            match += p2(i)*(1-nxt);
        }
    }
    return match;
}

int timer;
vi tin, tout, x;
V<V<array<int, 2>>> sons;
void dfs(int u) {
    tin[u] = timer++;
    for (auto [v, w] : sons[u]) {
        x[v] = x[u]^w;
        dfs(v);
    }
    tout[u] = timer;
}

void solve() {
    int q; cin >> q;
    int n = 1;

    V<array<int, 3>> queries;
    for (int i = 0; i < q; i++) {
        string typ; cin >> typ;
        if (typ == "Add") {
            int u, w; cin >> u >> w; u--;
            queries.pb({0, n, -1});
            sons.resize(n+1);
            sons[u].pb({n++, w});

        } else {
            int a, b; cin >> a >> b; a--; b--;
            queries.pb({1, a, b});
        }
    }

    tin.resize(n);
    tout.resize(n);
    x.resize(n);
    dfs(0);

    add_str(0, 0);
    for (int i = 0; i < q; i++) {
        if (queries[i][0] == 0) {
            auto [_, u, __] = queries[i];
            add_str(x[u], tin[u]);
        } else {
            auto [_, a, b] = queries[i];
            cout << (x[a]^best_match(x[a], tin[b], tout[b])) << '\n';
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 3 ms 1880 KB Output is correct
14 Correct 4 ms 3164 KB Output is correct
15 Correct 4 ms 4444 KB Output is correct
16 Correct 6 ms 5468 KB Output is correct
17 Correct 3 ms 1628 KB Output is correct
18 Correct 5 ms 2908 KB Output is correct
19 Correct 5 ms 4188 KB Output is correct
20 Correct 6 ms 5428 KB Output is correct
21 Correct 3 ms 1628 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
23 Correct 5 ms 4340 KB Output is correct
24 Correct 9 ms 5464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 448 ms 119148 KB Output is correct
2 Correct 778 ms 225584 KB Output is correct
3 Correct 1061 ms 327748 KB Output is correct
4 Correct 1434 ms 430396 KB Output is correct
5 Correct 479 ms 116984 KB Output is correct
6 Correct 821 ms 221336 KB Output is correct
7 Correct 1202 ms 321448 KB Output is correct
8 Correct 1686 ms 421444 KB Output is correct
9 Correct 483 ms 116552 KB Output is correct
10 Correct 808 ms 222264 KB Output is correct
11 Correct 1144 ms 323908 KB Output is correct
12 Correct 1435 ms 423724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 3 ms 1880 KB Output is correct
14 Correct 4 ms 3164 KB Output is correct
15 Correct 4 ms 4444 KB Output is correct
16 Correct 6 ms 5468 KB Output is correct
17 Correct 3 ms 1628 KB Output is correct
18 Correct 5 ms 2908 KB Output is correct
19 Correct 5 ms 4188 KB Output is correct
20 Correct 6 ms 5428 KB Output is correct
21 Correct 3 ms 1628 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
23 Correct 5 ms 4340 KB Output is correct
24 Correct 9 ms 5464 KB Output is correct
25 Correct 448 ms 119148 KB Output is correct
26 Correct 778 ms 225584 KB Output is correct
27 Correct 1061 ms 327748 KB Output is correct
28 Correct 1434 ms 430396 KB Output is correct
29 Correct 479 ms 116984 KB Output is correct
30 Correct 821 ms 221336 KB Output is correct
31 Correct 1202 ms 321448 KB Output is correct
32 Correct 1686 ms 421444 KB Output is correct
33 Correct 483 ms 116552 KB Output is correct
34 Correct 808 ms 222264 KB Output is correct
35 Correct 1144 ms 323908 KB Output is correct
36 Correct 1435 ms 423724 KB Output is correct
37 Correct 823 ms 119996 KB Output is correct
38 Correct 1212 ms 225452 KB Output is correct
39 Correct 1519 ms 330308 KB Output is correct
40 Correct 1634 ms 430856 KB Output is correct
41 Correct 841 ms 117300 KB Output is correct
42 Correct 1271 ms 221264 KB Output is correct
43 Correct 1558 ms 321700 KB Output is correct
44 Correct 1772 ms 420840 KB Output is correct
45 Correct 908 ms 116992 KB Output is correct
46 Correct 1366 ms 222344 KB Output is correct
47 Correct 1624 ms 322580 KB Output is correct
48 Correct 1727 ms 423904 KB Output is correct