Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define lld long long int
#define vec vector<lld>
#define pll pair<lld, lld>
#define pb push_back
#define ss second
#define ff first
#define mp make_pair
#define mod 1000000007
#define answer(x) cout << endl << "answer : " << x << endl << endl;
#define op(x) cout << x << endl;
#define maxn 500005
#define messi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  messi lld t = 1; // cin>>t;
  while (t--) {
    lld n = 0, m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0, z = 0,
        ans = 0, cnt = 0, l = 0, r = 0, mid = 0, lo = 0, hi = 0;
    string s;
    bool flag = false;
    lld h, w;
    cin >> h >> w >> m;
    set<pair<lld, lld>> st;
    lld row[h + 1];
    lld col[w + 1];
    for (i = 0; i <= h; i++)
      row[i] = 0;
    for (i = 0; i <= w; i++)
      col[i] = 0;
    for (i = 0; i < m; i++) {
      cin >> x >> y;
      row[x]++;
      col[y]++;
      st.insert(mp(x, y));
    }
    vec v1;
    pair<lld, lld> pp;
    lld mx1 = 0, mx2 = 0;
    for (i = 0; i <= h; i++) {
      if (row[i] > mx1) {
        v1.clear();
        mx1 = row[i];
        v1.pb(i);
      } else if (row[i] == mx1)
        v1.pb(i);
    }
    vec v2;
    for (i = 0; i <= w; i++) {
      if (col[i] > mx2) {
        v2.clear();
        mx2 = col[i];
        v2.pb(i);
      } else if (col[i] == mx2)
        v2.pb(i);
    }
    for (auto u : v1) {
      for (j = 0; j < v2.size(); j++) {
        x = u;
        y = v2[j];
        cnt = mx1 + mx2;
        pp.ff = x;
        pp.ss = y;
        if (st.find(pp) != st.end())
          cnt--;
        else {
          ans = max(ans, cnt);
          flag = true;
          break;
        }
        ans = max(ans, cnt);
      }
      if (flag)
        break;
    }
    op(ans);
  }
  return 0;
}
/*  CODED BY:-
 ________________________________________
|            __        __                |
|  /\\   /\\  |__)  /\\  |  \\ |__| \\ /  /\\  |
| /~~\\ /~~\\ |  \\ /~~\\ |__/ |  |  |  /~~\\ |
|________________________________________|

*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lld long long int
#define vec vector<lld>
#define pll pair<lld, lld>
#define pb push_back
#define ss second
#define ff first
#define mp make_pair
#define mod 1000000007
#define answer(x) cout << endl << "answer : " << x << endl << endl;
#define op(x) cout << x << endl;
#define maxn 500005
#define messi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  messi lld t = 1; // cin>>t;
  while (t--) {
    lld n = 0, m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0, z = 0,
        ans = 0, cnt = 0, l = 0, r = 0, mid = 0, lo = 0, hi = 0;
    string s;
    bool flag = false;
    lld h, w;
    cin >> h >> w >> m;
    set<pair<lld, lld>> st;
    lld row[h + 1];
    lld col[w + 1];
    for (i = 0; i <= h; i++)
      row[i] = 0;
    for (i = 0; i <= w; i++)
      col[i] = 0;
    for (i = 0; i < m; i++) {
      cin >> x >> y;
      row[x]++;
      col[y]++;
      st.insert(mp(x, y));
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        x = row[i] + col[j];
        pair<lld, lld> pp;
        pp.ff = i, pp.ss = j;
        if (st.find(pp) != st.end())
          x--;
        cnt = max(cnt, x);
      }
    }
    op(cnt);
  }
  return 0;
}
/*  CODED BY:-
 ________________________________________
|            __        __                |
|  /\\   /\\  |__)  /\\  |  \\ |__| \\ /  /\\  |
| /~~\\ /~~\\ |  \\ /~~\\ |__/ |  |  |  /~~\\ |
|________________________________________|

*/