Submission #258836

# Submission time Handle Problem Language Result Execution time Memory
258836 2020-08-06T15:27:08 Z EntityIT Palembang Bridges (APIO15_bridge) C++14
100 / 100
180 ms 7020 KB
#include <bits/stdc++.h>
using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())

template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
  template<class... Args>
  vec(size_t n = 0, Args... args)
      : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
  template<class... Args>
  vec(Args... args)
      : vector<T>(args...) {}
};

template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }

mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));

template<class T>
struct BIT {
  vec<T, 1> a;
  bool reversed;
  BIT(int n, bool t_reversed = false)
      : a(n),
        reversed(t_reversed) {}

  template<class U>
  void Add(int i, U value) {
    for (i = reversed ? SZ(a) - 1 - i : i; i < SZ(a); i |= i + 1) {
      a[i] += value;
    }
  }
  T Get(int i) const {
    T ret{};
    for (i = reversed ? SZ(a) - 1 - i : i; ~i; i = (i & (i + 1)) - 1) {
      ret += a[i];
    }
    return ret;
  }

  template<class U>
  void AddPosition(int i, U value) {
    Add(i, value);
  }
  T GetRange(int l, int r) const { return Get(r) - Get(l - 1); }

  template<class U>
  void AddRange(int l, int r, U value) {
    Add(l, value);
    Add(r + 1, -value);
  }
  T GetPosition(int i) const {
    return Get(i);
  }

  int UpperBound(T value) const {
    int ret = 0;
    for (int i = __lg(SZ(a)); ~i; --i) {
      ret ^= 1 << i;
      if (ret <= SZ(a) && value >= a[ret - 1]) {
        value -= a[ret - 1];
      } else {
        ret ^= 1 << i;
      }
    }
    return ret;
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  int n_citizens, n_bridges; cin >> n_bridges >> n_citizens;

  int64_t answer = 0;
  vec<pair<int, int>, 1> intervals;
  vec<int, 1> xs;
  while (n_citizens--) {
    char source_zone, target_zone; int source_x, target_x; cin >> source_zone >> source_x >> target_zone >> target_x;
    if (source_zone != target_zone) {
      ++answer;
      intervals.emplace_back(min(source_x, target_x), max(source_x, target_x));
      xs.emplace_back(intervals.back().first);
      xs.emplace_back(intervals.back().second);
    } else {
      answer += abs(source_x - target_x);
    }
  }
  sort(ALL(intervals), [&](const auto& i, const auto& j) {
    return i.first + i.second < j.first + j.second;
  });
  sort(ALL(xs)); xs.erase(unique(ALL(xs)), xs.end());

  if (!SZ(xs)) {
    cout << answer << '\n';
    return 0;
  }

  BIT<int> bit_left_amount(SZ(xs)), bit_right_amount(SZ(xs));
  BIT<int64_t> bit_left_sum(SZ(xs)), bit_right_sum(SZ(xs));
  for (auto& interval : intervals) {
    int index_x = static_cast<int>(lower_bound(ALL(xs), interval.first) - xs.begin());
    bit_right_amount.Add(index_x, 1);
    bit_right_sum.Add(index_x, interval.first);

    index_x = static_cast<int>(lower_bound(ALL(xs), interval.second) - xs.begin());
    bit_right_amount.Add(index_x, 1);
    bit_right_sum.Add(index_x, interval.second);
  }

  int64_t result = numeric_limits<int64_t>::max();
  auto Solve = [&]() {
    int64_t ret = 0;

    int amount = bit_left_amount.Get(SZ(xs) - 1),
        index_x = bit_left_amount.UpperBound(amount >> 1);
    if (amount) {
      ret += bit_left_sum.Get(SZ(xs) - 1) - (bit_left_sum.Get(index_x) << 1) +  static_cast<int64_t>(xs[index_x]) * ((bit_left_amount.Get(index_x) << 1) - amount);
    }

    amount = bit_right_amount.Get(SZ(xs) - 1);
    index_x = bit_right_amount.UpperBound(amount >> 1);
    if (amount) {
      ret += bit_right_sum.Get(SZ(xs) - 1) - (bit_right_sum.Get(index_x) << 1) +  static_cast<int64_t>(xs[index_x]) * ((bit_right_amount.Get(index_x) << 1) - amount);
    }

    Minimize(result, ret);
  };

  Solve();
  if (n_bridges == 2) {
    for (auto& interval : intervals) {
      int index_x = static_cast<int>(lower_bound(ALL(xs), interval.first) - xs.begin());
      bit_left_amount.Add(index_x, 1);
      bit_left_sum.Add(index_x, interval.first);
      bit_right_amount.Add(index_x, -1);
      bit_right_sum.Add(index_x, -interval.first);

      index_x = static_cast<int>(lower_bound(ALL(xs), interval.second) - xs.begin());
      bit_left_amount.Add(index_x, 1);
      bit_left_sum.Add(index_x, interval.second);
      bit_right_amount.Add(index_x, -1);
      bit_right_sum.Add(index_x, -interval.second);

      Solve();
    }
  }

  cout << answer + result << '\n';

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 26 ms 2164 KB Output is correct
13 Correct 96 ms 6880 KB Output is correct
14 Correct 59 ms 2164 KB Output is correct
15 Correct 54 ms 4324 KB Output is correct
16 Correct 33 ms 2292 KB Output is correct
17 Correct 59 ms 6884 KB Output is correct
18 Correct 68 ms 6792 KB Output is correct
19 Correct 79 ms 6752 KB Output is correct
20 Correct 39 ms 2156 KB Output is correct
21 Correct 72 ms 6864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 32 ms 2164 KB Output is correct
26 Correct 60 ms 2164 KB Output is correct
27 Correct 165 ms 6500 KB Output is correct
28 Correct 180 ms 7020 KB Output is correct
29 Correct 171 ms 6884 KB Output is correct
30 Correct 86 ms 3948 KB Output is correct
31 Correct 39 ms 2164 KB Output is correct
32 Correct 112 ms 6892 KB Output is correct
33 Correct 101 ms 6880 KB Output is correct
34 Correct 130 ms 6624 KB Output is correct
35 Correct 44 ms 2156 KB Output is correct
36 Correct 121 ms 7012 KB Output is correct
37 Correct 31 ms 2164 KB Output is correct