답안 #416683

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
416683 2021-06-02T18:42:40 Z fvogel499 Klasika (COCI20_klasika) C++14
110 / 110
3564 ms 524288 KB
/*
File created on 06/02/2021 at 18:42:44.
Link to problem: https://oj.uz/problem/view/COCI20_klasika
Description: use a trie structure, and to only consider subtree nodes use a disc/finish array;
Time complexity: O()
Space complexity: O()
Status: DEV
Copyright: Ⓒ 2021 Francois Vogel
*/

#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <cstring>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>

using namespace std;

#define mp pair<pii, int>
#define pii pair<int, int>
#define f first
#define s second

#define pb push_back
#define ins insert
#define cls clear

#define ll long long

const int siz = 2e5;
const int MAX_P2 = 30;

vector<int> graph [siz];
int par [siz];
int xorTill [siz];
int disc [siz];
int rightMost [siz];

int tim = 0;
int dfs(int cn) {
    disc[cn] = tim;
    rightMost[cn] = tim;
    tim++;
    for (int nn : graph[cn]) rightMost[cn] = dfs(nn);
    return rightMost[cn];
}

struct Node {
    Node() {
        lft = nullptr;
        rgt = nullptr;
    }
    void add(int v, int k, int tins) {
        if (k == MAX_P2) return;
        int i = (v>>(MAX_P2-1-k))%2;
        if (i == 0) {
            if (lft == nullptr) lft = new Node();
            zero.ins(tins);
            lft->add(v, k+1, tins);
        }
        else {
            if (rgt == nullptr) rgt = new Node();
            one.ins(tins);
            rgt->add(v, k+1, tins);
        }
    }
    int get(int v, int k, int lb, int rb) {
        if (k == MAX_P2) return 0;
        //
        int i = (v>>(MAX_P2-1-k))%2;
        //
        auto it1 = zero.upper_bound(lb-1);
        bool lftFind = (it1 != zero.end() and (*it1) <= rb);
        //
        auto it2 = one.upper_bound(lb-1);
        bool rgtFind = (it2 != one.end() and (*it2) <= rb);
        //
        if ((i == 0 and !rgtFind) or (i == 1 and lftFind)) {
            return lft->get(v, k+1, lb, rb);
        }
        else {
            return (1<<(MAX_P2-1-k))+rgt->get(v, k+1, lb, rb);
        }
    }
    set<int> zero, one;
    Node* lft, * rgt;
};

