Submission #844858

# Submission time Handle Problem Language Result Execution time Memory
844858 2023-09-06T05:55:30 Z vjudge1 Klasika (COCI20_klasika) C++17
66 / 110
5000 ms 96544 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
const int MAXN = 2e5 + 5;

#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    #define OPEN freopen(".in", "r", stdin); \
                 freopen(".out", "w", stdout);
#else
    #define OPEN void(23);
#endif

vector <pair <int, int>> adj[MAXN];
vector <pair <int, int>> radj[MAXN];
int cnt = 2;
int FindAns(int a, int b)
{
    vector <bool> sub(cnt + 5);
    function <void(int)> dfsSub = [&](int node) -> void
    {
        //cerr << node << " ";
        sub[node] = true;
        for(auto &[child, w] : adj[node]) 
            dfsSub(child);

        return;
    };

    dfsSub(b);

    int ans = 0;
    function <void(int, int, int)> dfs = [&](int node, int par, int sum) -> void
    {
        if(sub[node]) ans = max(ans, sum);
        for(auto &[child, w] : adj[node])
            if(child != par) 
                dfs(child, node, sum ^ w);

        for(auto &[child, w] : radj[node])
            if(child != par) 
                dfs(child, node, sum ^ w);
    };

    dfs(a, 0, 0);

    return ans;
}

void solve1(int q, vector <tuple <string, int, int>> &queries)
{
    for(int i = 1; i <= q; i++)
    {
        auto &[str, a, b] = queries[i -1];
        if(str[0] == 'A')
        {
            adj[a].emplace_back(cnt, b);
            radj[cnt].emplace_back(a, b);
            cnt++;
        }
        else
        {
            cout << FindAns(a, b) << "\n";
        }
    }

    return;
}

struct Node
{
    int l = -1, r = -1;
    Node() {}
};

struct Trie
{
    vector <Node> tree;
    Trie() {tree.resize(1);}
    int cnt = 1;
    void add(int x, int node = 0, int bit = 31)
    {
        if(bit < 0) return;
        if((x >> (bit)) & 1)
        {
            if(tree[node].r == -1) tree[node].r = cnt++, tree.emplace_back();
            add(x, tree[node].r, bit -1);
        }
        else
        {
            if(tree[node].l == -1) tree[node].l = cnt++, tree.emplace_back();
            add(x, tree[node].l, bit -1);
        }
    }

    int query(int x, int node = 0, int bit = 31)
    {  
        //cerr << x << " " << node << " " << bit << "\n";
        if(bit < 0) return 0;
        if((x >> (bit)) & 1)
        {
            if(tree[node].l != -1) return (1 << (bit)) + query(x, tree[node].l, bit -1);
            else return query(x, tree[node].r, bit -1);
        }
        else
        {
            if(tree[node].r != -1) return (1 << (bit)) + query(x, tree[node].r, bit -1);
            else return query(x, tree[node].l, bit -1);
        }
    }
};

void solve2(int q, vector <tuple <string, int, int>> &queries)
{
    Trie trie;
    vector <int> dists(2);
    trie.add(0);
    dists[1] = 0;
    for(int i = 1; i <= q; i++)
    {
        auto &[str, a, b] = queries[i -1];
        cerr << dists[a] << " " << b << "\n";
        if(str[0] == 'A')
        {
            dists.emplace_back(dists[a] ^ b);
            trie.add(dists[a] ^ b);
            cnt++;
        }
        else
        {
            cout << trie.query(dists[a]) << "\n";
        }
    }
}

