Submission #409835

# Submission time Handle Problem Language Result Execution time Memory
409835 2021-05-21T15:57:00 Z Cyanmond Swapping Cities (APIO20_swap) C++17
100 / 100
468 ms 27964 KB
#include "bits/stdc++.h"
#ifndef CYN_LOCAL_INCLUDED
#include "swap.h"
#endif
#pragma region header
using llint = long long int;  // 64 bit
using usize = size_t;
using isie = ptrdiff_t;
template <class T>
using Vec = std::vector<T>;
template <class T, size_t N>
using Arr = std::array<T, N>;

template <class T, T Div = 2>
constexpr T infty = std::numeric_limits<T>::max() / Div;
constexpr int infi32 = infty<int, 2>;      // 1073741823
constexpr llint infi64 = infty<llint, 4>;  // 2305843009213693951
// infi32 / 2 < 10^9
// infi64 / 2 < 2 * 10^18
constexpr char endl = '\n';  // std::endl without flush ('\n')

inline int popcount(unsigned long long x) noexcept {
#if __cplusplus >= 202002L
  return std::popcount(x);  // C++20
#else
  x = x - ((x >> 1) & 0x5555555555555555);
  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
  x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
  x = x + (x >> 8);
  x = x + (x >> 16);
  x = x + (x >> 32);
  return x & 0x0000007f;
#endif
}

