답안 #930210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930210 2024-02-19T02:56:59 Z duckindog Klasika (COCI20_klasika) C++17
110 / 110
528 ms 297300 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];

int maxBit = 30;
struct trie {
  int it;
  vector<array<int, 2>> nwt;
  vector<int> t;

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

      while (nwt.size() <= g) nwt.push_back(array<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(array<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;
  }

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

} node[N];

vector<pii> ad[N];
int d[N], t[N];
int answer[N];

int it;
int st[N], ed[N], mask[N];

void dfs(int u, int pre = 0) {
  mask[st[u] = ++it] = u;
  int best = 0;
  for (const auto& duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    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& duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    if (v == pre || v == best) continue;

    for (int i = st[v]; i <= ed[v]; ++i) node[u].add(d[mask[i]], t[mask[i]]);
    node[v].clear();
  }
  node[u].add(d[u], t[u]);
  for (const auto& duck : Q[u]) {
    int a = duck.a, time = duck.t;
    answer[time] = node[u].get(d[a], time);
  }

  ed[u] = it;
}

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:26:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   26 |       int bit = (val >> j - 1 & 1);
      |                         ~~^~~
klasika.cpp:28:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   28 |       while (nwt.size() <= g) nwt.push_back(array<int, 2>());
      |              ~~~~~~~~~~~^~~~
klasika.cpp:33:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   33 |       while (t.size() <= it) t.push_back(N);
      |              ~~~~~~~~~^~~~~
klasika.cpp: In member function 'int trie::get(int, int)':
klasika.cpp:41:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   41 |       while (nwt.size() <= g) nwt.push_back(array<int, 2>());
      |              ~~~~~~~~~~~^~~~
klasika.cpp:42:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |       while (t.size() <= it) t.push_back(N);
      |              ~~~~~~~~~^~~~~
klasika.cpp:43:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   43 |       int bit = (val >> j - 1 & 1);
      |                         ~~^~~
klasika.cpp:46:24: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   46 |         ret |= (1 << j - 1);
      |                      ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21592 KB Output is correct
2 Correct 12 ms 21596 KB Output is correct
3 Correct 13 ms 21564 KB Output is correct
4 Correct 11 ms 21596 KB Output is correct
5 Correct 11 ms 21596 KB Output is correct
6 Correct 11 ms 21596 KB Output is correct
7 Correct 13 ms 21592 KB Output is correct
8 Correct 12 ms 21592 KB Output is correct
9 Correct 10 ms 21628 KB Output is correct
10 Correct 12 ms 21596 KB Output is correct
11 Correct 11 ms 21524 KB Output is correct
12 Correct 11 ms 21768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21592 KB Output is correct
2 Correct 12 ms 21596 KB Output is correct
3 Correct 13 ms 21564 KB Output is correct
4 Correct 11 ms 21596 KB Output is correct
5 Correct 11 ms 21596 KB Output is correct
6 Correct 11 ms 21596 KB Output is correct
7 Correct 13 ms 21592 KB Output is correct
8 Correct 12 ms 21592 KB Output is correct
9 Correct 10 ms 21628 KB Output is correct
10 Correct 12 ms 21596 KB Output is correct
11 Correct 11 ms 21524 KB Output is correct
12 Correct 11 ms 21768 KB Output is correct
13 Correct 6 ms 21860 KB Output is correct
14 Correct 12 ms 22360 KB Output is correct
15 Correct 13 ms 22624 KB Output is correct
16 Correct 13 ms 22664 KB Output is correct
17 Correct 13 ms 22108 KB Output is correct
18 Correct 13 ms 22784 KB Output is correct
19 Correct 13 ms 23388 KB Output is correct
20 Correct 14 ms 23768 KB Output is correct
21 Correct 12 ms 22012 KB Output is correct
22 Correct 12 ms 22360 KB Output is correct
23 Correct 16 ms 22744 KB Output is correct
24 Correct 13 ms 22876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 49352 KB Output is correct
2 Correct 145 ms 66836 KB Output is correct
3 Correct 170 ms 76944 KB Output is correct
4 Correct 181 ms 108116 KB Output is correct
5 Correct 183 ms 89088 KB Output is correct
6 Correct 303 ms 158884 KB Output is correct
7 Correct 392 ms 216972 KB Output is correct
8 Correct 528 ms 294812 KB Output is correct
9 Correct 145 ms 59604 KB Output is correct
10 Correct 189 ms 93692 KB Output is correct
11 Correct 227 ms 121480 KB Output is correct
12 Correct 273 ms 164000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21592 KB Output is correct
2 Correct 12 ms 21596 KB Output is correct
3 Correct 13 ms 21564 KB Output is correct
4 Correct 11 ms 21596 KB Output is correct
5 Correct 11 ms 21596 KB Output is correct
6 Correct 11 ms 21596 KB Output is correct
7 Correct 13 ms 21592 KB Output is correct
8 Correct 12 ms 21592 KB Output is correct
9 Correct 10 ms 21628 KB Output is correct
10 Correct 12 ms 21596 KB Output is correct
11 Correct 11 ms 21524 KB Output is correct
12 Correct 11 ms 21768 KB Output is correct
13 Correct 6 ms 21860 KB Output is correct
14 Correct 12 ms 22360 KB Output is correct
15 Correct 13 ms 22624 KB Output is correct
16 Correct 13 ms 22664 KB Output is correct
17 Correct 13 ms 22108 KB Output is correct
18 Correct 13 ms 22784 KB Output is correct
19 Correct 13 ms 23388 KB Output is correct
20 Correct 14 ms 23768 KB Output is correct
21 Correct 12 ms 22012 KB Output is correct
22 Correct 12 ms 22360 KB Output is correct
23 Correct 16 ms 22744 KB Output is correct
24 Correct 13 ms 22876 KB Output is correct
25 Correct 115 ms 49352 KB Output is correct
26 Correct 145 ms 66836 KB Output is correct
27 Correct 170 ms 76944 KB Output is correct
28 Correct 181 ms 108116 KB Output is correct
29 Correct 183 ms 89088 KB Output is correct
30 Correct 303 ms 158884 KB Output is correct
31 Correct 392 ms 216972 KB Output is correct
32 Correct 528 ms 294812 KB Output is correct
33 Correct 145 ms 59604 KB Output is correct
34 Correct 189 ms 93692 KB Output is correct
35 Correct 227 ms 121480 KB Output is correct
36 Correct 273 ms 164000 KB Output is correct
37 Correct 155 ms 47664 KB Output is correct
38 Correct 167 ms 67976 KB Output is correct
39 Correct 181 ms 78708 KB Output is correct
40 Correct 204 ms 108312 KB Output is correct
41 Correct 196 ms 92584 KB Output is correct
42 Correct 302 ms 164876 KB Output is correct
43 Correct 423 ms 213536 KB Output is correct
44 Correct 517 ms 297300 KB Output is correct
45 Correct 176 ms 60876 KB Output is correct
46 Correct 217 ms 95044 KB Output is correct
47 Correct 272 ms 122360 KB Output is correct
48 Correct 294 ms 164796 KB Output is correct