답안 #930126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930126 2024-02-18T15:36:48 Z duckindog Klasika (COCI20_klasika) C++14
33 / 110
1327 ms 524288 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
const int N = 2e5 + 10;
int n;
struct Query {
  int a, t;
  Query(int a = 0, int t = 0) : a(a), t(t) {}
};
vector<Query> Q[N];

struct trie {
  int maxBit = 31;
  int it;
  vector<vector<int>> nwt;
  vector<int> t;

  set<int> S;
  map<int, int> tick;
  void add(int val, int time) { 
    S.insert(val);
    if (!tick[val] || tick[val] > time) tick[val] = time;

    int g = 0;
    for (int j = maxBit; j >= 1; --j) { 
      int bit = (val >> j - 1 & 1);

      while (nwt.size() <= g) nwt.push_back(vector<int>(2));

      if (!nwt[g][bit]) nwt[g][bit] = ++it;
      g = nwt[g][bit];

      while (t.size() <= it) t.push_back(N);
      t[g] = min(t[g], time);
    }
  }
  int get(int val, int time) { 
    int g = 0;
    int ret = 0;
    for (int j = maxBit; j >= 1; --j) { 
      while (nwt.size() <= g) nwt.push_back(vector<int>(2));
      while (t.size() <= it) t.push_back(N);
      int bit = (val >> j - 1 & 1);
      if (nwt[g][!bit] && t[nwt[g][!bit]] <= time) { 
        ret |= (1 << j - 1);
        g = nwt[g][!bit];
      } else g = nwt[g][bit];

    }
    return ret;
  }

  vector<pii> allValues() { 
    vector<pii> ret;
    for (auto &i : S) ret.push_back({i, tick[i]});
    return ret;
  }

  void clear() { 
    nwt.clear();
    t.clear();
    tick.clear();
    S.clear();
  }

} node[N];


vector<pii> ad[N];
int d[N], t[N];
int answer[N];
void dfs(int u, int pre = 0) { 
  int best = 0;
  for (const auto& [v, w] : ad[u]) { 
    if (v == pre) continue;
    dfs(v, u);
    if (!best || node[v].nwt.size() > node[best].nwt.size()) best = v;
  }
  swap(node[u], node[best]);
  for (const auto& [v, w] : ad[u]) { 
    if (v == pre || v == best) continue;
    for (auto &i : node[v].allValues()) node[u].add(i.first, i.second);
    node[v].clear();
  }
  node[u].add(d[u], t[u]);
  for (const auto& [a, time] : Q[u]) answer[time] = node[u].get(d[a], time);
}

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
  
  cin >> n;
  int cnt = 1;
  for (int i = 1; i <= n; ++i) { 
    string ty;
    int x, y; 
    cin >> ty >> x >> y;
    if (ty == "Add") { 
      cnt += 1;
      ad[x].push_back({cnt, y});
      ad[cnt].push_back({x, y});
      d[cnt] = (d[x] ^ y);
      t[cnt] = i;
    } else { 
      Q[y].push_back(Query(x, i));
    }
  }

  memset(answer, -1, sizeof answer);
  dfs(1);
  for (int i = 1; i <= n; ++i) 
    if (answer[i] != -1) cout << answer[i] << "\n";
}

Compilation message

klasika.cpp: In member function 'void trie::add(int, int)':
klasika.cpp:30:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   30 |       int bit = (val >> j - 1 & 1);
      |                         ~~^~~
klasika.cpp:32:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   32 |       while (nwt.size() <= g) nwt.push_back(vector<int>(2));
      |              ~~~~~~~~~~~^~~~
klasika.cpp:37:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |       while (t.size() <= it) t.push_back(N);
      |              ~~~~~~~~~^~~~~
klasika.cpp: In member function 'int trie::get(int, int)':
klasika.cpp:45:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   45 |       while (nwt.size() <= g) nwt.push_back(vector<int>(2));
      |              ~~~~~~~~~~~^~~~
klasika.cpp:46:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |       while (t.size() <= it) t.push_back(N);
      |              ~~~~~~~~~^~~~~
klasika.cpp:47:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   47 |       int bit = (val >> j - 1 & 1);
      |                         ~~^~~
klasika.cpp:49:24: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   49 |         ret |= (1 << j - 1);
      |                      ~~^~~