signed main() {
    cin.tie(0);
    // ios_base::sync_with_stdio(0);

    int q;
    cin >> q;
    par[0] = -1;

    xorTill[0] = 0;
    int n = 1;
    vector<pii> queries;
    for (int i = 0; i < q; i++) {
        string typ;
        cin >> typ;
        if (typ == "Add") {
            int parentNode, edgeWeight;
            cin >> parentNode >> edgeWeight;
            // bitset<MAX_P2> bit(edgeWeight);
            // bitset<MAX_P2> newBit;
            // for (int i = 0; i < MAX_P2; i++) newBit[i] = bit[MAX_P2-i-1];
            // edgeWeight = newBit.to_ulong();
            parentNode--;
            int curNode = n;
            graph[parentNode].pb(curNode);
            par[curNode] = parentNode;
            xorTill[curNode] = xorTill[parentNode]^edgeWeight;
            queries.pb(pii(curNode, -1));
            n++;
        }
        else {
            int a, b;
            cin >> a >> b;
            a--;
            b--;
            queries.pb(pii(a, b));
        }
    }

    dfs(0);

    Node* root = new Node();
    root->add(0, 0, disc[0]);
    for (pii i : queries) {
        if (i.s == -1) {
            root->add(xorTill[i.f], 0, disc[i.f]);
        }
        else {
            int aNode = i.f;
            int bNode = i.s;
            int xorDefault = xorTill[aNode];
            int xorMax = root->get(xorDefault, 0, disc[bNode], rightMost[bNode]);
            cout << (xorDefault^xorMax) << endl;
        }
    }

    int d = 0;
    d++;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5452 KB Output is correct
4 Correct 4 ms 5580 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5452 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5196 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 4 ms 5580 KB Output is correct
12 Correct 4 ms 5636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5452 KB Output is correct
4 Correct 4 ms 5580 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5452 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5196 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 4 ms 5580 KB Output is correct
12 Correct 4 ms 5636 KB Output is correct
13 Correct 11 ms 6860 KB Output is correct
14 Correct 12 ms 8396 KB Output is correct
15 Correct 13 ms 10124 KB Output is correct
16 Correct 14 ms 11532 KB Output is correct
17 Correct 11 ms 6740 KB Output is correct
18 Correct 13 ms 8336 KB Output is correct
19 Correct 13 ms 10000 KB Output is correct
20 Correct 14 ms 11356 KB Output is correct
21 Correct 11 ms 6732 KB Output is correct
22 Correct 13 ms 8396 KB Output is correct
23 Correct 14 ms 9932 KB Output is correct
24 Correct 14 ms 11388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1226 ms 149768 KB Output is correct
2 Correct 1743 ms 279776 KB Output is correct
3 Correct 2410 ms 403416 KB Output is correct
4 Correct 2940 ms 524288 KB Output is correct
5 Correct 1473 ms 147876 KB Output is correct
6 Correct 2067 ms 275624 KB Output is correct
7 Correct 2569 ms 397084 KB Output is correct
8 Correct 3119 ms 518324 KB Output is correct
9 Correct 1476 ms 147340 KB Output is correct
10 Correct 2071 ms 276576 KB Output is correct
11 Correct 2526 ms 400204 KB Output is correct
12 Correct 2952 ms 520924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5452 KB Output is correct
4 Correct 4 ms 5580 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5452 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5196 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 4 ms 5580 KB Output is correct
12 Correct 4 ms 5636 KB Output is correct
13 Correct 11 ms 6860 KB Output is correct
14 Correct 12 ms 8396 KB Output is correct
15 Correct 13 ms 10124 KB Output is correct
16 Correct 14 ms 11532 KB Output is correct
17 Correct 11 ms 6740 KB Output is correct
18 Correct 13 ms 8336 KB Output is correct
19 Correct 13 ms 10000 KB Output is correct
20 Correct 14 ms 11356 KB Output is correct
21 Correct 11 ms 6732 KB Output is correct
22 Correct 13 ms 8396 KB Output is correct
23 Correct 14 ms 9932 KB Output is correct
24 Correct 14 ms 11388 KB Output is correct
25 Correct 1226 ms 149768 KB Output is correct
26 Correct 1743 ms 279776 KB Output is correct
27 Correct 2410 ms 403416 KB Output is correct
28 Correct 2940 ms 524288 KB Output is correct
29 Correct 1473 ms 147876 KB Output is correct
30 Correct 2067 ms 275624 KB Output is correct
31 Correct 2569 ms 397084 KB Output is correct
32 Correct 3119 ms 518324 KB Output is correct
33 Correct 1476 ms 147340 KB Output is correct
34 Correct 2071 ms 276576 KB Output is correct
35 Correct 2526 ms 400204 KB Output is correct
36 Correct 2952 ms 520924 KB Output is correct
37 Correct 2588 ms 150952 KB Output is correct
38 Correct 3372 ms 279676 KB Output is correct
39 Correct 3564 ms 406764 KB Output is correct
40 Correct 3531 ms 524288 KB Output is correct
41 Correct 2479 ms 148560 KB Output is correct
42 Correct 3063 ms 275308 KB Output is correct
43 Correct 3251 ms 397640 KB Output is correct
44 Correct 3421 ms 517428 KB Output is correct
45 Correct 2663 ms 147620 KB Output is correct
46 Correct 3283 ms 276708 KB Output is correct
47 Correct 3438 ms 398384 KB Output is correct
48 Correct 3405 ms 520688 KB Output is correct