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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N = 3e5 + 10;
const int mod = 1e9 + 7;
int a[N], b[N];
ll maxs = -9999;
ll sum;
struct A {
  int x, y;
};
map<pair<int, int>, int> mp;
int main() {
  int h, w, m;
  int q[N], t[N];
  int cnt1, cnt2;
  cin >> h >> w >> m;
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x, y)]++;
    a[x]++;
    b[y]++;
  }
  for (int i = 1; i <= h; i++) {
    if (sum1 < a[i]) {
      cnt1 = 0;
      memset(q, 0, sizeof(q));
      sum1 = max(sum1, a[i]);
      q[cnt1++] = i;
      while (sum1 == a[i + 1]) {
        q[cnt1++] = i;
        i++;
      }
    }
    if (sum1 == a[i]) {
      q[cnt1++] = i;
      while (sum1 == a[i + 1]) {
        q[cnt1++] = i;
        i++;
      }
    }
  }
  for (int i = 1; i <= w; i++) {
    if (sum2 < b[i]) {
      cnt2 = 0;
      memset(t, 0, sizeof(t));
      sum2 = max(sum2, b[i]);
      t[cnt2++] = i;
      while (sum2 == b[i + 1]) {
        t[cnt2++] = i;
        i++;
      }
    }
    if (sum2 == b[i]) {
      t[cnt2++] = i;
      while (sum2 == b[i + 1]) {
        t[cnt2++] = i;
        i++;
      }
    }
  }
  sum = sum1 + sum2;
  int xx = 0;
  for (int i = 0; i < cnt1; i++) {
    for (int j = 0; j < cnt2; j++) {
      int x1, y1;
      x1 = q[i];
      y1 = t[j];
      if (mp[make_pair(x1, y1)] == 0) {
        xx = 1;
        break;
      }
    }
    if (xx == 1) {
      break;
    }
  }
  if (xx == 0) {
    sum--;
  }
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N = 3e5 + 50;
const int mod = 1e9 + 7;
int a[N], b[N];
ll maxs = -9999;
ll sum;
struct A {
  int x, y;
};
map<pair<int, int>, int> mp;
int main() {
  int h, w, m;
  int q[N], t[N];
  int cnt1, cnt2;
  cin >> h >> w >> m;
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x, y)]++;
    a[x]++;
    b[y]++;
  }
  for (int i = 1; i <= h; i++) {
    if (sum1 <= a[i]) {
      if (a[i] > sum1) {
        cnt1 = 0;
        memset(q, 0, sizeof(q));
      }
      sum1 = max(sum1, a[i]);
      q[cnt1++] = i;
      while (sum1 == a[i + 1]) {
        q[cnt1++] = i;
        i++;
      }
    }
  }
  for (int i = 1; i <= w; i++) {
    if (sum2 <= b[i]) {
      if (b[i] > sum2) {
        cnt2 = 0;
        memset(t, 0, sizeof(t));
      }
      sum2 = max(sum2, b[i]);
      t[cnt2++] = i;
      while (sum2 == b[i + 1]) {
        t[cnt2++] = i;
        i++;
      }
    }
  }
  sum = sum1 + sum2;
  int xx = 0;
  for (int i = 0; i < cnt1; i++) {
    int x1, y1;
    for (int j = 0; j < cnt2; j++) {

      x1 = q[i];
      y1 = t[j];
      if (mp[make_pair(x1, y1)] == 0) {
        xx = 1;
        break;
      }
    }
    if (xx == 1) {
      break;
    }
  }
  if (xx == 0) {
    sum--;
  }
  cout << sum << endl;
  return 0;
}