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

#include <bits/stdc++.h>
const int MAX = 3e5 + 10;
const int modn = 1e9 + 7;
typedef long long ll;
#define int ll
#define endl '\\n'
#define cwk                                                                    \\
  freopen("D:\\\\workplace\\\\CLion\\\\in.in", "r", stdin),                          \\
      freopen("D:\\\\workplace\\\\CLion\\\\out.out", "w", stdout)
using namespace std;
int h, w, m;
int x, y;
map<int, int> cntx, cnty;
map<int, int>::iterator it;
int maxx, maxy;
int ansx[MAX], ansy[MAX];
map<pair<int, int>, int> mp;
vector<int> flagx, flagy;
void solve() {
  cin >> h >> w >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    cntx[x]++;
    cnty[y]++;
    mp[pair<int, int>(x, y)]++;
  }
  for (it = cntx.begin(); it != cntx.end(); it++) {
    maxx = max(maxx, it->second);
  }
  for (it = cnty.begin(); it != cnty.end(); it++) {
    maxy = max(maxy, it->second);
  }
  for (it = cntx.begin(); it != cntx.end(); it++) {
    if (it->second == maxx)
      flagx.push_back(it->first);
  }
  for (it = cnty.begin(); it != cnty.end(); it++) {
    if (it->second == maxy)
      flagy.push_back(it->first);
  }
  int maxn = 0;
  for (int i = 0; i < flagx.size(); i++) {
    int xx = flagx[i];
    for (int j = 0; j < flagy.size(); j++) {
      int cnt = 0;
      int yy = flagy[j];
      if (mp[pair<int, int>(xx, yy)] == 0) {
        cout << maxx + maxy;
        return;
      }
    }
  }
  cout << maxx + maxy - 1;
}
signed main() {
  // ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  // cwk;
  int _ = 1;
  // cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int MAX = 3e5 + 10;
const int modn = 1e9 + 7;
typedef long long ll;
#define int ll
#define endl '\\n'
#define cwk                                                                    \\
  freopen("D:\\\\workplace\\\\CLion\\\\in.in", "r", stdin),                          \\
      freopen("D:\\\\workplace\\\\CLion\\\\out.out", "w", stdout)
using namespace std;
int h, w, m;
int x, y;
map<int, int> cntx, cnty;
map<int, int>::iterator it;
int maxx, maxy;
int ansx[MAX], ansy[MAX];
map<pair<int, int>, int> mp;
vector<int> flagx, flagy;
void solve() {
  cin >> h >> w >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    cntx[x]++;
    cnty[y]++;
    mp[pair<int, int>(x, y)]++;
  }
  for (it = cntx.begin(); it != cntx.end(); it++) {
    maxx = max(maxx, it->second);
  }
  for (it = cnty.begin(); it != cnty.end(); it++) {
    maxy = max(maxy, it->second);
  }
  for (it = cntx.begin(); it != cntx.end(); it++) {
    if (it->second == maxx)
      flagx.push_back(it->first);
  }
  for (it = cnty.begin(); it != cnty.end(); it++) {
    if (it->second == maxy)
      flagy.push_back(it->first);
  }
  int maxn = 0;
  for (int i = 0; i < flagx.size(); i++) {
    int xx = flagx[i];
    for (int j = 0; j < flagy.size(); j++) {
      int cnt = 0;
      int yy = flagy[j];
      cnt += cntx[xx] + cnty[yy];
      if (mp[pair<int, int>(xx, yy)] != 0)
        cnt--;
      maxn = max(maxn, cnt);
    }
  }
  cout << maxn;
}
signed main() {
  // ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  // cwk;
  int _ = 1;
  // cin>>_;
  while (_--) {
    solve();
  }
  return 0;
}