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

#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template <typename T> long long modpow(const T n, const T p, const T mod);
template <typename T> long long modinv(const T n, const T mod);
template <typename T> bool chmax(T &a, const T &b);
template <typename T> bool chmin(T &a, const T &b);

long long inf = 1000000007;

int main() {
  long long h, w, m;
  cin >> h >> w >> m;
  map<pair<long long, long long>, long long> zisyo;
  vector<long long> H(300001, 0), W = H;

  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    H[a]++;
    W[b]++;
    zisyo[make_pair(a, b)] = 1;
  }

  long long cntH = -1;
  long long cntW = -1;
  vector<long long> maxH, maxW;

  for (long long i = 0; i <= 300000; i++) {
    if (H.at(i) > cntH) {
      maxH.clear();
      maxH.push_back(i);
      cntH = H.at(i);
    } else if (H.at(i) == cntH) {
      maxH.push_back(i);
    }

    if (W.at(i) > cntW) {
      maxW.clear();
      maxW.push_back(i);
      cntW = W.at(i);
    } else if (W.at(i) == cntW) {
      maxW.push_back(i);
    }
  }

  long long add = -1;

  for (auto i : maxH) {
    for (auto j : maxW) {
      if (!zisyo.count(make_pair(i, j))) {
        add = 0;
        break;
      }
    }
  }

  cout << cntH + cntW + add << endl;

  return 0;
}

template <typename T> long long modpow(const T n, const T p, const T mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long a = modpow(n, p / 2, mod);
    return a * a % mod;
  }
  if (p % 2 == 1)
    return (modpow(n, p - 1, mod) * n) % mod;
  cerr << "ERROR" << endl;
  return 1;
}

template <typename T> long long modinv(const T n, const T mod) {
  return modpow(n, mod - 2, mod);
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template <typename T> long long modpow(const T n, const T p, const T mod);
template <typename T> long long modinv(const T n, const T mod);
template <typename T> bool chmax(T &a, const T &b);
template <typename T> bool chmin(T &a, const T &b);

long long inf = 1000000007;

int main() {
  long long h, w, m;
  cin >> h >> w >> m;
  map<pair<long long, long long>, long long> zisyo;
  vector<long long> H(300001, 0), W = H;

  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    H[a]++;
    W[b]++;
    zisyo[make_pair(a, b)] = 1;
  }

  long long cntH = -1;
  long long cntW = -1;
  vector<long long> maxH, maxW;

  for (long long i = 0; i <= 300000; i++) {
    if (H.at(i) > cntH) {
      maxH.clear();
      maxH.push_back(i);
      cntH = H.at(i);
    } else if (H.at(i) == cntH) {
      maxH.push_back(i);
    }

    if (W.at(i) > cntW) {
      maxW.clear();
      maxW.push_back(i);
      cntW = W.at(i);
    } else if (W.at(i) == cntW) {
      maxW.push_back(i);
    }
  }

  long long add = -1;

  for (auto i : maxH) {
    for (auto j : maxW) {
      if (!zisyo.count(make_pair(i, j)))
        add = 0;
    }
  }

  cout << cntH + cntW + add << endl;

  return 0;
}

template <typename T> long long modpow(const T n, const T p, const T mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long a = modpow(n, p / 2, mod);
    return a * a % mod;
  }
  if (p % 2 == 1)
    return (modpow(n, p - 1, mod) * n) % mod;
  cerr << "ERROR" << endl;
  return 1;
}

template <typename T> long long modinv(const T n, const T mod) {
  return modpow(n, mod - 2, mod);
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}