답안 #1091248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091248 2024-09-20T08:59:42 Z kawaii Klasika (COCI20_klasika) C++14
110 / 110
2387 ms 319200 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, val[MAXN], tin[MAXN], tout[MAXN], key[MAXN], cnt = 0, max_dist = 0, min_dist = 32;
string s;
vector<int> adj[MAXN];
bool vst[MAXN];
mt19937_64 rng;

struct Query{
    char ch;
    int x, y;
};

Query q[MAXN];

void dfs(int u){
    tin[u] = ++cnt; key[cnt] = u;
    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);
            if(!next[bit][crr_id]) bit = !bit;
            else ans |= (1 << i);
            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 v){
    if(l > rt || r < lf) return 0;
    if(lf <= l && r <= rt && r - l + 1 <= max_dist){
        if(r - l + 1 >= min_dist) return st[crr].query(v);
        int ans = 0;
        for(int i = l; i <= r; i++){
            if(vst[key[i]]) ans = max(ans, v ^ val[key[i]]);
        }
        return ans;
    }
    int mid = l + r >> 1;
    return max(get(crr * 2, l, mid, lf, rt, v), get(crr * 2 + 1, mid + 1, r, lf, rt, v));
}

void update(int crr, int l, int r, int id, int val){
    if(l > id || r < id) return;
    if(l == r){
        if(r - l + 1 <= max_dist && r - l + 1 >= min_dist) 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);
    if(r - l + 1 <= max_dist && r - l + 1 >= min_dist) st[crr].add(val);
}

void solve(){
    dfs(1);
    node = 1; vst[node] = 1;
    max_dist = max(1, cnt / 32);
    for(int i = 1; i <= n; i++){
        if(q[i].ch == 'A') vst[++node] = 1, update(1, 1, cnt, tin[node], val[node]);
        else{
            int ans = get(1, 1, cnt, tin[q[i].y], tout[q[i].y], val[q[i].x]);
            if(tin[q[i].y] == 1) ans = max(ans, val[q[i].x]);
            cout << ans << "\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 >> s >> q[i].x >> q[i].y;
        if(s == "Add"){
            q[i].ch = 'A';
            val[++node] = val[q[i].x] ^ q[i].y;
            adj[q[i].x].push_back(node);
        }
        else q[i].ch = 'Q';
    }
    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 Correct 21 ms 48988 KB Output is correct
2 Correct 20 ms 48988 KB Output is correct
3 Correct 20 ms 49144 KB Output is correct
4 Correct 20 ms 48988 KB Output is correct
5 Correct 20 ms 48864 KB Output is correct
6 Correct 20 ms 48988 KB Output is correct
7 Correct 20 ms 48988 KB Output is correct
8 Correct 20 ms 48984 KB Output is correct
9 Correct 20 ms 48988 KB Output is correct
10 Correct 20 ms 49040 KB Output is correct
11 Correct 21 ms 48984 KB Output is correct
12 Correct 20 ms 49012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 48988 KB Output is correct
2 Correct 20 ms 48988 KB Output is correct
3 Correct 20 ms 49144 KB Output is correct
4 Correct 20 ms 48988 KB Output is correct
5 Correct 20 ms 48864 KB Output is correct
6 Correct 20 ms 48988 KB Output is correct
7 Correct 20 ms 48988 KB Output is correct
8 Correct 20 ms 48984 KB Output is correct
9 Correct 20 ms 48988 KB Output is correct
10 Correct 20 ms 49040 KB Output is correct
11 Correct 21 ms 48984 KB Output is correct
12 Correct 20 ms 49012 KB Output is correct
13 Correct 21 ms 48988 KB Output is correct
14 Correct 22 ms 48988 KB Output is correct
15 Correct 22 ms 49196 KB Output is correct
16 Correct 22 ms 49500 KB Output is correct
17 Correct 21 ms 48988 KB Output is correct
18 Correct 21 ms 48988 KB Output is correct
19 Correct 22 ms 49240 KB Output is correct
20 Correct 22 ms 49204 KB Output is correct
21 Correct 23 ms 49068 KB Output is correct
22 Correct 21 ms 48988 KB Output is correct
23 Correct 22 ms 49340 KB Output is correct
24 Correct 24 ms 49240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1503 ms 132600 KB Output is correct
2 Correct 986 ms 189164 KB Output is correct
3 Correct 1223 ms 255408 KB Output is correct
4 Correct 901 ms 314972 KB Output is correct
5 Correct 2387 ms 102192 KB Output is correct
6 Correct 2051 ms 167608 KB Output is correct
7 Correct 1577 ms 256660 KB Output is correct
8 Correct 1784 ms 304336 KB Output is correct
9 Correct 1339 ms 129060 KB Output is correct
10 Correct 1631 ms 188260 KB Output is correct
11 Correct 1296 ms 248628 KB Output is correct
12 Correct 783 ms 316848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 48988 KB Output is correct
2 Correct 20 ms 48988 KB Output is correct
3 Correct 20 ms 49144 KB Output is correct
4 Correct 20 ms 48988 KB Output is correct
5 Correct 20 ms 48864 KB Output is correct
6 Correct 20 ms 48988 KB Output is correct
7 Correct 20 ms 48988 KB Output is correct
8 Correct 20 ms 48984 KB Output is correct
9 Correct 20 ms 48988 KB Output is correct
10 Correct 20 ms 49040 KB Output is correct
11 Correct 21 ms 48984 KB Output is correct
12 Correct 20 ms 49012 KB Output is correct
13 Correct 21 ms 48988 KB Output is correct
14 Correct 22 ms 48988 KB Output is correct
15 Correct 22 ms 49196 KB Output is correct
16 Correct 22 ms 49500 KB Output is correct
17 Correct 21 ms 48988 KB Output is correct
18 Correct 21 ms 48988 KB Output is correct
19 Correct 22 ms 49240 KB Output is correct
20 Correct 22 ms 49204 KB Output is correct
21 Correct 23 ms 49068 KB Output is correct
22 Correct 21 ms 48988 KB Output is correct
23 Correct 22 ms 49340 KB Output is correct
24 Correct 24 ms 49240 KB Output is correct
25 Correct 1503 ms 132600 KB Output is correct
26 Correct 986 ms 189164 KB Output is correct
27 Correct 1223 ms 255408 KB Output is correct
28 Correct 901 ms 314972 KB Output is correct
29 Correct 2387 ms 102192 KB Output is correct
30 Correct 2051 ms 167608 KB Output is correct
31 Correct 1577 ms 256660 KB Output is correct
32 Correct 1784 ms 304336 KB Output is correct
33 Correct 1339 ms 129060 KB Output is correct
34 Correct 1631 ms 188260 KB Output is correct
35 Correct 1296 ms 248628 KB Output is correct
36 Correct 783 ms 316848 KB Output is correct
37 Correct 1259 ms 135068 KB Output is correct
38 Correct 1074 ms 189200 KB Output is correct
39 Correct 1072 ms 255420 KB Output is correct
40 Correct 859 ms 319200 KB Output is correct
41 Correct 234 ms 105952 KB Output is correct
42 Correct 419 ms 162868 KB Output is correct
43 Correct 646 ms 244536 KB Output is correct
44 Correct 966 ms 299532 KB Output is correct
45 Correct 381 ms 108200 KB Output is correct
46 Correct 455 ms 170996 KB Output is correct
47 Correct 512 ms 237612 KB Output is correct
48 Correct 550 ms 308024 KB Output is correct