Submission #930198

# Submission time Handle Problem Language Result Execution time Memory
930198 2024-02-19T00:48:00 Z duckindog Klasika (COCI20_klasika) C++17
33 / 110
5000 ms 39652 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 it;
  vector<array<int, 2>> nwt;
  vector<int> t;

  void add(int val, int time) {
    int g = 0;
    for (int j = 30; 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.shrink_to_fit();
      t[g] = min(t[g], time);
    }
  }
  int get(int val, int time) {
    int g = 0;
    int ret = 0;
    for (int j = 30; j >= 1; --j) {
      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& [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]);
  node[u].add(d[u], t[u]);
  for (const auto& [v, w] : ad[u]) {
    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();
  }

  for (const auto& [a, time] : Q[u]) {
    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:25:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   25 |       int bit = (val >> j - 1 & 1);
      |                         ~~^~~
klasika.cpp:27: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]
   27 |       while (nwt.size() <= g) nwt.push_back(array<int, 2>());
      |              ~~~~~~~~~~~^~~~
klasika.cpp:32:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   32 |       while (t.size() <= it) t.push_back(N);
      |              ~~~~~~~~~^~~~~
klasika.cpp: In member function 'int trie::get(int, int)':
klasika.cpp:41:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   41 |       int bit = (val >> j - 1 & 1);
      |                         ~~^~~
klasika.cpp:44:24: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   44 |         ret |= (1 << j - 1);
      |                      ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21616 KB Output is correct
2 Correct 11 ms 21608 KB Output is correct
3 Correct 12 ms 21780 KB Output is correct
4 Correct 12 ms 21780 KB Output is correct
5 Correct 11 ms 21608 KB Output is correct
6 Correct 14 ms 21624 KB Output is correct
7 Correct 12 ms 21608 KB Output is correct
8 Correct 13 ms 21888 KB Output is correct
9 Correct 11 ms 21608 KB Output is correct
10 Correct 12 ms 21604 KB Output is correct
11 Correct 13 ms 21608 KB Output is correct
12 Correct 13 ms 21860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21616 KB Output is correct
2 Correct 11 ms 21608 KB Output is correct
3 Correct 12 ms 21780 KB Output is correct
4 Correct 12 ms 21780 KB Output is correct
5 Correct 11 ms 21608 KB Output is correct
6 Correct 14 ms 21624 KB Output is correct
7 Correct 12 ms 21608 KB Output is correct
8 Correct 13 ms 21888 KB Output is correct
9 Correct 11 ms 21608 KB Output is correct
10 Correct 12 ms 21604 KB Output is correct
11 Correct 13 ms 21608 KB Output is correct
12 Correct 13 ms 21860 KB Output is correct
13 Correct 18 ms 21864 KB Output is correct
14 Correct 43 ms 22312 KB Output is correct
15 Correct 151 ms 22676 KB Output is correct
16 Correct 302 ms 22844 KB Output is correct
17 Correct 21 ms 22316 KB Output is correct
18 Correct 64 ms 22840 KB Output is correct
19 Correct 96 ms 23408 KB Output is correct
20 Correct 136 ms 23840 KB Output is correct
21 Correct 20 ms 22108 KB Output is correct
22 Correct 65 ms 22628 KB Output is correct
23 Correct 167 ms 23016 KB Output is correct
24 Correct 319 ms 23528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5020 ms 39652 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21616 KB Output is correct
2 Correct 11 ms 21608 KB Output is correct
3 Correct 12 ms 21780 KB Output is correct
4 Correct 12 ms 21780 KB Output is correct
5 Correct 11 ms 21608 KB Output is correct
6 Correct 14 ms 21624 KB Output is correct
7 Correct 12 ms 21608 KB Output is correct
8 Correct 13 ms 21888 KB Output is correct
9 Correct 11 ms 21608 KB Output is correct
10 Correct 12 ms 21604 KB Output is correct
11 Correct 13 ms 21608 KB Output is correct
12 Correct 13 ms 21860 KB Output is correct
13 Correct 18 ms 21864 KB Output is correct
14 Correct 43 ms 22312 KB Output is correct
15 Correct 151 ms 22676 KB Output is correct
16 Correct 302 ms 22844 KB Output is correct
17 Correct 21 ms 22316 KB Output is correct
18 Correct 64 ms 22840 KB Output is correct
19 Correct 96 ms 23408 KB Output is correct
20 Correct 136 ms 23840 KB Output is correct
21 Correct 20 ms 22108 KB Output is correct
22 Correct 65 ms 22628 KB Output is correct
23 Correct 167 ms 23016 KB Output is correct
24 Correct 319 ms 23528 KB Output is correct
25 Execution timed out 5020 ms 39652 KB Time limit exceeded
26 Halted 0 ms 0 KB -