답안 #1101139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101139 2024-10-15T15:25:37 Z InvMOD Klasika (COCI20_klasika) C++14
110 / 110
2096 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;


using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T &a, T b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T &a, T b){if(a > b) return a = b, true; return false;}


const int N = 2e5;
const int C = 2;

struct Query{
    int8_t op;
    int v1, v2;
    Query(int8_t op = 0, int v1 = 0, int v2 = 0): op(op), v1(v1), v2(v2) {}
};
struct Trie{
    struct Node{
        Node* child[C];
        set<int> ckset;

        Node(){
            ckset.insert(N+1);
            for(int i = 0; i < C; i++){
                child[i] = nullptr;
            }
            return;
        }

        bool ck_good(const int& tin, const int& tout){
            return ((*ckset.lower_bound(tin)) <= tout);
        }

        void add(const int& tin){
            return void(ckset.insert(tin));
        }
    };

    typedef Node* pNode;
    pNode root;
    int mxB;
    Trie(int maxBit): mxB(maxBit){
        root = new Node();
    }

    void add_Num(int& x, int& tin){
        pNode cur = root;
        for(int i = mxB; i >= 0; i--){
            int v = (x>>i&1);
            if(cur->child[v] == nullptr){
                cur->child[v] = new Node();
            }
            cur = cur->child[v];
            cur->add(tin);
        }
        return;
    }

    int get_MaxXor(const int& x, const int& tin, const int& tout){
        pNode cur = root;
        int answer = 0;
        for(int i = mxB; i >= 0; i--){
            int v = ((x>>i&1)^1);
            if(cur->child[v] != nullptr && cur->child[v]->ck_good(tin, tout)){
                cur = cur->child[v];
                answer = answer ^ (1<<i);
            }
            else{
                cur = cur->child[(v^1)];
            }
        }
        return answer;
    }
};

int q, i, timerDFS, mx;
vector<int> tin, tout, val;
vector<Query> Q;
vector<vector<int>> adj;

void dfs(int x){
    tin[x] = ++timerDFS;
    for(int v: adj[x]){
        val[v] ^= val[x];
        dfs(v);
    }
    tout[x] = timerDFS;
    return;
}

void solve()
{
    cin >> q;
    timerDFS = 0, mx = 0;
    val.push_back(0);
    adj.push_back(vector<int>());
    for(i = 0; i < q; i++){
        string op;
        cin >> op;
        if(op == "Add"){
            int x,y; cin >> x >> y;
            --x;
            adj[x].push_back(++timerDFS);
            adj.push_back(vector<int>());
            Q.push_back(Query(1, x, timerDFS));
            val.push_back(y);
            mx = max(mx, y);
        }
        else{
            int u,v; cin >> u >> v;
            u--, v--;
            Q.push_back(Query(2, u, v));
        }
    }

    tin.resize(val.size());
    tout.resize(val.size());
    timerDFS = 0;
    dfs(0);

    Trie trie(31 - __builtin_clz(mx));
    trie.add_Num(val[0], tin[0]);
    for(i = 0; i < q; i++){
        if(Q[i].op == 1){
            trie.add_Num(val[Q[i].v2], tin[Q[i].v2]);
        }
        else{
            cout << trie.get_MaxXor(val[Q[i].v1], tin[Q[i].v2], tout[Q[i].v2]) <<"\n";
        }
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 2 ms 1272 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 2 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 2 ms 1272 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 2 ms 1108 KB Output is correct
13 Correct 4 ms 2132 KB Output is correct
14 Correct 5 ms 3964 KB Output is correct
15 Correct 7 ms 5624 KB Output is correct
16 Correct 9 ms 6996 KB Output is correct
17 Correct 4 ms 2296 KB Output is correct
18 Correct 5 ms 3704 KB Output is correct
19 Correct 7 ms 5460 KB Output is correct
20 Correct 8 ms 6740 KB Output is correct
21 Correct 4 ms 2132 KB Output is correct
22 Correct 6 ms 3668 KB Output is correct
23 Correct 7 ms 5460 KB Output is correct
24 Correct 8 ms 6740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 554 ms 146836 KB Output is correct
2 Correct 936 ms 277784 KB Output is correct
3 Correct 1174 ms 402012 KB Output is correct
4 Correct 1480 ms 524288 KB Output is correct
5 Correct 525 ms 144772 KB Output is correct
6 Correct 984 ms 273520 KB Output is correct
7 Correct 1366 ms 395612 KB Output is correct
8 Correct 1846 ms 517884 KB Output is correct
9 Correct 540 ms 144212 KB Output is correct
10 Correct 914 ms 274444 KB Output is correct
11 Correct 1302 ms 398484 KB Output is correct
12 Correct 1658 ms 520488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 2 ms 1272 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 2 ms 1108 KB Output is correct
13 Correct 4 ms 2132 KB Output is correct
14 Correct 5 ms 3964 KB Output is correct
15 Correct 7 ms 5624 KB Output is correct
16 Correct 9 ms 6996 KB Output is correct
17 Correct 4 ms 2296 KB Output is correct
18 Correct 5 ms 3704 KB Output is correct
19 Correct 7 ms 5460 KB Output is correct
20 Correct 8 ms 6740 KB Output is correct
21 Correct 4 ms 2132 KB Output is correct
22 Correct 6 ms 3668 KB Output is correct
23 Correct 7 ms 5460 KB Output is correct
24 Correct 8 ms 6740 KB Output is correct
25 Correct 554 ms 146836 KB Output is correct
26 Correct 936 ms 277784 KB Output is correct
27 Correct 1174 ms 402012 KB Output is correct
28 Correct 1480 ms 524288 KB Output is correct
29 Correct 525 ms 144772 KB Output is correct
30 Correct 984 ms 273520 KB Output is correct
31 Correct 1366 ms 395612 KB Output is correct
32 Correct 1846 ms 517884 KB Output is correct
33 Correct 540 ms 144212 KB Output is correct
34 Correct 914 ms 274444 KB Output is correct
35 Correct 1302 ms 398484 KB Output is correct
36 Correct 1658 ms 520488 KB Output is correct
37 Correct 821 ms 148136 KB Output is correct
38 Correct 1289 ms 277512 KB Output is correct
39 Correct 1621 ms 405088 KB Output is correct
40 Correct 1665 ms 524288 KB Output is correct
41 Correct 890 ms 145276 KB Output is correct
42 Correct 1512 ms 273148 KB Output is correct
43 Correct 1849 ms 395860 KB Output is correct
44 Correct 2096 ms 517016 KB Output is correct
45 Correct 1078 ms 144688 KB Output is correct
46 Correct 1519 ms 274360 KB Output is correct
47 Correct 1633 ms 397012 KB Output is correct
48 Correct 1879 ms 520328 KB Output is correct