Submission #1354115

#TimeUsernameProblemLanguageResultExecution timeMemory
1354115minhtienKlasika (COCI20_klasika)C++20
33 / 110
47 ms13156 KiB
#include <bits/stdc++.h>
#define ll long long
#define ii pair<int,int>
#define iii pair<string,pair<int,pair<int,int>>>
#define fi first
#define se second
using namespace std;
const int N=2e5+6;
int n;
int k=2;
ll dp[N],dp1[N];
vector<iii>v2;
vector<ii>v1[N];
int in[N],out[N];
ll a3[N];
int t=0;
ll tong1=0;
struct trie{
    trie *child[2];
    trie(){
        for(int i=0;i<2;i++){
            child[i]=NULL;
        }
    }
};
trie *a1;
void add(ll x){
    trie *p=a1;
    for(int j=30;j>=0;j--){
        int bit1=(x>>j)&1;
        if(p->child[bit1]==NULL){
            p->child[bit1]=new trie();
        }
        p=p->child[bit1];
    }
}
ll get(ll s){
    trie *p=a1;
    ll tong3=0;
    for(int j=30;j>=0;j--){
        int bit1=(s>>j)&1;
        if(p->child[1^bit1]!=NULL){
            tong3+=(1LL<<j);
            p=p->child[1^bit1];
        }
        else{
            p=p->child[bit1];
        }
    }
    return tong3;
}
void dfs(int u,int pr){
    for(auto x:v1[u]){
        int node=x.fi;
        int s1=x.se;
        if(node!=pr){
            dp[node]=dp[u]^s1;
            dfs(node,u);
        }
    }
}
void dfs1(int u,int pr){
    t++;
    in[u]=out[u]=t;
    for(auto x:v1[u]){
        int node=x.fi;
        int s1=x.se;
        if(node!=pr){
            dfs1(node,u);
            out[u]=max(out[u],out[node]);
        }
    }
}
void sub2(){
    for(auto x:v2){
        string x1=x.fi;
        if(x1=="Add"){
            int y=x.se.fi;
            int z=x.se.se.fi;
            v1[y].push_back({k,z});
            v1[k].push_back({y,z});
            for(int i=1;i<=n;i++){
                in[i]=0;
                out[i]=0;
            }
            dfs1(1,-1);
            t=0;
            k++;
        }
        else{
            int y=x.se.fi;
            int z=x.se.se.fi;
            ll tong=0;
            dfs(1,-1);
            for(int i=1;i<=n;i++){
                if(in[i]>=in[z] && out[i]<=out[z]){
                    tong=max(tong,dp[y]^dp[i]);
                }
            }
            cout << tong << "\n";
        }
    }
}
void sub3(){
    for(auto x:v2){
        string x1=x.fi;
        ll tong2=x.se.se.fi;
        ll tong4=get(tong2);
        tong1=max(tong1,tong4);
        if(x1=="Add"){
            dp1[k]=max(dp1[k],(dp1[k-1]^tong2));
            add(dp1[k]);
        }
        else{
            a3[x.se.se.se]=tong1;
        }
    }
    for(int i=1;i<=n;i++){
        cout << a3[i] << "\n";
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    cin >>n;
    a1=new trie();
    add(0);
    for(int i=1;i<=n;i++){
        string x;
        cin >>x;
        if(x=="Add"){
            int y,z;
            cin >>y >>z;
            v2.push_back({x,{y,{z,i}}});
        }
        else{
            int y,z;
            cin >>y >>z;
            v2.push_back({x,{y,{z,i}}});
        }
    }
    if(n<=5000) sub2();
    else sub3();
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...