Submission #210124

# Submission time Handle Problem Language Result Execution time Memory
210124 2020-03-16T15:09:56 Z waynetuinfor Cats or Dogs (JOI18_catdog) C++17
100 / 100
1040 ms 51064 KB
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#include "catdog.h"

namespace {

constexpr int kInf = 1'000'000;

std::vector<std::vector<int>> g, nd;
std::vector<int> sz, to, fa, fr, dep, ksz;

void Dfs(int x, int p) {
  sz[x] = 1;
  fa[x] = p;
  for (int u : g[x]) {
    if (u == p) continue;
    dep[u] = dep[x] + 1;
    Dfs(u, x);
    sz[x] += sz[u];
    if (to[x] == -1 || sz[u] > sz[to[x]]) to[x] = u;
  }
}

void Link(int x, int f) {
  fr[x] = f;
  if (to[x] != -1) Link(to[x], f);
  for (int u : g[x]) {
    if (u == fa[x] || u == to[x]) continue;
    Link(u, u);
  }
}

struct IntervalTree {
  int n;
  std::vector<std::vector<int>> tree;
  std::vector<std::vector<int>> aux;

  IntervalTree() = default;
  IntervalTree(int n)
      : n(n), tree(n * 4, std::vector<int>(4)), aux(n, std::vector<int>(2)) {
    for (auto &v : tree) {
      v[1] = kInf;
      v[2] = kInf;
    }
  }

  std::vector<int> Merge(const std::vector<int> &a, const std::vector<int> &b,
                         int p) {
    std::vector<int> res(4, kInf);
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        res[(i & 2) | (j & 1)] = std::min(
            res[(i & 2) | (j & 1)],
            a[i] + b[j] + ((i & 1) != (j >> 1 & 1)) + aux[p][j >> 1 & 1]);
      }
    }
    return res;
  }

  void UpdateAux(int p, const std::vector<int> &v) {
    auto Impl = [&](auto self, int l, int r, int o = 0) {
      if (r - l == 1) {
        aux[l] = v;
        return;
      }
      int m = (l + r) >> 1;
      if (p < m) {
        self(self, l, m, o * 2 + 1);
      } else {
        self(self, m, r, o * 2 + 2);
      }
      tree[o] = Merge(tree[o * 2 + 1], tree[o * 2 + 2], m);
    };
    Impl(Impl, 0, n);
  }

  void Modify(int p, const std::vector<int> &v) {
    auto Impl = [&](auto self, int l, int r, int o = 0) {
      if (r - l == 1) {
        std::fill(tree[o].begin(), tree[o].end(), kInf);
        for (int i = 0; i < 2; ++i) tree[o][i << 1 | i] = v[i];
        return;
      }
      int m = (l + r) >> 1;
      if (p < m) {
        self(self, l, m, o * 2 + 1);
      } else {
        self(self, m, r, o * 2 + 2);
      }
      tree[o] = Merge(tree[o * 2 + 1], tree[o * 2 + 2], m);
    };
    Impl(Impl, 0, n);
  }

  std::vector<int> Query(int ql, int qr) {
    auto Impl = [&](auto self, int l, int r, int o = 0) {
      if (l >= ql && r <= qr) return tree[o];
      int m = (l + r) >> 1;
      if (qr <= m) return self(self, l, m, o * 2 + 1);
      if (ql >= m) return self(self, m, r, o * 2 + 2);
      return Merge(self(self, l, m, o * 2 + 1), self(self, m, r, o * 2 + 2), m);
    };
    return Impl(Impl, 0, n);
  }
};

std::vector<IntervalTree> tree;

}  // namespace

void initialize(int n, std::vector<int> a, std::vector<int> b) {
  g.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u = a[i], v = b[i];
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  sz.assign(n, 0);
  ksz.assign(n, 0);
  to.assign(n, -1);
  fa.assign(n, -1);
  fr.assign(n, -1);
  dep.assign(n, 0);
  tree.assign(n, IntervalTree());

  Dfs(0, -1);
  Link(0, 0);

  for (int i = 0; i < n; ++i) ksz[fr[i]]++;
  for (int i = 0; i < n; ++i) {
    if (fr[i] == i) {
      tree[i] = IntervalTree(ksz[i]);
    }
  }
}

namespace {

std::vector<int> GetAux(int v) {
  int p = dep[v] - dep[fr[v]];
  return tree[fr[v]].aux[p];
}

std::vector<int> GetValue(int v) {
  int p = dep[v] - dep[fr[v]];
  auto val = tree[fr[v]].Query(p, ksz[fr[v]]);
  std::vector<int> res(2, kInf);
  for (int i = 0; i < 4; ++i) {
    res[i >> 1 & 1] = std::min(res[i >> 1 & 1], val[i]);
  }
  for (int i = 0; i < 2; ++i) res[i] += GetAux(v)[i];
  return res;
}

void UpdateAux(int v, const std::vector<int> &aux) {
  int p = dep[v] - dep[fr[v]];
  tree[fr[v]].UpdateAux(p, aux);
}

int Exec(int v, std::vector<int> upd) {
  std::vector<std::vector<int>> hist;
  for (int w = fr[v]; fa[w] != -1; w = fr[fa[w]]) {
    hist.push_back(GetValue(w));
  }
  tree[fr[v]].Modify(dep[v] - dep[fr[v]], upd);
  v = fr[v];
  int ptr = 0;
  while (fa[v] >= 0) {
    auto aux = GetAux(fa[v]);
    auto old = hist[ptr];
    aux[0] -= std::min(old[0], old[1] + 1);
    aux[1] -= std::min(old[1], old[0] + 1);
    auto upd = GetValue(v);
    aux[0] += std::min(upd[0], upd[1] + 1);
    aux[1] += std::min(upd[1], upd[0] + 1);
    ptr++;
    UpdateAux(fa[v], aux);
    v = fr[fa[v]];
  }
  auto res = GetValue(0);
  return std::min(res[0], res[1]);
}

}  // namespace

