Submission #1040566

# Submission time Handle Problem Language Result Execution time Memory
1040566 2024-08-01T07:28:25 Z erray Beech Tree (IOI23_beechtree) C++17
53 / 100
224 ms 83660 KB
#include "beechtree.h"
#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG 
  #include "/home/ioi/contests/ioi23_d2/debug.h"
#else 
  #define debug(...) void(37)
#endif

/*
template<typename T, using F = function<T(const T&, const T&)>>
struct SegTree {
  F op;
  vector<T> tree; T def;
  int n;
  SegTree(int _n, T _def, F _op) : n(_n), op(_op), def(_def) {
    tree.resize(2 * n);
  }
  T get(int l, int r) {
    T res = def;
    for (l += n, r += n + 1; l < r; l >> 1, r >> 1) {
      if (l % 2) res = op(res, tree[l++]);
      if (r % 2) res = op(res, tree[--r]);
    }
    return res;
  } 
  void modify(int p, T x) {
    for (tree[p += n] = x; p > 1; p >>= 1) {
      tree[p >> 1] = op(tree[p], tree[p ^ 1]);
    }
  }
};
*/
struct Fenwick {
  vector<int> sum;
  int n;
  Fenwick() { } 
  Fenwick(int _n) : n(_n) {
    sum.resize(n + 1);
  }
  int get(int x) {
    x += 1;
    int res = 0;
    while (x > 0) {
      res += sum[x];
      x -= x & -x;
    } 
    return res;
  }
  void modify(int x, int v) {
    x += 1;
    while (x <= n) {
      sum[x] += v;
      x += x & -x;
    }
  }
};



