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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, m;
  cin >> h >> w >> m;

  map<P, int> field;
  vector<P> row(h);
  vector<P> col(w);
  rep(i, h) row[i] = P{0, i};
  rep(j, w) col[j] = P{0, j};
  rep(i, m) {
    int y, x;
    cin >> y >> x;
    --y, --x;
    field[P{y, x}] = 1;
    row[y] = P{row[y].first + 1, row[y].second};
    col[x] = P{col[x].first + 1, col[x].second};
  }

  sort(row.begin(), row.end(), greater<P>());
  sort(col.begin(), col.end(), greater<P>());

  int ans = 0;
  bool found = false;
  for (int i = 0; i < h && row[0].first == row[i].first; ++i) {
    for (int j = 0; j < w && col[0].first == col[j].first; ++j) {
      int y = row[i].second;
      int x = col[j].second;
      if (field.find(P{y, x}) != field.end()) {
        ans = max(ans, row[i].first + col[j].first - 1);
      } else {
        ans = max(ans, row[i].first + col[j].first);
        found = true;
      }
      if (found)
        break;
    }
    if (found)
      break;
  }

  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, m;
  cin >> h >> w >> m;

  map<P, int> field;
  vector<P> row(h);
  vector<P> col(w);
  rep(i, h) row[i] = P{0, i};
  rep(j, w) col[j] = P{0, j};
  rep(i, m) {
    int y, x;
    cin >> y >> x;
    --y, --x;
    field[P{y, x}] = 1;
    row[y] = P{row[y].first + 1, row[y].second};
    col[x] = P{col[x].first + 1, col[x].second};
  }

  sort(row.begin(), row.end(), greater<P>());
  sort(col.begin(), col.end(), greater<P>());

  int ans = 0;
  for (int i = 0; i < h && row[0].first == row[i].first; ++i) {
    for (int j = 0; j < w && col[0].first == col[j].first; ++j) {
      int y = row[i].second;
      int x = col[j].second;
      if (field.find(P{y, x}) != field.end()) {
        ans = max(ans, row[i].first + col[j].first - 1);
      } else {
        ans = max(ans, row[i].first + col[j].first);
      }
    }
  }

  cout << ans << "\\n";
  return 0;
}