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

#include <bits/stdc++.h>

// @author : sam000013

#define int long long
#define PI 3.14159265
#define pb(x) push_back(x)
#define PI 3.14159265
#define all(v) v.begin(), v.end()
#define endl cout << "\\n"
#define dbg(x)                                                                 \\
  cout << #x << " " << x;                                                      \\
  endl;
#define bye                                                                    \\
  endl;                                                                        \\
  return
#define uni 1.000000
using namespace std;

const int mod = 1000000007;
// template<typename T>

// void debug(vector<T> x){
//     for(auto e:x){
//         cout<<e<<" ";
//     }
//     cout<<"\\n";
// }
/*


*/

vector<int> column(300005, 0);
multiset<int> best;
vector<int> inRow[300005];

void upd(int pos, int delta) {
  best.erase(best.find(column[pos]));
  column[pos] += delta;
  best.insert(column[pos]);
}
void solve() {
  int h, w, m;
  cin >> h >> w >> m;

  for (int i = 0; i < m; i++) {
    column[i] = 0;
    best.insert(0);
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    inRow[x].push_back(y);
    upd(y, 1);
  }

  int ans = 0;
  for (int i = 0; i < h; i++) {
    int cur = inRow[i].size();
    for (auto x : inRow[i]) {
      upd(x, -1);
    }

    if (best.size())
      cur += *(best.rbegin());
    // cout<<cur<<" ";
    ans = max(ans, cur);
    for (auto x : inRow[i]) {
      upd(x, 1);
    }
  }
  cout << ans;
  bye;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// @author : sam000013

#define int long long
#define PI 3.14159265
#define fi(i, start, end) for (int i = 0; i < n; i++)
#define pb(x) push_back(x)
#define int long long
#define PI 3.14159265
#define all(v) v.begin(), v.end()
#define endl cout << "\\n"
#define ll long long
#define dbg(x)                                                                 \\
  cout << #x << " " << x;                                                      \\
  endl;
#define bye                                                                    \\
  endl;                                                                        \\
  return
#define uni 1.000000
using namespace std;

int mod = 1000000007;
template <typename T>

void debug(vector<T> x) {
  for (auto e : x) {
    cout << e << " ";
  }
  cout << "\\n";
}
/*
1
8 6
1 2 4 1 2 4 1 2

*/
bool isValid(int i, int j, int n, int m) {
  return (i >= 0 && i < n && j >= 0 && j < m);
}
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
ll powerer(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = ans * a;
    }
    b = b >> 1;
    a = a * a;
  }
  return ans;
}
void solve() {
  int h, w;
  cin >> h >> w;
  vector<vector<pair<int, int>>> helper(h, vector<pair<int, int>>(w));
  vector<vector<bool>> targets(h, vector<bool>(w));
  int m;
  cin >> m;
  vector<pair<int, int>> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].first >> v[i].second;
    v[i].first--;
    v[i].second--;
    targets[v[i].first][v[i].second] = true;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i == 0 && j == 0) {
        if (targets[i][j])
          helper[i][j] = make_pair(1, 1);
        else
          helper[i][j] = make_pair(0, 0);
      } else if (i == 0) {
        if (targets[i][j]) {
          helper[i][j].first = helper[i][j - 1].first + 1;
          helper[i][j].second = 1;
        } else {
          helper[i][j].first = helper[i][j - 1].first;
          helper[i][j].second = 0;
        }
      } else if (j == 0) {
        if (targets[i][j]) {
          helper[i][j] = make_pair(1, helper[i - 1][j].second + 1);
        } else {
          helper[i][j] = make_pair(0, helper[i - 1][j].second);
        }
      } else {
        if (targets[i][j]) {
          helper[i][j] = make_pair(helper[i][j - 1].first + 1,
                                   helper[i - 1][j].second + 1);
        } else {
          helper[i][j] =
              make_pair(helper[i][j - 1].first, helper[i - 1][j].second);
        }
      }
    }
  }
  int ans = 0;
  vector<int> row(h);
  vector<int> column(w);
  for (int i = 0; i < h; i++) {
    int mxm = 0;
    for (int j = 0; j < w; j++) {
      mxm = max(mxm, helper[i][j].first);
    }

    for (int j = 0; j < w; j++) {
      helper[i][j].first = mxm;
    }
  }
  for (int j = 0; j < w; j++) {
    int mxm = 0;
    for (int i = 0; i < h; i++) {
      mxm = max(mxm, helper[i][j].second);
    }
    for (int i = 0; i < h; i++)
      helper[i][j].second = mxm;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      // cout<<"("<<helper[i][j].first<<","<<helper[i][j].second<<"), ";
      int x = helper[i][j].first + helper[i][j].second;
      if (targets[i][j])
        x--;
      ans = max(x, ans);
    }
    // endl;
  }
  cout << ans;
  endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}