klasika.cpp: In function 'void dfs(int, int)':
klasika.cpp:78:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |   for (const auto& [v, w] : ad[u]) {
      |                    ^
klasika.cpp:84:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   84 |   for (const auto& [v, w] : ad[u]) {
      |                    ^
klasika.cpp:90:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |   for (const auto& [a, time] : Q[u]) answer[time] = node[u].get(d[a], time);
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 40540 KB Output is correct
2 Correct 11 ms 40540 KB Output is correct
3 Correct 11 ms 40796 KB Output is correct
4 Correct 10 ms 40680 KB Output is correct
5 Correct 10 ms 40540 KB Output is correct
6 Correct 12 ms 40796 KB Output is correct
7 Correct 11 ms 40796 KB Output is correct
8 Correct 11 ms 41052 KB Output is correct
9 Correct 10 ms 40656 KB Output is correct
10 Correct 11 ms 40796 KB Output is correct
11 Correct 11 ms 40928 KB Output is correct
12 Correct 10 ms 40972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 40540 KB Output is correct
2 Correct 11 ms 40540 KB Output is correct
3 Correct 11 ms 40796 KB Output is correct
4 Correct 10 ms 40680 KB Output is correct
5 Correct 10 ms 40540 KB Output is correct
6 Correct 12 ms 40796 KB Output is correct
7 Correct 11 ms 40796 KB Output is correct
8 Correct 11 ms 41052 KB Output is correct
9 Correct 10 ms 40656 KB Output is correct
10 Correct 11 ms 40796 KB Output is correct
11 Correct 11 ms 40928 KB Output is correct
12 Correct 10 ms 40972 KB Output is correct
13 Correct 12 ms 41368 KB Output is correct
14 Correct 13 ms 42392 KB Output is correct
15 Correct 14 ms 42516 KB Output is correct
16 Correct 17 ms 44052 KB Output is correct
17 Correct 14 ms 42076 KB Output is correct
18 Correct 16 ms 43672 KB Output is correct
19 Correct 19 ms 44952 KB Output is correct
20 Correct 26 ms 46364 KB Output is correct
21 Correct 13 ms 41820 KB Output is correct
22 Correct 15 ms 42908 KB Output is correct
23 Correct 17 ms 43672 KB Output is correct
24 Correct 18 ms 44468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 110116 KB Output is correct
2 Correct 481 ms 179296 KB Output is correct
3 Correct 583 ms 210984 KB Output is correct
4 Correct 779 ms 309852 KB Output is correct
5 Correct 641 ms 212952 KB Output is correct
6 Correct 1195 ms 412376 KB Output is correct
7 Runtime error 1327 ms 524288 KB Execution killed with signal 9
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 40540 KB Output is correct
2 Correct 11 ms 40540 KB Output is correct
3 Correct 11 ms 40796 KB Output is correct
4 Correct 10 ms 40680 KB Output is correct
5 Correct 10 ms 40540 KB Output is correct
6 Correct 12 ms 40796 KB Output is correct
7 Correct 11 ms 40796 KB Output is correct
8 Correct 11 ms 41052 KB Output is correct
9 Correct 10 ms 40656 KB Output is correct
10 Correct 11 ms 40796 KB Output is correct
11 Correct 11 ms 40928 KB Output is correct
12 Correct 10 ms 40972 KB Output is correct
13 Correct 12 ms 41368 KB Output is correct
14 Correct 13 ms 42392 KB Output is correct
15 Correct 14 ms 42516 KB Output is correct
16 Correct 17 ms 44052 KB Output is correct
17 Correct 14 ms 42076 KB Output is correct
18 Correct 16 ms 43672 KB Output is correct
19 Correct 19 ms 44952 KB Output is correct
20 Correct 26 ms 46364 KB Output is correct
21 Correct 13 ms 41820 KB Output is correct
22 Correct 15 ms 42908 KB Output is correct
23 Correct 17 ms 43672 KB Output is correct
24 Correct 18 ms 44468 KB Output is correct
25 Correct 285 ms 110116 KB Output is correct
26 Correct 481 ms 179296 KB Output is correct
27 Correct 583 ms 210984 KB Output is correct
28 Correct 779 ms 309852 KB Output is correct
29 Correct 641 ms 212952 KB Output is correct
30 Correct 1195 ms 412376 KB Output is correct
31 Runtime error 1327 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -