답안 #844431

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844431 2023-09-05T13:10:13 Z vjudge1 Klasika (COCI20_klasika) C++17
33 / 110
562 ms 524288 KB
/* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 18, KK = 30;


struct Query{
  int a, b, t, idx;
};

struct Node{
  Node *l, *r;
  int min_time;
  bool is;
  Node(){
    l = r = nullptr;
    min_time = MOD;
    is = 0;
  }
  void extend(){
    if(l == nullptr){
      l = new Node();
    }
    if(r == nullptr){
      r = new Node();
    }
  }
  void update(int new_time, int x, int dep){
    min_time = min(min_time, new_time);
    is = 1;
    if(dep == -1) return;
    extend();
    if(x&(1<<dep)){
      r->update(new_time, x, dep - 1);
    }else{
      l->update(new_time, x, dep - 1);
    }
  }
  int get(int val, int dep, int qt){
    extend();
    // cout << dep << ' ' << val << ' ' << min_time << ' ' << is << '\n';
    if((1<<dep)&val){
      if(l->is && l->min_time <= qt){
        return (1<<dep) + l->get(val, dep - 1, qt);
      }
      if(r->is && r->min_time <= qt)
        return r->get(val, dep - 1, qt);
      return 0;
    }
    if(r->is && r->min_time <= qt){
      return (1<<dep) + r->get(val, dep - 1, qt);
    }
    if(l->is && l->min_time <= qt)
      return l->get(val, dep - 1, qt);
    return 0;
  }
};

int q, ans[N], sz[N], n = 1, pref[N], bigxo[N];
vector<array<int, 3>> g[N];
vector<Query> Q[N];
vector<Node*> trie;
vector<pair<int, int>> values[N];

void pre(int v){
  sz[v] = 1;
  for(auto U: g[v]){
    int u = U[0];
    pref[u] = pref[v] ^ U[1];
    pre(u);
    sz[v] += sz[u];
  }
}

int calcc(int a, int b){
  return pref[a]^pref[b];
}
void dfs(int v, int w, int tm){
  int big = -1;
  for(auto U: g[v]){
    int u = U[0];
    dfs(u, U[1], U[2]);
    if(big == -1 || sz[u] > sz[big]) big = u;
  }
  if(big != -1){
    swap(trie[v], trie[big]);
    values[v].swap(values[big]);
  }else{
    trie[v] = new Node();
    trie[v]->is = 1;
  }
  for(auto U: g[v]){
    if(U[0] != big){
      for(auto x: values[U[0]]){
        trie[v]->update(x.second, x.first, KK);
        values[v].pb({x.first, x.second});
        // cout << (x.first ^ U[1] ^ bigxo[v]) << ' ' << v << '\n';
      }
    }
  }
  // for(auto f: values[v]){
    // cout << f.first << ' ' << f.second << ' ' << v << '\n';
  // }
  values[v].pb({pref[v], tm});
  trie[v]->update(tm, pref[v], KK);
  for(auto qq: Q[v]){
    int xo = pref[qq.a];
    ans[qq.idx] = trie[v]->get(xo, KK, qq.t);
    // cout <<qq.t << ' ';
  }
  /*
  values[v].pb({w, tm});
  // cout << w << ' ' << tm << '\n';
  trie[v]->update(tm, w, KK);
  */
}

void solve(){
  cin >> q;
  int qq = 0;
  for(int i = 1; i <= q; ++i){
    string s; cin >> s;
    if(s == "Add"){
      int p, w; cin >> p >> w;
      g[p].pb({++n, w, i});
    }else{
      int a, b; cin >> a >> b;
      Q[b].pb({a, b, i, ++qq});
    }
  }
  pref[1] = 0;
  pre(1);
  trie.resize(n+1);
  dfs(1, 0, 0);

  for(int i = 1; i <= qq; ++i) cout << ans[i] << '\n';
}


int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // cin >> tt;
  while(tt--){
    solve();
    // en;
  }
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
  return 0;
}

Compilation message

klasika.cpp: In function 'int main()':
klasika.cpp:148:15: warning: unused variable 'aa' [-Wunused-variable]
  148 |   int tt = 1, aa;
      |               ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 78680 KB Output is correct
2 Correct 16 ms 78556 KB Output is correct
3 Correct 18 ms 78576 KB Output is correct
4 Correct 17 ms 78684 KB Output is correct
5 Correct 16 ms 78684 KB Output is correct
6 Correct 16 ms 78784 KB Output is correct
7 Correct 17 ms 79060 KB Output is correct
8 Correct 17 ms 79312 KB Output is correct
9 Correct 17 ms 78684 KB Output is correct
10 Correct 19 ms 78812 KB Output is correct
11 Correct 19 ms 78940 KB Output is correct
12 Correct 17 ms 79192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 78680 KB Output is correct
2 Correct 16 ms 78556 KB Output is correct
3 Correct 18 ms 78576 KB Output is correct
4 Correct 17 ms 78684 KB Output is correct
5 Correct 16 ms 78684 KB Output is correct
6 Correct 16 ms 78784 KB Output is correct
7 Correct 17 ms 79060 KB Output is correct
8 Correct 17 ms 79312 KB Output is correct
9 Correct 17 ms 78684 KB Output is correct
10 Correct 19 ms 78812 KB Output is correct
11 Correct 19 ms 78940 KB Output is correct
12 Correct 17 ms 79192 KB Output is correct
13 Correct 19 ms 79196 KB Output is correct
14 Correct 18 ms 79836 KB Output is correct
15 Correct 21 ms 80384 KB Output is correct
16 Correct 20 ms 80732 KB Output is correct
17 Correct 19 ms 80336 KB Output is correct
18 Correct 22 ms 82524 KB Output is correct
19 Correct 24 ms 84824 KB Output is correct
20 Correct 25 ms 87036 KB Output is correct
21 Correct 21 ms 79924 KB Output is correct
22 Correct 21 ms 81496 KB Output is correct
23 Correct 23 ms 82808 KB Output is correct
24 Correct 25 ms 83720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 132628 KB Output is correct
2 Correct 273 ms 171680 KB Output is correct
3 Correct 328 ms 208480 KB Output is correct
4 Correct 326 ms 244156 KB Output is correct
5 Correct 460 ms 346764 KB Output is correct
6 Runtime error 562 ms 524288 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 78680 KB Output is correct
2 Correct 16 ms 78556 KB Output is correct
3 Correct 18 ms 78576 KB Output is correct
4 Correct 17 ms 78684 KB Output is correct
5 Correct 16 ms 78684 KB Output is correct
6 Correct 16 ms 78784 KB Output is correct
7 Correct 17 ms 79060 KB Output is correct
8 Correct 17 ms 79312 KB Output is correct
9 Correct 17 ms 78684 KB Output is correct
10 Correct 19 ms 78812 KB Output is correct
11 Correct 19 ms 78940 KB Output is correct
12 Correct 17 ms 79192 KB Output is correct
13 Correct 19 ms 79196 KB Output is correct
14 Correct 18 ms 79836 KB Output is correct
15 Correct 21 ms 80384 KB Output is correct
16 Correct 20 ms 80732 KB Output is correct
17 Correct 19 ms 80336 KB Output is correct
18 Correct 22 ms 82524 KB Output is correct
19 Correct 24 ms 84824 KB Output is correct
20 Correct 25 ms 87036 KB Output is correct
21 Correct 21 ms 79924 KB Output is correct
22 Correct 21 ms 81496 KB Output is correct
23 Correct 23 ms 82808 KB Output is correct
24 Correct 25 ms 83720 KB Output is correct
25 Correct 205 ms 132628 KB Output is correct
26 Correct 273 ms 171680 KB Output is correct
27 Correct 328 ms 208480 KB Output is correct
28 Correct 326 ms 244156 KB Output is correct
29 Correct 460 ms 346764 KB Output is correct
30 Runtime error 562 ms 524288 KB Execution killed with signal 9
31 Halted 0 ms 0 KB -