int32_t main()
{
    OPEN;

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int q; cin >> q;
    vector <tuple <string, int, int>> queries(q);
    for(auto &[a, b, c] : queries) cin >> a >> b >> c;

    bool ok = true;
    for(auto &[a, b, c] : queries) ok &= ((a[0] == 'Q') ? (c == 1) : true);

    if(ok) cerr << 2, solve2(q, queries);
    else solve1(q, queries);
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9824 KB Output is correct
2 Correct 3 ms 9816 KB Output is correct
3 Correct 3 ms 9820 KB Output is correct
4 Correct 3 ms 9816 KB Output is correct
5 Correct 3 ms 9816 KB Output is correct
6 Correct 3 ms 9816 KB Output is correct
7 Correct 3 ms 9816 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 3 ms 9816 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9824 KB Output is correct
2 Correct 3 ms 9816 KB Output is correct
3 Correct 3 ms 9820 KB Output is correct
4 Correct 3 ms 9816 KB Output is correct
5 Correct 3 ms 9816 KB Output is correct
6 Correct 3 ms 9816 KB Output is correct
7 Correct 3 ms 9816 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 3 ms 9816 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 9 ms 9816 KB Output is correct
14 Correct 10 ms 10072 KB Output is correct
15 Correct 11 ms 10072 KB Output is correct
16 Correct 8 ms 10072 KB Output is correct
17 Correct 7 ms 9816 KB Output is correct
18 Correct 8 ms 9816 KB Output is correct
19 Correct 8 ms 9816 KB Output is correct
20 Correct 9 ms 9816 KB Output is correct
21 Correct 7 ms 9820 KB Output is correct
22 Correct 9 ms 9816 KB Output is correct
23 Correct 9 ms 9816 KB Output is correct
24 Correct 7 ms 10072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1154 ms 43564 KB Output is correct
2 Correct 1127 ms 61664 KB Output is correct
3 Correct 1122 ms 59604 KB Output is correct
4 Correct 1130 ms 96544 KB Output is correct
5 Correct 1160 ms 42684 KB Output is correct
6 Correct 1115 ms 61732 KB Output is correct
7 Correct 1075 ms 60428 KB Output is correct
8 Correct 1068 ms 96524 KB Output is correct
9 Correct 1160 ms 44080 KB Output is correct
10 Correct 1110 ms 60700 KB Output is correct
11 Correct 1056 ms 59212 KB Output is correct
12 Correct 1041 ms 96156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9824 KB Output is correct
2 Correct 3 ms 9816 KB Output is correct
3 Correct 3 ms 9820 KB Output is correct
4 Correct 3 ms 9816 KB Output is correct
5 Correct 3 ms 9816 KB Output is correct
6 Correct 3 ms 9816 KB Output is correct
7 Correct 3 ms 9816 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 3 ms 9816 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 9 ms 9816 KB Output is correct
14 Correct 10 ms 10072 KB Output is correct
15 Correct 11 ms 10072 KB Output is correct
16 Correct 8 ms 10072 KB Output is correct
17 Correct 7 ms 9816 KB Output is correct
18 Correct 8 ms 9816 KB Output is correct
19 Correct 8 ms 9816 KB Output is correct
20 Correct 9 ms 9816 KB Output is correct
21 Correct 7 ms 9820 KB Output is correct
22 Correct 9 ms 9816 KB Output is correct
23 Correct 9 ms 9816 KB Output is correct
24 Correct 7 ms 10072 KB Output is correct
25 Correct 1154 ms 43564 KB Output is correct
26 Correct 1127 ms 61664 KB Output is correct
27 Correct 1122 ms 59604 KB Output is correct
28 Correct 1130 ms 96544 KB Output is correct
29 Correct 1160 ms 42684 KB Output is correct
30 Correct 1115 ms 61732 KB Output is correct
31 Correct 1075 ms 60428 KB Output is correct
32 Correct 1068 ms 96524 KB Output is correct
33 Correct 1160 ms 44080 KB Output is correct
34 Correct 1110 ms 60700 KB Output is correct
35 Correct 1056 ms 59212 KB Output is correct
36 Correct 1041 ms 96156 KB Output is correct
37 Execution timed out 5009 ms 26080 KB Time limit exceeded
38 Halted 0 ms 0 KB -