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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1ll, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}

int main(void) {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> y(m), x(m);
  vector<pair<int, int>> data;
  rep(i, 0, m) {
    cin >> y[i] >> x[i];
    data.push_back({x[i], y[i]});
  }
  map<int, int> mx, my;
  rep(i, 0, m) {
    mx[x[i]]++;
    my[y[i]]++;
  }
  int A = 0, B = 0, d = 1;
  vector<int> tx, ty;
  rep2(m, mx) {
    if (A < m.second)
      A = m.second, tx.resize(0), tx.push_back(m.first);
    else if (A == m.second)
      tx.push_back(m.first);
  }
  rep2(m, my) {
    if (B < m.second)
      B = m.second, ty.resize(0), ty.push_back(m.first);
    else if (B == m.second)
      ty.push_back(m.first);
  }
  sort(all(data));
  rep2(vx, tx) {
    rep2(vy, ty) {
      if (!binary_search(all(data), make_pair(vx, vy))) {
        d = 0;
      }
    }
    if (d == 0)
      break;
  }
  printf("%d\\n", A + B - d);
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1ll, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}

int main(void) {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> y(m), x(m);
  rep(i, 0, m) cin >> y[i] >> x[i];
  map<int, int> mx, my;
  rep(i, 0, m) {
    mx[x[i]]++;
    my[y[i]]++;
  }
  int A = 0, B = 0, d = 1;
  vector<int> tx, ty;
  rep2(m, mx) {
    if (A < m.second)
      A = m.second, tx.resize(0), tx.push_back(m.first);
    else if (A == m.second)
      tx.push_back(m.first);
  }
  rep2(m, my) {
    if (B < m.second)
      B = m.second, ty.resize(0), ty.push_back(m.first);
    else if (B == m.second)
      ty.push_back(m.first);
  }
  bool ok = true;
  rep2(vx, tx) {
    rep2(vy, ty) {
      ok = true;
      rep(i, 0, m) {
        if (vx == x[i] && vy == y[i]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        d = 0;
        break;
      }
    }
    if (ok)
      break;
  }
  printf("%d\\n", A + B - d);
  return 0;
}