답안 #211220

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211220 2020-03-19T13:57:13 Z sm1ley Klasika (COCI20_klasika) C++17
110 / 110
3119 ms 452128 KB
#include <vector>
#include <set>
#include <iostream>
#define ll long long
#define endl "\n"
#define IOS cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
using namespace std;

const int N = 2e5+13;
struct Query{
    int t,x,y;
};
struct Node{
    set<int> times;
    Node *bit[2];
    Node(){
        bit[0]=bit[1]=NULL;
    }
};
Node trie;

int timer;
int tin[N], tout[N];
int xr[N]; //distance from node 1 to node i
vector<pair<int,int>> g[N];
vector<Query> question;

void dfs(int v,int p,int val){
    tin[v]=++timer;
    xr[v]=xr[p]^val;
    for(auto t:g[v]){
        if(t.first==p)continue;
        dfs(t.first,v,t.second);
    }
    tout[v]=timer;//++;
}

void insert(int val,int num){
    Node *t = &trie;
    for(int i=30;i>=-1;i--){
        t->times.insert(num);
        if(val&(1<<i)){
            if(t->bit[1]==NULL)t->bit[1]=new Node();
            t = t->bit[1];
        }else{
            if(t->bit[0]==NULL)t->bit[0]=new Node();
            t = t->bit[0];
        }
    }
}
bool can(set<int> &a,int in,int out){
    return a.lower_bound(in)==a.upper_bound(out);
}

//
void get(int val,int in,int out){
    Node *t = &trie;
    int ans = 0;
    for(int i=30;i>=0;i--){
        if(val&(1<<i)){//ned 0
            if(t->bit[0]==NULL || can(t->bit[0]->times,in,out)){
                t = t->bit[1];
            }else{
                t = t->bit[0];
                ans |= (1<<i);
            }
        }
        else{ //ned 1
            if(t->bit[1]==NULL||can(t->bit[1]->times,in,out)){
                t = t->bit[0];
            }else{
                ans |= (1<<i);
                t = t->bit[1];
            }
        }
    }
    cout<<ans<<endl;
}

int q;
int main(){
    IOS;
    cin>>q;
    int num=2;
    for(int i=0;i<q;i++){
        string s;cin>>s;
        int a,b;cin>>a>>b;
        if(s=="Add"){
            question.push_back({1,num,a});
            g[a].push_back({num,b});
            g[num].push_back({a,b});
            num++;
        }
        if(s=="Query")question.push_back({2,a,b});
    }
    dfs(1,1,0);
    insert(0,tin[1]);
    for(int i=0;i<q;i++){
        int t=question[i].t;
        int x=question[i].x;
        int y=question[i].y;
        if(t==1) insert(xr[x],tin[x]);    
        else get(xr[x],tin[y],tout[y]);
    }
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5248 KB Output is correct
2 Correct 8 ms 5376 KB Output is correct
3 Correct 8 ms 5632 KB Output is correct
4 Correct 8 ms 5632 KB Output is correct
5 Correct 7 ms 5248 KB Output is correct
6 Correct 10 ms 5376 KB Output is correct
7 Correct 8 ms 5504 KB Output is correct
8 Correct 8 ms 5632 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5376 KB Output is correct
11 Correct 7 ms 5504 KB Output is correct
12 Correct 8 ms 5632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5248 KB Output is correct
2 Correct 8 ms 5376 KB Output is correct
3 Correct 8 ms 5632 KB Output is correct
4 Correct 8 ms 5632 KB Output is correct
5 Correct 7 ms 5248 KB Output is correct
6 Correct 10 ms 5376 KB Output is correct
7 Correct 8 ms 5504 KB Output is correct
8 Correct 8 ms 5632 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5376 KB Output is correct
11 Correct 7 ms 5504 KB Output is correct
12 Correct 8 ms 5632 KB Output is correct
13 Correct 11 ms 6528 KB Output is correct
14 Correct 12 ms 7936 KB Output is correct
15 Correct 14 ms 9216 KB Output is correct
16 Correct 18 ms 10368 KB Output is correct
17 Correct 11 ms 6528 KB Output is correct
18 Correct 13 ms 7808 KB Output is correct
19 Correct 14 ms 9088 KB Output is correct
20 Correct 16 ms 10368 KB Output is correct
21 Correct 12 ms 6528 KB Output is correct
22 Correct 13 ms 7808 KB Output is correct
23 Correct 16 ms 9088 KB Output is correct
24 Correct 16 ms 10240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 990 ms 127840 KB Output is correct
2 Correct 1586 ms 238660 KB Output is correct
3 Correct 2350 ms 344820 KB Output is correct
4 Correct 2828 ms 451368 KB Output is correct
5 Correct 1068 ms 126528 KB Output is correct
6 Correct 1586 ms 235960 KB Output is correct
7 Correct 2566 ms 340840 KB Output is correct
8 Correct 2880 ms 445620 KB Output is correct
9 Correct 1045 ms 126148 KB Output is correct
10 Correct 1705 ms 236704 KB Output is correct
11 Correct 2477 ms 342736 KB Output is correct
12 Correct 2854 ms 447260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5248 KB Output is correct
2 Correct 8 ms 5376 KB Output is correct
3 Correct 8 ms 5632 KB Output is correct
4 Correct 8 ms 5632 KB Output is correct
5 Correct 7 ms 5248 KB Output is correct
6 Correct 10 ms 5376 KB Output is correct
7 Correct 8 ms 5504 KB Output is correct
8 Correct 8 ms 5632 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5376 KB Output is correct
11 Correct 7 ms 5504 KB Output is correct
12 Correct 8 ms 5632 KB Output is correct
13 Correct 11 ms 6528 KB Output is correct
14 Correct 12 ms 7936 KB Output is correct
15 Correct 14 ms 9216 KB Output is correct
16 Correct 18 ms 10368 KB Output is correct
17 Correct 11 ms 6528 KB Output is correct
18 Correct 13 ms 7808 KB Output is correct
19 Correct 14 ms 9088 KB Output is correct
20 Correct 16 ms 10368 KB Output is correct
21 Correct 12 ms 6528 KB Output is correct
22 Correct 13 ms 7808 KB Output is correct
23 Correct 16 ms 9088 KB Output is correct
24 Correct 16 ms 10240 KB Output is correct
25 Correct 990 ms 127840 KB Output is correct
26 Correct 1586 ms 238660 KB Output is correct
27 Correct 2350 ms 344820 KB Output is correct
28 Correct 2828 ms 451368 KB Output is correct
29 Correct 1068 ms 126528 KB Output is correct
30 Correct 1586 ms 235960 KB Output is correct
31 Correct 2566 ms 340840 KB Output is correct
32 Correct 2880 ms 445620 KB Output is correct
33 Correct 1045 ms 126148 KB Output is correct
34 Correct 1705 ms 236704 KB Output is correct
35 Correct 2477 ms 342736 KB Output is correct
36 Correct 2854 ms 447260 KB Output is correct
37 Correct 1821 ms 129240 KB Output is correct
38 Correct 2730 ms 238372 KB Output is correct
39 Correct 2554 ms 347432 KB Output is correct
40 Correct 3019 ms 452128 KB Output is correct
41 Correct 1667 ms 127136 KB Output is correct
42 Correct 2464 ms 235808 KB Output is correct
43 Correct 3039 ms 340920 KB Output is correct
44 Correct 3119 ms 445060 KB Output is correct
45 Correct 1864 ms 126436 KB Output is correct
46 Correct 2567 ms 236604 KB Output is correct
47 Correct 2787 ms 341312 KB Output is correct
48 Correct 2914 ms 447092 KB Output is correct