std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C) {
  for (auto& x : C) --x;
  vector<vector<int>> g(N);
  for (int i = 1; i < N; ++i) {
    g[P[i]].push_back(i);
  }
  vector<vector<int>> sub(N);
  vector<int> s(N, 1);
  for (int i = N - 1; i > 0; --i) {
    s[P[i]] += s[i];
    sub[P[i]].push_back(C[i]);
  }
  vector<bool> bad(N, false);
  for (int i = 0; i < N; ++i) {
    sort(sub[i].begin(), sub[i].end());
    for (int j = 0; j < int(sub[i].size()) - 1; ++j) {
      if (sub[i][j] == sub[i][j + 1]) bad[i] = true;
    }
  }
  vector<vector<int>> all(M);
  for (int i = 1; i < N; ++i) {
    all[C[i]].push_back(s[i]);
  }
  debug(all);
  for (int i = 0; i < M; ++i) {
    sort(all[i].begin(), all[i].end());
    all[i].erase(unique(all[i].begin(), all[i].end()), all[i].end());
  }
  vector<int> ind(N, -1);
  for (int i = 1; i < N; ++i) {
    ind[i] = int(all[C[i]].size()) - 1 - int(lower_bound(all[C[i]].begin(), all[C[i]].end(), s[i]) - all[C[i]].begin());
  }
  debug(ind);
  vector<Fenwick> fenws(M);
  for (int i = 0; i < M; ++i) {
    fenws[i] = Fenwick(int(all[i].size()));
  }
  vector<int> ct(M);
  set<array<int, 2>> sizes;
  vector<array<int, 2>> fenwick_updates;
  bool still_good = true;
  
  vector<bool> cur_color(M);
  auto Add = [&](int v) {
    int l = 0, r = N;
    for (auto u : g[v]) {
      cur_color[C[u]] = true;
    }
    {
      auto it = sizes.begin();
      while (it != sizes.end()) {
        if (!cur_color[(*it)[1]]) {
          l = (*it)[0];
          break;
        }
        it = next(it);
      }
    }
    for (auto u : g[v]) {
      r = min(r, fenws[C[u]].get(ind[u]));
      l = max(l, fenws[C[u]].get(ind[u] - 1));
    }
    still_good &= l <= r;
    for (auto u : g[v]) {
      cur_color[C[u]] = false;
      sizes.erase({ct[C[u]], C[u]});
      ct[C[u]]++;
      sizes.insert({ct[C[u]], C[u]});
      fenws[C[u]].modify(ind[u], +1);
      fenwick_updates.push_back({C[u], ind[u]});
    }
  };
  auto Reset = [&] {
    still_good = true;
    for (auto[c, x] : fenwick_updates) {
      fenws[c].modify(x, -1);
      ct[c] = 0;
      sizes.clear();
    }
    fenwick_updates.clear();
  };
  auto Dfs = [&](int v, bool calculating, auto&& Dfs) -> void {
    debug(v, calculating);
    for (int i = 0; i < int(g[v].size()); ++i) {
      if (s[g[v][0]] < s[g[v][i]]) swap(g[v][0], g[v][i]);
    }
    if (calculating) {
      for (int i = int(g[v].size()) - 1; i >= 0; --i) {
        Reset();
        Dfs(g[v][i], true, Dfs);
      }
      for (int i = int(g[v].size()) - 1; i > 0; --i) {
        Dfs(g[v][i], false, Dfs);
      }
      Add(v);
      if (!still_good) bad[v] = true;
    } else {
      Add(v);
      for (auto u : g[v]) {
        Dfs(u, false, Dfs);
      }
    }
  };
  Dfs(0, true, Dfs);
  
  for (int i = N - 1; i > 0; --i) {
    if (bad[i]) bad[P[i]] = true;
  }
  vector<int> res(N);
  for (int i = 0; i < N; ++i) res[i] = 1 ^ bool(bad[i]);
  return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 424 KB Output is correct
20 Correct 0 ms 856 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 408 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 224 ms 83660 KB Output is correct
8 Correct 165 ms 79560 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 432 KB Output is correct
12 Correct 0 ms 352 KB Output is correct
13 Correct 12 ms 18864 KB Output is correct
14 Correct 13 ms 18780 KB Output is correct
15 Correct 13 ms 18976 KB Output is correct
16 Correct 13 ms 19036 KB Output is correct
17 Correct 99 ms 73008 KB Output is correct
18 Correct 105 ms 73160 KB Output is correct
19 Correct 175 ms 79440 KB Output is correct
20 Correct 220 ms 81604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Incorrect 0 ms 352 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 224 ms 83660 KB Output is correct
6 Correct 165 ms 79560 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 19 ms 18776 KB Output is correct
10 Correct 11 ms 18520 KB Output is correct
11 Correct 181 ms 53188 KB Output is correct
12 Correct 203 ms 47600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 424 KB Output is correct
23 Correct 0 ms 856 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 408 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 432 KB Output is correct
41 Correct 0 ms 352 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 0 ms 352 KB Output is correct
44 Incorrect 0 ms 352 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
45 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 424 KB Output is correct
16 Correct 0 ms 856 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 408 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 424 KB Output is correct
23 Correct 0 ms 856 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 408 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 432 KB Output is correct
41 Correct 0 ms 352 KB Output is correct
42 Correct 12 ms 18864 KB Output is correct
43 Correct 13 ms 18780 KB Output is correct
44 Correct 13 ms 18976 KB Output is correct
45 Correct 13 ms 19036 KB Output is correct
46 Correct 1 ms 352 KB Output is correct
47 Correct 0 ms 352 KB Output is correct
48 Incorrect 0 ms 352 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
49 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 424 KB Output is correct
16 Correct 0 ms 856 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 408 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 85 ms 54356 KB Output is correct
40 Correct 88 ms 54352 KB Output is correct
41 Correct 87 ms 54468 KB Output is correct
42 Correct 90 ms 54560 KB Output is correct
43 Correct 88 ms 46152 KB Output is correct
44 Correct 97 ms 34492 KB Output is correct
45 Correct 173 ms 28356 KB Output is correct
46 Correct 176 ms 27700 KB Output is correct
47 Correct 120 ms 27636 KB Output is correct
48 Correct 105 ms 27652 KB Output is correct
49 Correct 93 ms 27012 KB Output is correct
50 Correct 176 ms 25360 KB Output is correct
51 Correct 163 ms 25412 KB Output is correct
52 Correct 126 ms 25668 KB Output is correct
53 Correct 122 ms 25504 KB Output is correct
54 Correct 90 ms 24468 KB Output is correct
55 Correct 72 ms 33604 KB Output is correct
56 Correct 71 ms 33736 KB Output is correct
57 Correct 86 ms 26400 KB Output is correct
58 Correct 91 ms 25664 KB Output is correct
59 Correct 90 ms 25664 KB Output is correct
60 Correct 82 ms 25412 KB Output is correct
61 Correct 99 ms 25408 KB Output is correct
62 Correct 77 ms 25432 KB Output is correct
63 Correct 80 ms 31272 KB Output is correct
64 Correct 91 ms 33216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 352 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 424 KB Output is correct
23 Correct 0 ms 856 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 408 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 224 ms 83660 KB Output is correct
39 Correct 165 ms 79560 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 432 KB Output is correct
43 Correct 0 ms 352 KB Output is correct
44 Correct 12 ms 18864 KB Output is correct
45 Correct 13 ms 18780 KB Output is correct
46 Correct 13 ms 18976 KB Output is correct
47 Correct 13 ms 19036 KB Output is correct
48 Correct 99 ms 73008 KB Output is correct
49 Correct 105 ms 73160 KB Output is correct
50 Correct 175 ms 79440 KB Output is correct
51 Correct 220 ms 81604 KB Output is correct
52 Correct 1 ms 352 KB Output is correct
53 Correct 0 ms 352 KB Output is correct
54 Incorrect 0 ms 352 KB 2nd lines differ - on the 1st token, expected: '0', found: '1'
55 Halted 0 ms 0 KB -