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

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mp make_pair
#define ld long double
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int>> small_heap;
typedef priority_queue<int> big_heap;
const int N = 1e6 + 100;
int T;
int h, w, m;
pii d[N];
vector<int> v[N], vv[N], mmm;
int num[N], nnum[N];

int main() {
  scanf("%d%d%d", &h, &w, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &d[i].first, &d[i].second);
    v[d[i].first].push_back(d[i].second);
    vv[d[i].second].push_back(d[i].first);
  }
  int mmax = -1;
  for (int i = 1; i <= h; i++) {
    num[i] = v[i].size();
    if (num[i] > mmax) {
      mmax = num[i];
      mmm.clear();
      mmm.push_back(i);
    }
    if (num[i] == mmax)
      mmm.push_back(i);
  }
  for (int i = 1; i <= w; i++)
    nnum[i] = vv[i].size();
  int maxx = -1;
  for (auto x : mmm) {
    sort(v[x].begin(), v[x].end());
  }
  for (int i = 1; i <= w; i++) {
    if (num[mmm[0]] + nnum[i] - 1 >= maxx) {
      for (auto x : mmm) {
        int pos = lower_bound(v[x].begin(), v[x].end(), i) - v[x].begin();
        if (v[x][pos] != i) {
          maxx = max(maxx, num[x] + nnum[i]);
          break;
        }
        maxx = max(maxx, num[x] + nnum[i] - 1);
      }
    }
    //        for (auto x:mmm) {
    //            if (num[x] + nnum[i] - 1 >= maxx) {
    //                if (mmp[make_pair(x, i)] == 0) {
    //                    maxx = num[x] + nnum[i];
    //                    break;
    //                } else {
    //                    maxx = num[x] + nnum[i] - (mmp[make_pair(x, i)] == 1 ?
    //                    1 : 0);
    //                }
    //            }
    //        }
  }
  printf("%d\\n", maxx);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mp make_pair
#define ld long double
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int>> small_heap;
typedef priority_queue<int> big_heap;
const int N = 1e6 + 100;
int T;
int h, w, m;
pii d[N];
vector<int> v[N], vv[N], mmm;
map<pii, int> mmp;
int num[N], nnum[N];

int main() {
  scanf("%d%d%d", &h, &w, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &d[i].first, &d[i].second);
    mmp[d[i]] = 1;
    v[d[i].first].push_back(d[i].second);
    vv[d[i].second].push_back(d[i].first);
  }
  int mmax = -1;
  for (int i = 1; i <= h; i++) {
    num[i] = v[i].size();
    if (num[i] > mmax) {
      mmax = num[i];
      mmm.clear();
      mmm.push_back(i);
    }
    if (num[i] == mmax)
      mmm.push_back(i);
  }
  for (int i = 1; i <= w; i++)
    nnum[i] = vv[i].size();
  int maxx = -1;
  for (int i = 1; i <= w; i++) {
    for (auto x : mmm) {
      if (num[x] + nnum[i] - 1 >= maxx) {
        if (mmp[make_pair(x, i)] == 0) {
          maxx = num[x] + nnum[i];
          break;
        } else {
          maxx = num[x] + nnum[i] - (mmp[make_pair(x, i)] == 1 ? 1 : 0);
        }
      }
    }
  }
  cout << maxx << endl;
  return 0;
}