Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define ll long long
#define ll long long
#define all(c) (c).begin(), (c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//
const int N = 3e5 + 5;
map<pair<int, int>, int> dict;
int h, w, m;
int x[N], y[N];

int32_t main() {
  fastio;
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  cin >> h >> w >> m;
  vector<pair<int, int>> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].first >> v[i].second;
    x[v[i].first]++;
    y[v[i].second]++;
    dict[{v[i].first, v[i].second}] = 1LL;
  }
  int mx_x = *max_element(x + 1, x + h + 1);
  int mx_y = *max_element(y + 1, y + w + 1);
  vector<int> X, Y;
  for (int i = 1; i <= h; i++)
    if (x[i] == mx_x)
      X.pb(i);
  for (int i = 1; i <= w; i++)
    if (y[i] == mx_y)
      Y.pb(i);
  int ans = 0;
  for (int i : X) {
    for (int j : Y) {
      int z = 0;
      if (dict.find({i, j}) != dict.end())
        z = 1;
      ans = max(ans, x[i] + y[j] - z);
    }
  }
  // for(int i=1;i<=h;i++)
  // {
  // 	for(int j=1;j<=w;j++)
  // 	{
  // 		int z = 0;
  // 		if(dict.find({i, j}) != dict.end()) z = 1;
  // 		ans = max(ans, x[i] + y[j] - z);
  // 	}
  // }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define ll long long
#define ll long long
#define all(c) (c).begin(), (c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//
const int N = 3e5 + 5;
map<pair<int, int>, int> dict;
int h, w, m;
int x[N], y[N];

int32_t main() {
  fastio;
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  cin >> h >> w >> m;
  vector<pair<int, int>> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].first >> v[i].second;
    x[v[i].first]++;
    y[v[i].second]++;
    dict[{v[i].first, v[i].second}] = 1LL;
  }
  int mx_x = *max_element(x + 1, x + h + 1);
  int mx_y = *max_element(y + 1, y + w + 1);
  vector<int> X, Y;
  for (int i = 1; i <= h; i++)
    if (x[i] == mx_x)
      X.pb(i);
  for (int i = 1; i <= w; i++)
    if (y[i] == mx_y)
      Y.pb(i);
  int ans = 0;
  for (int i : X) {
    for (int j : Y) {
      int z = 0;
      if (dict.find({i, j}) != dict.end())
        z = 1;
      ans = max(ans, x[i] + y[j] - z);
      if (z == 0) {
        cout << ans << "\n";
        return 0;
      }
    }
  }
  // for(int i=1;i<=h;i++)
  // {
  // 	for(int j=1;j<=w;j++)
  // 	{
  // 		int z = 0;
  // 		if(dict.find({i, j}) != dict.end()) z = 1;
  // 		ans = max(ans, x[i] + y[j] - z);
  // 	}
  // }
  cout << ans << "\n";
  return 0;
}