int cat(int v) { return Exec(v - 1, {0, kInf}); }

int dog(int v) { return Exec(v - 1, {kInf, 0}); }

int neighbor(int v) { return Exec(v - 1, {0, 0}); }
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 5 ms 256 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 256 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 6 ms 296 KB Output is correct
13 Correct 5 ms 380 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 5 ms 256 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 256 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 6 ms 296 KB Output is correct
13 Correct 5 ms 380 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 6 ms 376 KB Output is correct
17 Correct 8 ms 632 KB Output is correct
18 Correct 8 ms 760 KB Output is correct
19 Correct 7 ms 632 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 6 ms 376 KB Output is correct
22 Correct 6 ms 504 KB Output is correct
23 Correct 8 ms 760 KB Output is correct
24 Correct 8 ms 632 KB Output is correct
25 Correct 7 ms 504 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 6 ms 376 KB Output is correct
28 Correct 6 ms 632 KB Output is correct
29 Correct 7 ms 760 KB Output is correct
30 Correct 6 ms 376 KB Output is correct
31 Correct 5 ms 632 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 5 ms 256 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 256 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 6 ms 296 KB Output is correct
13 Correct 5 ms 380 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 6 ms 376 KB Output is correct
17 Correct 8 ms 632 KB Output is correct
18 Correct 8 ms 760 KB Output is correct
19 Correct 7 ms 632 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 6 ms 376 KB Output is correct
22 Correct 6 ms 504 KB Output is correct
23 Correct 8 ms 760 KB Output is correct
24 Correct 8 ms 632 KB Output is correct
25 Correct 7 ms 504 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 6 ms 376 KB Output is correct
28 Correct 6 ms 632 KB Output is correct
29 Correct 7 ms 760 KB Output is correct
30 Correct 6 ms 376 KB Output is correct
31 Correct 5 ms 632 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
33 Correct 552 ms 23144 KB Output is correct
34 Correct 184 ms 28536 KB Output is correct
35 Correct 492 ms 15856 KB Output is correct
36 Correct 843 ms 39952 KB Output is correct
37 Correct 43 ms 13432 KB Output is correct
38 Correct 979 ms 44664 KB Output is correct
39 Correct 1014 ms 44752 KB Output is correct
40 Correct 1008 ms 44752 KB Output is correct
41 Correct 1040 ms 44672 KB Output is correct
42 Correct 998 ms 44648 KB Output is correct
43 Correct 1000 ms 44672 KB Output is correct
44 Correct 945 ms 44644 KB Output is correct
45 Correct 1018 ms 44648 KB Output is correct
46 Correct 986 ms 44648 KB Output is correct
47 Correct 1030 ms 44676 KB Output is correct
48 Correct 198 ms 33004 KB Output is correct
49 Correct 230 ms 42072 KB Output is correct
50 Correct 77 ms 8440 KB Output is correct
51 Correct 93 ms 16476 KB Output is correct
52 Correct 43 ms 8696 KB Output is correct
53 Correct 515 ms 43640 KB Output is correct
54 Correct 331 ms 18644 KB Output is correct
55 Correct 909 ms 30404 KB Output is correct
56 Correct 537 ms 20120 KB Output is correct
57 Correct 647 ms 39680 KB Output is correct
58 Correct 58 ms 19164 KB Output is correct
59 Correct 94 ms 14824 KB Output is correct
60 Correct 200 ms 37080 KB Output is correct
61 Correct 212 ms 38512 KB Output is correct
62 Correct 164 ms 31576 KB Output is correct
63 Correct 108 ms 26104 KB Output is correct
64 Correct 116 ms 29176 KB Output is correct
65 Correct 151 ms 47712 KB Output is correct
66 Correct 156 ms 10720 KB Output is correct
67 Correct 162 ms 33016 KB Output is correct
68 Correct 335 ms 47992 KB Output is correct
69 Correct 78 ms 3448 KB Output is correct
70 Correct 19 ms 760 KB Output is correct
71 Correct 144 ms 20192 KB Output is correct
72 Correct 203 ms 37324 KB Output is correct
73 Correct 494 ms 51040 KB Output is correct
74 Correct 529 ms 47564 KB Output is correct
75 Correct 322 ms 51064 KB Output is correct
76 Correct 345 ms 49784 KB Output is correct
77 Correct 510 ms 47868 KB Output is correct