답안 #1091206

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091206 2024-09-20T06:35:13 Z kawaii Klasika (COCI20_klasika) C++14
0 / 110
379 ms 211008 KB
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define fi first
#define se second

const int MAXN = 2e5 + 5;
int t, n, m, k, node = 1, a[MAXN], val[MAXN], tin[MAXN], tout[MAXN], cnt = 0, mod = 1e9 + 7;
vector<int> adj[MAXN];
mt19937_64 rng;

int mul(int x, int y){
    if(y == 0) return 1;
    int ans = mul(x, y / 2);
    if(y % 2 == 0) return (ans * ans) % mod;
    else return (((ans * ans) % mod) * x) % mod;
}

struct Query{
    string operation;
    int x, y;
};

Query q[MAXN];

void dfs(int u){
    tin[u] = ++cnt;
    for(auto i: adj[u]) dfs(i);
    tout[u] = cnt;
}

struct Node{
    int size = 0;
    vector<int> next[2];
    void add(int x){
        if(!size){   
            next[0].push_back(0);
            next[1].push_back(0);
        }
        int crr_id = 0;
        for(int i = 30; i >= 0; i--){
            bool bit = (((1 << i) & x) > 0);
            if(!next[bit][crr_id]){
                next[0].push_back(0);
                next[1].push_back(0);
                next[bit][crr_id] = ++size;
            }
            crr_id = next[bit][crr_id];
        }
    }
    int query(int x){
        if(!size){   
            next[0].push_back(0);
            next[1].push_back(0);
        }
        int crr_id = 0, ans = 0;
        for(int i = 30; i >= 0; i--){
            bool bit = !((1 << i) & x);
            // cout << bit <<" "<< crr_id <<"    "<< next[0][crr_id] <<" "<< next[1][crr_id] << "\n";
            if(!next[bit][crr_id]) bit = !bit;
            else ans |= (1 << i);
            if(!next[bit][crr_id]) return ans;
            crr_id = next[bit][crr_id]; 
        }
        return ans;
    }
};

Node st[MAXN * 4];

int get(int crr, int l, int r, int lf, int rt, int val){
    if(l > rt || r < lf) return 0;
    if(lf <= l && r <= rt) return st[crr].query(val);
    int mid = l + r >> 1;
    return max(get(crr * 2, l, mid, lf, rt, val), get(crr * 2 + 1, mid + 1, r, lf, rt, val));
}

void update(int crr, int l, int r, int id, int val){
    if(l > id || r < id) return;
    if(l == r){
        st[crr].add(val);
        return;
    }
    int mid = l + r >> 1;
    update(crr * 2, l, mid, id, val);
    update(crr * 2 + 1, mid + 1, r, id, val);
    st[crr].add(val);
}

void solve(){
    dfs(1);
    node = 1;
    update(1, 1, cnt, 1, 0);
    // cout << cnt << "\n";
    for(int i = 1; i <= n; i++){
        if(q[i].operation == "Add") node++, update(1, 1, cnt, tin[node], val[node]);
        else cout << get(1, 1, cnt, tin[q[i].y], tout[q[i].y], val[q[i].x]) << "\n";
    }
    cout << get(1, 1, cnt, 1, cnt, 6) << "\n";
}

signed main(){
    ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
    // rng.seed((int)main ^ time(0));
    #ifdef Kawaii
        auto starttime = chrono::high_resolution_clock::now();
    #endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> q[i].operation >> q[i].x >> q[i].y;
        if(q[i].operation == "Add"){
            val[++node] = val[q[i].x] ^ q[i].y;
            adj[q[i].x].push_back(node);
        }
    }
    solve();

    #ifdef Kawaii
        auto endtime = chrono::high_resolution_clock::now();
        auto duration = chrono::duration_cast<chrono::milliseconds>(endtime - starttime).count(); 
        cout << "\n=====" << "\nUsed: " << duration << " ms\n";
    #endif
}

Compilation message

klasika.cpp: In function 'int get(int, int, int, int, int, int)':
klasika.cpp:74:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   74 |     int mid = l + r >> 1;
      |               ~~^~~
klasika.cpp: In function 'void update(int, int, int, int, int)':
klasika.cpp:84:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 56912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 56912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 379 ms 211008 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 56912 KB Output isn't correct
2 Halted 0 ms 0 KB -