Problem p02580 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <class Monoid> class SegmentTree {
private:
  using T = typename Monoid::T;
  using size_type = std::size_t;
  size_type n;
  std::vector<T> data;
  void prop(int i) { data[i] = Monoid::op(data[2 * i], data[2 * i + 1]); }

public:
  SegmentTree() : n(0) {}

  SegmentTree(int n, T initial = Monoid::identity()) : n(n) {
    data.resize(n * 2, initial);
    for (int i = n - 1; i > 0; i--)
      data[i] = Monoid::op(data[i * 2], data[i * 2 + 1]);
  }

  template <class InputIter,
            class = typename std::iterator_traits<InputIter>::value_type>
  SegmentTree(InputIter first, InputIter last)
      : SegmentTree(distance(first, last)) {
    std::copy(first, last, data.begin() + n);
    for (int i = n - 1; i > 0; i--)
      prop(i);
  }

  SegmentTree(std::vector<T> v) : SegmentTree(v.begin(), v.end()) {}

  SegmentTree(std::initializer_list<T> v) : SegmentTree(v.begin(), v.end()) {}

  void set(size_t i, const T &v) {
    data[i += n] = v;
    while (i >>= 1)
      prop(i);
  }

  void add(size_t i, const T &v) {
    data[i += n] += v;
    while (i >>= 1)
      prop(i);
  }

  T get(size_t i) { return data[i + n]; }

  T fold(int l, int r) {
    if (l < 0)
      l = 0;
    if (l >= r)
      return Monoid::identity();
    if (r > int(n))
      r = n;
    T tmpL = Monoid::identity(), tmpR = Monoid::identity();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        tmpL = Monoid::op(tmpL, data[l++]);
      if (r & 1)
        tmpR = Monoid::op(data[--r], tmpR);
    }
    return Monoid::op(tmpL, tmpR);
  }

  size_type size() { return n; }
};

template <class U = long long> struct RangeMax {
  using T = U;
  static T op(const T &a, const T &b) { return std::max<T>(a, b); }
  static constexpr T identity() { return std::numeric_limits<T>::min(); }
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int h, w, m;
  std::cin >> h >> w >> m;
  std::vector<int> x(m), y(m);
  rep(i, m) std::cin >> x[i] >> y[i];
  rep(i, m) x[i]--, y[i]--;

  std::vector<int> indx(m);
  rep(i, m) indx[i] = i;
  std::sort(all(indx), [&](auto i, auto j) { return x[i] < x[j]; });

  std::vector<int> sx(h), sy(w);
  rep(i, m) {
    sx[x[i]]++;
    sy[y[i]]++;
  }

  SegmentTree<RangeMax<ll>> sg(w);
  rep(i, w) sg.set(i, sy[i]);

  ll ans = 0;
  int s = 0, t = 0;
  for (int i = 0; i < h; i++) {
    for (; t < m; t++) {
      if (x[indx[t]] > i)
        break;
      sg.add(y[indx[t]], -1);
    }
    ans = std::max<ll>(ans, sx[i] + sg.fold(0, w + 1));
    for (int j = s; j < t; j++)
      sg.add(y[indx[j]], +1);
    s = t;
  }
  std::cout << ans << std::endl;

  return 0;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int h, w, m;
  std::cin >> h >> w >> m;
  std::vector<int> x(m), y(m);
  rep(i, m) std::cin >> x[i] >> y[i];

  int xmax = 0;
  auto v = x;
  std::sort(all(v));
  v.erase(std::unique(all(v)), v.end());
  rep(i, m) {
    x[i] = std::lower_bound(all(v), x[i]) - v.begin();
    xmax = std::max(xmax, x[i]);
  }

  int ymax = 0;
  v = y;
  std::sort(all(v));
  v.erase(std::unique(all(v)), v.end());
  rep(i, m) {
    y[i] = std::lower_bound(all(v), y[i]) - v.begin();
    ymax = std::max(ymax, y[i]);
  }
  xmax++, ymax++;

  std::vector<std::vector<int>> G(xmax, std::vector<int>(ymax, 0));
  rep(i, m) G[x[i]][y[i]] = 1;

  std::vector<int> sx(xmax), sy(ymax);
  rep(i, m) {
    sx[x[i]]++;
    sy[y[i]]++;
  }

  int ans = 0;
  rep(i, xmax) rep(j, ymax) ans = std::max(ans, sx[i] + sy[j] - G[i][j]);
  std::cout << ans << std::endl;
  return 0;
}