Problem p02580 (C++)

Buggy submission (status: Runtime Error):
// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, r) for (int i = (l); i < (r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

// Segment Tree
template <typename Operator> struct SegmentTree {
  Operator OP;
  using NodeType = decltype(OP.NodeE);
  int length;
  vector<NodeType> node;
  SegmentTree(int N) {
    length = 1;
    while (length < N)
      length <<= 1;
    node.assign(length << 1, OP.NodeE);
  }
  SegmentTree(vector<NodeType> &vec) {
    length = 1;
    while (length < vec.size())
      length <<= 1;
    node.assign(2 * length, OP.NodeE);
    rep(i, vec.size()) node[i + length] = vec[i];
    for (int i = length - 1; i > 0; --i)
      node[i] = OP.func(node[(i << 1) + 0], node[(i << 1) + 1]);
  }
  void update(int idx, NodeType val) {
    idx += length;
    node[idx] = OP.change(node[idx], val);
    while (idx >>= 1)
      node[idx] = OP.func(node[(idx << 1) + 0], node[(idx << 1) + 1]);
  }
  NodeType get(int l, int r) {
    l += length;
    r += length;
    NodeType vl = OP.NodeE, vr = OP.NodeE;
    while (r > l) {
      if (l & 1)
        vl = OP.func(vl, node[l++]);
      if (r & 1)
        vr = OP.func(node[--r], vr);
      l >>= 1;
      r >>= 1;
    }
    return OP.func(vl, vr);
  }
};
struct RMQ {
  using NodeType = int;
  NodeType NodeE = 0;
  NodeType change(NodeType x, NodeType y) { return y; }
  NodeType func(NodeType x, NodeType y) { return max(x, y); }
};

// Main
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    --x[i];
    --y[i];
  }

  vector<vector<int>> g(w);
  rep(i, m) g[y[i]].push_back(x[i]);
  vector<int> cnt1(h, 0), cnt2(w, 0);
  rep(i, m) {
    ++cnt1[x[i]];
    ++cnt2[y[i]];
  }
  SegmentTree<RMQ> st(cnt1);
  int ans = 0;
  rep(i, w) {
    for (int n : g[i])
      st.update(n, cnt1[n] - 1);
    int mx = st.get(0, m);
    chmax(ans, mx + cnt2[i]);
    for (int n : g[i])
      st.update(n, cnt1[n]);
  }

  cout << ans << "\n";
  return 0;
}


Fixed submission:
// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, r) for (int i = (l); i < (r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

// Segment Tree
template <typename Operator> struct SegmentTree {
  Operator OP;
  using NodeType = decltype(OP.NodeE);
  int length;
  vector<NodeType> node;
  SegmentTree(int N) {
    length = 1;
    while (length < N)
      length <<= 1;
    node.assign(length << 1, OP.NodeE);
  }
  SegmentTree(vector<NodeType> &vec) {
    length = 1;
    while (length < vec.size())
      length <<= 1;
    node.assign(2 * length, OP.NodeE);
    rep(i, vec.size()) node[i + length] = vec[i];
    for (int i = length - 1; i > 0; --i)
      node[i] = OP.func(node[(i << 1) + 0], node[(i << 1) + 1]);
  }
  void update(int idx, NodeType val) {
    idx += length;
    node[idx] = OP.change(node[idx], val);
    while (idx >>= 1)
      node[idx] = OP.func(node[(idx << 1) + 0], node[(idx << 1) + 1]);
  }
  NodeType get(int l, int r) {
    l += length;
    r += length;
    NodeType vl = OP.NodeE, vr = OP.NodeE;
    while (r > l) {
      if (l & 1)
        vl = OP.func(vl, node[l++]);
      if (r & 1)
        vr = OP.func(node[--r], vr);
      l >>= 1;
      r >>= 1;
    }
    return OP.func(vl, vr);
  }
};
struct RMQ {
  using NodeType = int;
  NodeType NodeE = 0;
  NodeType change(NodeType x, NodeType y) { return y; }
  NodeType func(NodeType x, NodeType y) { return max(x, y); }
};

// Main
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    --x[i];
    --y[i];
  }

  vector<vector<int>> g(w);
  rep(i, m) g[y[i]].push_back(x[i]);
  vector<int> cnt1(h, 0), cnt2(w, 0);
  rep(i, m) {
    ++cnt1[x[i]];
    ++cnt2[y[i]];
  }
  SegmentTree<RMQ> st(cnt1);
  int ans = 0;
  rep(i, w) {
    for (int n : g[i])
      st.update(n, cnt1[n] - 1);
    int mx = st.get(0, h);
    chmax(ans, mx + cnt2[i]);
    for (int n : g[i])
      st.update(n, cnt1[n]);
  }

  cout << ans << "\n";
  return 0;
}