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

#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define INF 1000000000
#define endl "\\n"
#define all(s) s.begin(), s.end()
#define pb push_back
#define uno unordered_map
#define pi 3.1415926536
#define dig(a) fixed << setprecision(a)
#define test(zz)                                                               \\
  int zz;                                                                      \\
  cin >> zz;                                                                   \\
  while (zz--)
#define foreach(var, i) for (auto i = var.begin(); i != var.end(); i++)
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res *= a;
    }
    n >>= 1;
    a *= a;
  }
  return res;
}
ll powerm(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res = (res * a) % m;
    }
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}
using namespace std;

int main() {
  fast();
  // test(t){}
  int r, c, tar;
  cin >> r >> c >> tar;
  map<pair<int, int>, int> vis;
  map<int, int> row, col;
  int x, y, mx = 0, my = 0, pos_x, pos_y;
  for (int i = 0; i < tar; i++) {
    cin >> x >> y;
    vis[{x, y}] = 1;
    row[x]++;
    col[y]++;
    if (row[x] > mx) {
      mx = row[x];
      pos_x = x;
    }
    if (col[y] > my) {
      my = col[y];
      pos_y = y;
    }
  }
  int res, ma = 0;
  for (int i = 1; i <= c; i++) {
    res = mx + col[i];
    if (vis[{pos_x, i}]) {
      res--;
    }
    ma = max(ma, res);
  }
  for (int i = 1; i <= r; i++) {
    res = my + row[i];
    if (vis[{i, pos_y}]) {
      res--;
    }
    ma = max(res, ma);
  }
  cout << ma << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define INF 1000000000
#define endl "\\n"
#define all(s) s.begin(), s.end()
#define pb push_back
#define uno unordered_map
#define pi 3.1415926536
#define dig(a) fixed << setprecision(a)
#define test(zz)                                                               \\
  int zz;                                                                      \\
  cin >> zz;                                                                   \\
  while (zz--)
#define foreach(var, i) for (auto i = var.begin(); i != var.end(); i++)
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res *= a;
    }
    n >>= 1;
    a *= a;
  }
  return res;
}
ll powerm(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n % 2) {
      res = (res * a) % m;
    }
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}
using namespace std;

int main() {
  fast();
  // test(t){}
  int r, c, tar;
  cin >> r >> c >> tar;
  int x, y;
  map<pair<int, int>, int> mp;
  map<int, int> row;
  map<int, int> col;
  for (int i = 0; i < tar; i++) {
    cin >> x >> y;
    row[x - 1]++;
    col[y - 1]++;
    mp[{x - 1, y - 1}] = 1;
  }
  int ans = INT_MIN;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mp[{i, j}] == 1) {
        ans = max(ans, row[i] + col[j] - 1);
      } else {
        ans = max(ans, row[i] + col[j]);
      }
    }
  }
  cout << ans << endl;

  return 0;
}