답안 #889432

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
889432 2023-12-19T16:39:09 Z dimashhh Klasika (COCI20_klasika) C++17
33 / 110
557 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1, MOD = 998244353,b = 30;
typedef long long ll;

vector<pair<int,int>> ans;
struct node{
    node *next[2];
    int col = 0;
    node(){
        col = 0;
        for(int i = 0;i < 2;i++){
            next[i] = nullptr;
        }
    }
};
using pnode = node *;
void add(pnode &x,int val,int delta){
    if(!x) x = new node();
    pnode f = x;
    for(int i = b;i >= 0;i--){
        int nv = ((val >> i) & 1);
        // cout << nv << "x\n";
        if(!x->next[nv]){
            x->next[nv] = new node();
        }
        x = x->next[nv];
        x->col+=delta;
    }
    x = f;

}
int get(int val,pnode x){
    int res = 0;
    if(!x) return 0;
    for(int i = b;i >= 0;i--){
        int nv = ((val >> i) & 1);
        nv = (!nv);
        // cout << (x->next[nv] ? 1 : 0) << ' ' << (x->next[!nv] ? 1 : 0) << '\n';
        if(x->next[nv] && x->next[nv]->col){
            res += (1ll << i);
            x = x->next[nv];
        }else if(x->next[(!nv)] && x->next[!nv]->col){
            x = x->next[(!nv)];
        }
    }
    return res;
}
pnode T[N * 4];
int n,q,tadd[N],d[N],s[N];
vector<pair<int,int>> g[N],qr[N];
void dfs(int v){
    s[v] = 1;
    for(auto [to,w]:g[v]){
        d[to] = (d[v] ^ w);
        dfs(to);
        s[v] += s[to];
    }
}
void upd(int pos,int val,int d,int v = 1,int tl = 1,int tr = N){
    add(T[v],val,d);
    // if(tl == tr && tl == 1){
    //     cout << val << "x\n";
    // }
    // cout << v << " " << tl << ' ' << tr << "f\n";
    if(tl == tr) return;
    int tm = (tl + tr) >> 1;
    if(pos <= tm) upd(pos,val,d,v+v,tl,tm);
    else upd(pos,val,d,v+v+1,tm+1,tr);
}
void add_sub(int v,int dd){
    upd(tadd[v],d[v],dd);
    // cout << tadd[v] << ' ' << d[v] << ' ' << dd << "q\n";
    for(auto [to,w]:g[v]){
        add_sub(to,dd);
    }
}
int get(int l,int r,int x,int v = 1,int tl = 1,int tr = N){
    if(l > r || tl > r || l > tr) return 0;
    if(tl >= l && tr <= r){
        // cout << v << " " << tl << ' ' << tr << '\n';
        return get(x,T[v]);
    }else{
        int tm = (tl + tr) >> 1;
        return max(get(l,r,x,v+v,tl,tm),get(l,r,x,v+v+1,tm+1,tr));
    }
}
void _dfs(int v,bool keep = true){
    int b = -1;
    for(auto [to,w]:g[v]){
        if(b == -1 || s[to] > s[b]){
            b = to;
        }
    }
    for(auto [to,w]:g[v]){
        if(to != b){
            _dfs(to,false);
        }
    }
    if(b != -1){
        _dfs(b,1);
    }
    for(auto [to,w]:g[v]){
        if(to == b) continue;
        add_sub(to,1);
    }
    upd(tadd[v],d[v],1);
    // cout << tadd[v] << ' ' << d[v] << ' ' << 1 << "q\n";
    // cout << b << '\n';
    for(auto [x,y]:qr[v]){
        // cout << 1 << ' ' << y - 1 << ' ' << x << ' ' << d[x] << " " << get(1,y - 1,d[x]) << '\n';
        ans.push_back({y,get(1,y - 1,d[x])});
        // exit(0);
    }
    if(!keep) add_sub(v,-1);
}
void test(){
    cin >> q;
    n = 1;
    tadd[1] = 1;
    for(int i = 1;i <= q;i++){
        string tp;
        cin >> tp;
        if(tp[0] == 'A'){
            n++;
            int x,y;
            cin >> x >> y;
            g[x].push_back({n,y});
            tadd[n] = i + 1;
        }else{
            int a,b;
            cin >> a >> b;
            qr[b].push_back({a,i + 1});
        }
    }
    dfs(1);
    _dfs(1);
    sort(ans.begin(),ans.end());
    for(auto [x,y]:ans){
        cout << y << '\n';
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1; // cin >> T;
    while (T--)
        test();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 4 ms 13916 KB Output is correct
3 Correct 5 ms 14684 KB Output is correct
4 Correct 4 ms 15196 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13932 KB Output is correct
7 Correct 6 ms 14684 KB Output is correct
8 Correct 5 ms 15196 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 14252 KB Output is correct
11 Correct 5 ms 14684 KB Output is correct
12 Correct 5 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 4 ms 13916 KB Output is correct
3 Correct 5 ms 14684 KB Output is correct
4 Correct 4 ms 15196 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13932 KB Output is correct
7 Correct 6 ms 14684 KB Output is correct
8 Correct 5 ms 15196 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 14252 KB Output is correct
11 Correct 5 ms 14684 KB Output is correct
12 Correct 5 ms 15196 KB Output is correct
13 Correct 11 ms 19288 KB Output is correct
14 Correct 16 ms 24924 KB Output is correct
15 Correct 21 ms 30812 KB Output is correct
16 Correct 26 ms 35676 KB Output is correct
17 Correct 12 ms 19036 KB Output is correct
18 Correct 20 ms 24664 KB Output is correct
19 Correct 30 ms 30292 KB Output is correct
20 Correct 37 ms 35412 KB Output is correct
21 Correct 12 ms 19032 KB Output is correct
22 Correct 17 ms 24668 KB Output is correct
23 Correct 24 ms 30300 KB Output is correct
24 Correct 31 ms 35420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 557 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 4 ms 13916 KB Output is correct
3 Correct 5 ms 14684 KB Output is correct
4 Correct 4 ms 15196 KB Output is correct
5 Correct 4 ms 13400 KB Output is correct
6 Correct 4 ms 13932 KB Output is correct
7 Correct 6 ms 14684 KB Output is correct
8 Correct 5 ms 15196 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 14252 KB Output is correct
11 Correct 5 ms 14684 KB Output is correct
12 Correct 5 ms 15196 KB Output is correct
13 Correct 11 ms 19288 KB Output is correct
14 Correct 16 ms 24924 KB Output is correct
15 Correct 21 ms 30812 KB Output is correct
16 Correct 26 ms 35676 KB Output is correct
17 Correct 12 ms 19036 KB Output is correct
18 Correct 20 ms 24664 KB Output is correct
19 Correct 30 ms 30292 KB Output is correct
20 Correct 37 ms 35412 KB Output is correct
21 Correct 12 ms 19032 KB Output is correct
22 Correct 17 ms 24668 KB Output is correct
23 Correct 24 ms 30300 KB Output is correct
24 Correct 31 ms 35420 KB Output is correct
25 Runtime error 557 ms 524288 KB Execution killed with signal 9
26 Halted 0 ms 0 KB -