답안 #1098649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098649 2024-10-09T16:20:55 Z hiensumi Klasika (COCI20_klasika) C++14
110 / 110
2564 ms 434292 KB
#include <bits/stdc++.h>
using namespace std;
#define fod(i,a,b) for(int i = (int) (a); i <= (int) (b); i++)
#define fok(i,a,b) for(int i = (int) (a); i >= (int) (b); i--)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define el '\n'
#define ve vector
#define vi ve<int>
#define vll ve<ll>
#define mask(a) (1LL<<(a))
#define BIT(msk,i) (msk>>(i)&1LL)
using namespace std;
template <class T> bool mini(T &a, T b){ return (a > (b)) ? a = (b), 1 : 0; }
template <class T> bool maxi(T &a, T b){ return (a < (b)) ? a = (b), 1 : 0; }
#define name "klasika"

int n = 1, q;

const int N = 2e5;

struct Query{
    bool new_node;
    int x, y;
}query[N + 5];

ve <ve<pii>> g;

int in[N + 5], out[N + 5], timedfs = 0;
int f[N + 5];

void dfs(int u, int p){
    in[u] = ++timedfs;
    for(pii who : g[u]) if(who.fi != p){
        int v, w; tie(v, w) = who;
        f[v] = f[u] xor w;
        dfs(v, u);
    }
    out[u] = timedfs;
}

struct Node{
    set <int> ids;
    Node* nxt[2];
    Node(){
        ids.clear();
        fod(j,0,1) nxt[j] = NULL;
    }
};

Node* root = new Node();

inline void add(int val, int id){
    Node* cur = root;
    fok(j,30,0){
        int w = BIT(val, j);
        if(cur -> nxt[w] == NULL) cur -> nxt[w] = new Node();
        cur = cur -> nxt[w];
        cur -> ids.insert(id);
    }
}

int get(int val, int l, int r){
    Node* cur = root;
    int res = 0;

    fok(j,30,0){
        int w = BIT(val, j);
        if(cur -> nxt[w^1] == NULL) cur = cur -> nxt[w];
        else{
            if(cur -> nxt[w^1] -> ids.lower_bound(l) == cur -> nxt[w^1] -> ids.upper_bound(r)){
                cur = cur -> nxt[w];
                continue;
            }
            res += mask(j);
            cur = cur -> nxt[w^1];
        }
    }

    return res;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> q;

    g.resize(q + 1);

    fod(i,1,q){
        string s; cin >> s;
        int x, y; cin >> x >> y;

        if(s == "Add"){
            n++;
            g[x].pb(mp(n, y));
            query[i] = Query{1, n, y};
        }
        else query[i] = Query{0, x, y};
    }

    dfs(1, 0);

    add(f[1], in[1]);

    fod(i,1,q){
        if(query[i].new_node){
            add(f[query[i].x], in[query[i].x]);
        }
        else{
            cout << get(f[query[i].x], in[query[i].y], out[query[i].y]) << el;
        }
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 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 0 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 600 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 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 0 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 600 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 4 ms 1884 KB Output is correct
14 Correct 4 ms 3460 KB Output is correct
15 Correct 5 ms 4440 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 2932 KB Output is correct
19 Correct 8 ms 4440 KB Output is correct
20 Correct 8 ms 5464 KB Output is correct
21 Correct 4 ms 1884 KB Output is correct
22 Correct 6 ms 3140 KB Output is correct
23 Correct 8 ms 4188 KB Output is correct
24 Correct 9 ms 5344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 721 ms 122424 KB Output is correct
2 Correct 1088 ms 228364 KB Output is correct
3 Correct 1230 ms 331484 KB Output is correct
4 Correct 1586 ms 433816 KB Output is correct
5 Correct 573 ms 120732 KB Output is correct
6 Correct 1061 ms 224288 KB Output is correct
7 Correct 1530 ms 323352 KB Output is correct
8 Correct 2150 ms 422452 KB Output is correct
9 Correct 579 ms 120408 KB Output is correct
10 Correct 969 ms 225256 KB Output is correct
11 Correct 1597 ms 326204 KB Output is correct
12 Correct 1899 ms 425120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 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 0 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 600 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 4 ms 1884 KB Output is correct
14 Correct 4 ms 3460 KB Output is correct
15 Correct 5 ms 4440 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 2932 KB Output is correct
19 Correct 8 ms 4440 KB Output is correct
20 Correct 8 ms 5464 KB Output is correct
21 Correct 4 ms 1884 KB Output is correct
22 Correct 6 ms 3140 KB Output is correct
23 Correct 8 ms 4188 KB Output is correct
24 Correct 9 ms 5344 KB Output is correct
25 Correct 721 ms 122424 KB Output is correct
26 Correct 1088 ms 228364 KB Output is correct
27 Correct 1230 ms 331484 KB Output is correct
28 Correct 1586 ms 433816 KB Output is correct
29 Correct 573 ms 120732 KB Output is correct
30 Correct 1061 ms 224288 KB Output is correct
31 Correct 1530 ms 323352 KB Output is correct
32 Correct 2150 ms 422452 KB Output is correct
33 Correct 579 ms 120408 KB Output is correct
34 Correct 969 ms 225256 KB Output is correct
35 Correct 1597 ms 326204 KB Output is correct
36 Correct 1899 ms 425120 KB Output is correct
37 Correct 1190 ms 124384 KB Output is correct
38 Correct 1611 ms 229640 KB Output is correct
39 Correct 1882 ms 334000 KB Output is correct
40 Correct 2213 ms 434292 KB Output is correct
41 Correct 1367 ms 121124 KB Output is correct
42 Correct 1763 ms 224036 KB Output is correct
43 Correct 2272 ms 323516 KB Output is correct
44 Correct 2564 ms 421776 KB Output is correct
45 Correct 1211 ms 120752 KB Output is correct
46 Correct 1689 ms 225324 KB Output is correct
47 Correct 1848 ms 324664 KB Output is correct
48 Correct 2273 ms 424892 KB Output is correct