template <class T>
constexpr bool setmin(T& a, const T b) noexcept {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
constexpr bool setmax(T& a, const T b) noexcept {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
constexpr T difrc(const T a, const T b) noexcept {
  return a > b ? a - b : b - a;
}

class range {
  using value_type = int;
  struct range_iterator {
    value_type itr;
    constexpr range_iterator(const value_type pos) noexcept : itr(pos) {}
    constexpr void operator++() noexcept { ++itr; }
    constexpr bool operator!=(const range_iterator& other) const noexcept {
      return itr != other.itr;
    }
    constexpr value_type operator*() const noexcept { return itr; }
  };
  const range_iterator first, last;

 public:
  constexpr range(const value_type first_, const value_type last_) noexcept
      : first(first_), last(last_) {}  // [l, r)
  constexpr range_iterator begin() const noexcept { return first; }
  constexpr range_iterator end() const noexcept { return last; }
};

template <class T>
class span {
  using iterator_type = typename std::vector<T>::iterator;
  iterator_type first, last;

 public:
  span(std::vector<T>& v, const usize l, const usize r) noexcept
      : first(v.begin() + l), last(v.begin() + r) {}  // [l, r)
  auto begin() const noexcept { return first; }
  auto end() const noexcept { return last; }
};

template <class F>
class rec_lambda {
  F f;

 public:
  explicit constexpr rec_lambda(F&& f_) : f(std::forward<F>(f_)) {}
  template <class... Args>
  constexpr auto operator()(Args&&... args) const {
    return f(*this, std::forward<Args>(args)...);
  }
};
#pragma endregion

class partially_persistent_UnionFind {
  std::vector<int> par, last;
  std::vector<int> edge;
  std::vector<int> deg;
  std::vector<std::vector<std::tuple<int, int, int, int>>> history;

 public:
  partially_persistent_UnionFind() = default;
  partially_persistent_UnionFind(int n)
      : par(n, -1), last(n, -1), history(n), edge(n, 0), deg(n, 0) {
    for (auto& vec : history) vec.emplace_back(-1, -1, 0, 0);
  }
  int root(int t, int x) {
    if (last[x] == -1 || t < last[x]) return x;
    return root(t, par[x]);
  }
  bool same(int t, int x, int y) { return root(t, x) == root(t, y); }

  bool merge(int t, int x, int y) {
    ++deg[x];
    ++deg[y];
    int memo1 = x, memo2 = y;
    x = root(t, x);
    y = root(t, y);

    ++edge[x];
    if (x == y) {
      history[x].emplace_back(
          t, par[x], edge[x],
          std::max({std::get<3>(history[x].back()), deg[memo1], deg[memo2]}));
      return false;
    }
    if (par[x] > par[y]) std::swap(x, y);
    par[x] += par[y];
    par[y] = x;
    last[y] = t;
    edge[x] += edge[y];
    history[x].emplace_back(
        t, par[x], edge[x],
        std::max({std::get<3>(history[x].back()),
                  std::get<3>(history[y].back()), deg[memo1], deg[memo2]}));
    return true;
  }
  bool is_ok(int t, int x, int y) {
    x = root(t, x);
    y = root(t, y);
    if (x != y) return false;
    auto Tuple = *prev(lower_bound(history[x].begin(), history[x].end(),
                                   std::make_tuple(t, 0, 0, 0)));

    if (-std::get<1>(Tuple) == std::get<2>(Tuple) or std::get<3>(Tuple) >= 3)
      return true;
    return false;
  }
};

namespace cs_helper {
void zip_sort_renumber([[maybe_unused]] const std::vector<usize>& order) {}
template <class T, class... Args>
void zip_sort_renumber(const std::vector<usize>& order, std::vector<T>& head,
                       Args&... args) {
  usize n = order.size();
  assert(n == head.size());
  std::vector<T> sorted_head(n);
  for (usize i = 0; i < n; ++i) sorted_head[i] = head[order[i]];
  head = std::move(sorted_head);
  zip_sort_renumber(order, args...);
}
}  // namespace cs_helper

template <class T, class... Args>
std::vector<usize> zip_sort(std::vector<T>& head, Args&... args) {
  usize n = head.size();
  std::vector<std::pair<T, usize>> tmp(n);
  for (usize i = 0; i < n; ++i) tmp[i] = std::make_pair(head[i], i);
  std::sort(tmp.begin(), tmp.end());
  std::vector<usize> order(n);
  for (usize i = 0; i < n; ++i) order[i] = tmp[i].second;
  cs_helper::zip_sort_renumber(order, head, args...);
  return order;
}

partially_persistent_UnionFind uf;
int N, M;
std::vector<int> U, V, W;

void init(int n_, int m_, std::vector<int> u_, std::vector<int> v_,
          std::vector<int> w_) {
  std::swap(N, n_);
  std::swap(M, m_);
  std::swap(U, u_);
  std::swap(V, v_);
  std::swap(W, w_);
  zip_sort(W, U, V);
  uf = partially_persistent_UnionFind(N);
  for (usize i : range(0, M)) {
    uf.merge(i, U[i], V[i]);
  }
}

int getMinimumFuelCapacity(int X, int Y) {
  int ok = M, ng = -1;
  while (ok - ng > 1) {
    const int mid = (ok + ng) >> 1;
    if (uf.is_ok(mid, X, Y))
      ok = mid;
    else
      ng = mid;
  }
  return ok == M ? -1 : W[ok];
  //
}

#ifdef CYN_LOCAL_INCLUDED
int main() {
  int n = 5, m = 6;
  std::vector<int> u = {0, 0, 1, 1, 1, 2};
  std::vector<int> v = {1, 2, 2, 3, 4, 3};
  std::vector<int> w = {4, 4, 1, 2, 10, 3};
  init(n, m, u, v, w);
  std::cout << getMinimumFuelCapacity(1, 2) << '\n';
}
#endif

Compilation message

swap.cpp:5: warning: ignoring '#pragma region header' [-Wunknown-pragmas]
    5 | #pragma region header
      | 
swap.cpp:100: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
  100 | #pragma endregion
      | 
swap.cpp: In constructor 'partially_persistent_UnionFind::partially_persistent_UnionFind(int)':
swap.cpp:106:60: warning: 'partially_persistent_UnionFind::history' will be initialized after [-Wreorder]
  106 |   std::vector<std::vector<std::tuple<int, int, int, int>>> history;
      |                                                            ^~~~~~~
swap.cpp:104:20: warning:   'std::vector<int> partially_persistent_UnionFind::edge' [-Wreorder]
  104 |   std::vector<int> edge;
      |                    ^~~~
swap.cpp:110:3: warning:   when initialized here [-Wreorder]
  110 |   partially_persistent_UnionFind(int n)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 63 ms 11764 KB Output is correct
10 Correct 77 ms 14272 KB Output is correct
11 Correct 79 ms 14088 KB Output is correct
12 Correct 95 ms 14804 KB Output is correct
13 Correct 73 ms 14896 KB Output is correct
14 Correct 79 ms 12180 KB Output is correct
15 Correct 257 ms 18896 KB Output is correct
16 Correct 246 ms 18512 KB Output is correct
17 Correct 269 ms 19264 KB Output is correct
18 Correct 249 ms 18620 KB Output is correct
19 Correct 137 ms 7604 KB Output is correct
20 Correct 257 ms 20172 KB Output is correct
21 Correct 259 ms 19868 KB Output is correct
22 Correct 287 ms 20732 KB Output is correct
23 Correct 253 ms 20060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 298 ms 18560 KB Output is correct
4 Correct 296 ms 20136 KB Output is correct
5 Correct 302 ms 19968 KB Output is correct
6 Correct 284 ms 20068 KB Output is correct
7 Correct 297 ms 20108 KB Output is correct
8 Correct 298 ms 19616 KB Output is correct
9 Correct 308 ms 19984 KB Output is correct
10 Correct 303 ms 19664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 428 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 63 ms 11764 KB Output is correct
11 Correct 77 ms 14272 KB Output is correct
12 Correct 79 ms 14088 KB Output is correct
13 Correct 95 ms 14804 KB Output is correct
14 Correct 73 ms 14896 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 428 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 9 ms 2260 KB Output is correct
35 Correct 83 ms 14576 KB Output is correct
36 Correct 80 ms 14568 KB Output is correct
37 Correct 80 ms 14648 KB Output is correct
38 Correct 75 ms 14384 KB Output is correct
39 Correct 75 ms 14232 KB Output is correct
40 Correct 68 ms 12904 KB Output is correct
41 Correct 77 ms 14948 KB Output is correct
42 Correct 77 ms 14692 KB Output is correct
43 Correct 61 ms 14564 KB Output is correct
44 Correct 77 ms 14588 KB Output is correct
45 Correct 92 ms 17184 KB Output is correct
46 Correct 74 ms 14568 KB Output is correct
47 Correct 78 ms 14588 KB Output is correct
48 Correct 77 ms 15204 KB Output is correct
49 Correct 77 ms 13320 KB Output is correct
50 Correct 60 ms 9904 KB Output is correct
51 Correct 79 ms 14928 KB Output is correct
52 Correct 112 ms 19656 KB Output is correct
53 Correct 122 ms 20640 KB Output is correct
54 Correct 149 ms 22676 KB Output is correct
55 Correct 64 ms 14440 KB Output is correct
56 Correct 113 ms 20432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 63 ms 11764 KB Output is correct
10 Correct 77 ms 14272 KB Output is correct
11 Correct 79 ms 14088 KB Output is correct
12 Correct 95 ms 14804 KB Output is correct
13 Correct 73 ms 14896 KB Output is correct
14 Correct 79 ms 12180 KB Output is correct
15 Correct 257 ms 18896 KB Output is correct
16 Correct 246 ms 18512 KB Output is correct
17 Correct 269 ms 19264 KB Output is correct
18 Correct 249 ms 18620 KB Output is correct
19 Correct 298 ms 18560 KB Output is correct
20 Correct 296 ms 20136 KB Output is correct
21 Correct 302 ms 19968 KB Output is correct
22 Correct 284 ms 20068 KB Output is correct
23 Correct 297 ms 20108 KB Output is correct
24 Correct 298 ms 19616 KB Output is correct
25 Correct 308 ms 19984 KB Output is correct
26 Correct 303 ms 19664 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 9 ms 2260 KB Output is correct
37 Correct 83 ms 14576 KB Output is correct
38 Correct 80 ms 14568 KB Output is correct
39 Correct 80 ms 14648 KB Output is correct
40 Correct 75 ms 14384 KB Output is correct
41 Correct 75 ms 14232 KB Output is correct
42 Correct 68 ms 12904 KB Output is correct
43 Correct 77 ms 14948 KB Output is correct
44 Correct 77 ms 14692 KB Output is correct
45 Correct 61 ms 14564 KB Output is correct
46 Correct 77 ms 14588 KB Output is correct
47 Correct 26 ms 2596 KB Output is correct
48 Correct 273 ms 20124 KB Output is correct
49 Correct 255 ms 20208 KB Output is correct
50 Correct 270 ms 20716 KB Output is correct
51 Correct 269 ms 20632 KB Output is correct
52 Correct 267 ms 19660 KB Output is correct
53 Correct 258 ms 16424 KB Output is correct
54 Correct 262 ms 21560 KB Output is correct
55 Correct 262 ms 20700 KB Output is correct
56 Correct 248 ms 19696 KB Output is correct
57 Correct 335 ms 21288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 63 ms 11764 KB Output is correct
11 Correct 77 ms 14272 KB Output is correct
12 Correct 79 ms 14088 KB Output is correct
13 Correct 95 ms 14804 KB Output is correct
14 Correct 73 ms 14896 KB Output is correct
15 Correct 79 ms 12180 KB Output is correct
16 Correct 257 ms 18896 KB Output is correct
17 Correct 246 ms 18512 KB Output is correct
18 Correct 269 ms 19264 KB Output is correct
19 Correct 249 ms 18620 KB Output is correct
20 Correct 298 ms 18560 KB Output is correct
21 Correct 296 ms 20136 KB Output is correct
22 Correct 302 ms 19968 KB Output is correct
23 Correct 284 ms 20068 KB Output is correct
24 Correct 297 ms 20108 KB Output is correct
25 Correct 298 ms 19616 KB Output is correct
26 Correct 308 ms 19984 KB Output is correct
27 Correct 303 ms 19664 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 9 ms 2260 KB Output is correct
38 Correct 83 ms 14576 KB Output is correct
39 Correct 80 ms 14568 KB Output is correct
40 Correct 80 ms 14648 KB Output is correct
41 Correct 75 ms 14384 KB Output is correct
42 Correct 75 ms 14232 KB Output is correct
43 Correct 68 ms 12904 KB Output is correct
44 Correct 77 ms 14948 KB Output is correct
45 Correct 77 ms 14692 KB Output is correct
46 Correct 61 ms 14564 KB Output is correct
47 Correct 77 ms 14588 KB Output is correct
48 Correct 26 ms 2596 KB Output is correct
49 Correct 273 ms 20124 KB Output is correct
50 Correct 255 ms 20208 KB Output is correct
51 Correct 270 ms 20716 KB Output is correct
52 Correct 269 ms 20632 KB Output is correct
53 Correct 267 ms 19660 KB Output is correct
54 Correct 258 ms 16424 KB Output is correct
55 Correct 262 ms 21560 KB Output is correct
56 Correct 262 ms 20700 KB Output is correct
57 Correct 248 ms 19696 KB Output is correct
58 Correct 335 ms 21288 KB Output is correct
59 Correct 137 ms 7604 KB Output is correct
60 Correct 257 ms 20172 KB Output is correct
61 Correct 259 ms 19868 KB Output is correct
62 Correct 287 ms 20732 KB Output is correct
63 Correct 253 ms 20060 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 1 ms 428 KB Output is correct
67 Correct 2 ms 332 KB Output is correct
68 Correct 1 ms 332 KB Output is correct
69 Correct 2 ms 460 KB Output is correct
70 Correct 2 ms 460 KB Output is correct
71 Correct 2 ms 460 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 2 ms 460 KB Output is correct
74 Correct 92 ms 17184 KB Output is correct
75 Correct 74 ms 14568 KB Output is correct
76 Correct 78 ms 14588 KB Output is correct
77 Correct 77 ms 15204 KB Output is correct
78 Correct 77 ms 13320 KB Output is correct
79 Correct 60 ms 9904 KB Output is correct
80 Correct 79 ms 14928 KB Output is correct
81 Correct 112 ms 19656 KB Output is correct
82 Correct 122 ms 20640 KB Output is correct
83 Correct 149 ms 22676 KB Output is correct
84 Correct 64 ms 14440 KB Output is correct
85 Correct 113 ms 20432 KB Output is correct
86 Correct 103 ms 7640 KB Output is correct
87 Correct 274 ms 20800 KB Output is correct
88 Correct 275 ms 20708 KB Output is correct
89 Correct 374 ms 20516 KB Output is correct
90 Correct 351 ms 19056 KB Output is correct
91 Correct 363 ms 19272 KB Output is correct
92 Correct 401 ms 20480 KB Output is correct
93 Correct 452 ms 25348 KB Output is correct
94 Correct 468 ms 26692 KB Output is correct
95 Correct 381 ms 27964 KB Output is correct
96 Correct 268 ms 19712 KB Output is correct
97 Correct 445 ms 23092 KB Output is correct