답안 #1091217

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091217 2024-09-20T07:32:50 Z kawaii Klasika (COCI20_klasika) C++17
33 / 110
3617 ms 524288 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], cnt = 0, max_dist = 0;
string s;
vector<int> adj[MAXN];
mt19937_64 rng;

struct Query{
    char ch;
    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);
            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 val){
    if(l > rt || r < lf) return 0;
    if(lf <= l && r <= rt && r - l + 1 <= max_dist) 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){
        if(r - l + 1 <= max_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) st[crr].add(val);
}

void solve(){
    dfs(1);
    node = 1;
    max_dist = max(1, cnt / 64);
    for(int i = 1; i <= n; i++){
        if(q[i].ch == 'A') node++, 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:66:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |     int mid = l + r >> 1;
      |               ~~^~~
klasika.cpp: In function 'void update(int, int, int, int, int)':
klasika.cpp:76:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   76 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 48984 KB Output is correct
2 Correct 21 ms 49080 KB Output is correct
3 Correct 22 ms 48960 KB Output is correct
4 Correct 29 ms 48988 KB Output is correct
5 Correct 23 ms 48988 KB Output is correct
6 Correct 20 ms 48988 KB Output is correct
7 Correct 25 ms 48988 KB Output is correct
8 Correct 24 ms 48988 KB Output is correct
9 Correct 25 ms 48984 KB Output is correct
10 Correct 20 ms 49068 KB Output is correct
11 Correct 20 ms 48992 KB Output is correct
12 Correct 20 ms 49092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 48984 KB Output is correct
2 Correct 21 ms 49080 KB Output is correct
3 Correct 22 ms 48960 KB Output is correct
4 Correct 29 ms 48988 KB Output is correct
5 Correct 23 ms 48988 KB Output is correct
6 Correct 20 ms 48988 KB Output is correct
7 Correct 25 ms 48988 KB Output is correct
8 Correct 24 ms 48988 KB Output is correct
9 Correct 25 ms 48984 KB Output is correct
10 Correct 20 ms 49068 KB Output is correct
11 Correct 20 ms 48992 KB Output is correct
12 Correct 20 ms 49092 KB Output is correct
13 Correct 39 ms 50260 KB Output is correct
14 Correct 35 ms 50932 KB Output is correct
15 Correct 29 ms 51292 KB Output is correct
16 Correct 27 ms 51936 KB Output is correct
17 Correct 22 ms 49496 KB Output is correct
18 Correct 23 ms 50012 KB Output is correct
19 Correct 30 ms 51036 KB Output is correct
20 Correct 29 ms 51284 KB Output is correct
21 Correct 29 ms 49456 KB Output is correct
22 Correct 25 ms 50008 KB Output is correct
23 Correct 28 ms 51032 KB Output is correct
24 Correct 30 ms 51340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3617 ms 231216 KB Output is correct
2 Correct 1972 ms 328684 KB Output is correct
3 Correct 2658 ms 434496 KB Output is correct
4 Runtime error 1582 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 48984 KB Output is correct
2 Correct 21 ms 49080 KB Output is correct
3 Correct 22 ms 48960 KB Output is correct
4 Correct 29 ms 48988 KB Output is correct
5 Correct 23 ms 48988 KB Output is correct
6 Correct 20 ms 48988 KB Output is correct
7 Correct 25 ms 48988 KB Output is correct
8 Correct 24 ms 48988 KB Output is correct
9 Correct 25 ms 48984 KB Output is correct
10 Correct 20 ms 49068 KB Output is correct
11 Correct 20 ms 48992 KB Output is correct
12 Correct 20 ms 49092 KB Output is correct
13 Correct 39 ms 50260 KB Output is correct
14 Correct 35 ms 50932 KB Output is correct
15 Correct 29 ms 51292 KB Output is correct
16 Correct 27 ms 51936 KB Output is correct
17 Correct 22 ms 49496 KB Output is correct
18 Correct 23 ms 50012 KB Output is correct
19 Correct 30 ms 51036 KB Output is correct
20 Correct 29 ms 51284 KB Output is correct
21 Correct 29 ms 49456 KB Output is correct
22 Correct 25 ms 50008 KB Output is correct
23 Correct 28 ms 51032 KB Output is correct
24 Correct 30 ms 51340 KB Output is correct
25 Correct 3617 ms 231216 KB Output is correct
26 Correct 1972 ms 328684 KB Output is correct
27 Correct 2658 ms 434496 KB Output is correct
28 Runtime error 1582 ms 524288 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -