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

/*   --------------------
    |       LOSER        |
    |     ~NOOBOSS~      |
     --------------------
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define inl(x) scanf("%lld", &x)
#define in(x) scanf("%d", &x)
#define ss(x) scanf("%s", x)
#define bitcount __builtin_popcountll
#define mxx LLONG_MAX
#define mnn LLONG_MIN
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define endl "\\n"
#define max_3(a, b, c) max(a, max(b, c))
#define max_4(a, b, c, d) max(a, max(b, max(c, d)))
#define min_3(a, b, c) min(a, min(b, c))
#define min_4(a, b, c, d) min(a, min(b, min(c, d)))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define loser return 0
#define ll long long
#define PI acos(-1)
#define mem(a, v) memset(a, v, sizeof(a))
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define O(x) cout << x << endl
#define B begin()
#define E end()
#define V vector
#define F first
#define S second
#define PSB push_back
#define EMB emplace_back
#define MP make_pair
#define PII pair<int, int>
#define MII map<int, int>
#define flash cout.flush()
#define InTheNameOfGod                                                         \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#ifndef ONLINE_JUDGE
#define dbg(a)                                                                 \\
  cerr << #a << ": ";                                                          \\
  _print(a);                                                                   \\
  cerr << endl;
#else
#define dbg(a) // No more TLEs
#endif
template <typename T>
using ordered_set = tree<
    T, null_type, less<T>, rb_tree_tag,
    tree_order_statistics_node_update>; //*(x.find_by_order(i)),x.order_of_key(k)
const ll MOD = 998244353;
constexpr ll mod = 1e9 + 7;
// auto it=std:: unique(v.B,v.E);
// v.resize(std::distance(v.B,it));
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
/*-----*/
#define bug1(a) cout << a << endl;
#define bug2(a, b) cout << a << " " << b << endl;
#define bug3(a, b, c) cout << a << " " << b << " " << c << endl;
/*----*/
const ll N = 3e5 + 7;
vector<ll> adj[N];
/*----*/
void normal(ll &a) {
  if (a >= MOD)
    a %= MOD;
  (a < 0) && (a += MOD);
}
ll modMul(ll a, ll b) {
  normal(a), normal(b);
  return (a * b) % MOD;
}
ll modAdd(ll a, ll b) {
  normal(a), normal(b);
  return (a + b) % MOD;
}
ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
} // if MOD is prime, modPow(b, p % (MOD-1))
ll modInverse(ll a) { return modPow(a, MOD - 2); }
/*-----*/
ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
ll power(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n;
  if (p % 2)
    return power(n, p - 1) * n;
  else {
    ll x = power(n, p / 2);
    return x * x;
  }
}
ll modpow(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
ll fact(ll num) {
  ll fac = 1;
  while (num != 1) {
    fac = fac * num;
    num--;
  }
  return fac;
}
string to_binary(int x) {
  string s;
  while (x > 0) {
    s += (x % 2 ? '1' : '0');
    x /= 2;
  }
  reverse(s.begin(), s.end());
  return s;
}
double quick_pow(double x, ll y) {
  if (!y)
    return 1.0;
  double ans = quick_pow(x, y >> 1);
  ans *= ans;
  if (y & 1)
    ans *= x;
  return ans;
}
ll nsum(ll num) { return (num * (num + 1)) / 2; }
void edge(ll u, ll v) {
  adj[u].PSB(v);
  adj[v].PSB(u);
}
/*-----*/
// exit(0); //fully exit the program.
// return for void // exit from that function.
// return 0 for ll or int // exit from that function.
map<pair<ll, ll>, ll> mp;
ll row[N], col[N];
/*-----*/
void solve() {
  ll n, h, w, m, a, b;
  string s;
  ll mxr = 0, mxc = 0;
  ll kr, kc;
  cin >> h >> w >> m;
  ll ans = 0, bal;
  for (ll i = 0; i < m; i++) {
    ll ck = 0;
    cin >> a >> b;
    mp[MP(a, b)]++;
    row[a]++;
    col[b]++;
    if (row[a] >= mxr) {
      mxr = row[a];
      kr = a;
      bal = mxr + mxc;
      if (mp[MP(kr, kc)])
        bal--;
      ans = max(ans, bal);
    }
    if (col[b] >= mxc) {
      mxc = col[b];
      kc = b;
      bal = mxr + mxc;
      if (mp[MP(kr, kc)])
        bal--;
      ans = max(ans, bal);
    }
  }
  V<ll> v1, v2;
  for (ll i = 1; i <= h; i++) {
    if (row[i] == mxr)
      v1.PSB(i);
  }
  for (ll i = 1; i <= w; i++) {
    if (col[i] == mxc)
      v2.PSB(i);
  }
  // SORT(v1); REV(v1);
  // SORT(v2); REV(v2);
  ll mx = mxr + mxc;
  // bug1(mx);
  for (ll i = 0; i < v1.size(); i++) {
    for (ll j = 0; j < v2.size(); j++) {
      if (mp[MP(v1[i], v2[j])] == 0) {
        // cout<<i<<' '<<j<<endl;
        cout << mx << endl;
        return;
      }
    }
  }
  cout << mx - 1 << endl;
  /* for(ll i=1;i<=h;i++){
    for(ll j=1;j<=w;j++){
        ll bal=row[i]+col[j];
        if(mp[MP(i,j)]) bal--;
        ans=max(ans,bal);

    }
   }*/
  // cout<<ans<<endl;
}
/*-----*/
int main() {
  InTheNameOfGod

      ll Test = 1;
  // cin>>Test;
  while (Test--) {
    solve();
  }
  loser;
}
///////LeArN & PrAcTiCe MoRe AnD Wait FoR ThE Result///////


Error: TLE

Fixed submission:

/*   --------------------
    |       LOSER        |
    |     ~NOOBOSS~      |
     --------------------
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define inl(x) scanf("%lld", &x)
#define in(x) scanf("%d", &x)
#define ss(x) scanf("%s", x)
#define bitcount __builtin_popcountll
#define mxx LLONG_MAX
#define mnn LLONG_MIN
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define endl "\\n"
#define max_3(a, b, c) max(a, max(b, c))
#define max_4(a, b, c, d) max(a, max(b, max(c, d)))
#define min_3(a, b, c) min(a, min(b, c))
#define min_4(a, b, c, d) min(a, min(b, min(c, d)))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define loser return 0
#define ll long long
#define PI acos(-1)
#define mem(a, v) memset(a, v, sizeof(a))
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define O(x) cout << x << endl
#define B begin()
#define E end()
#define V vector
#define F first
#define S second
#define PSB push_back
#define EMB emplace_back
#define MP make_pair
#define PII pair<int, int>
#define MII map<int, int>
#define flash cout.flush()
#define InTheNameOfGod                                                         \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#ifndef ONLINE_JUDGE
#define dbg(a)                                                                 \\
  cerr << #a << ": ";                                                          \\
  _print(a);                                                                   \\
  cerr << endl;
#else
#define dbg(a) // No more TLEs
#endif
template <typename T>
using ordered_set = tree<
    T, null_type, less<T>, rb_tree_tag,
    tree_order_statistics_node_update>; //*(x.find_by_order(i)),x.order_of_key(k)
const ll MOD = 998244353;
constexpr ll mod = 1e9 + 7;
// auto it=std:: unique(v.B,v.E);
// v.resize(std::distance(v.B,it));
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
/*-----*/
#define bug1(a) cout << a << endl;
#define bug2(a, b) cout << a << " " << b << endl;
#define bug3(a, b, c) cout << a << " " << b << " " << c << endl;
/*----*/
const ll N = 3e5 + 7;
vector<ll> adj[N];
/*----*/
void normal(ll &a) {
  if (a >= MOD)
    a %= MOD;
  (a < 0) && (a += MOD);
}
ll modMul(ll a, ll b) {
  normal(a), normal(b);
  return (a * b) % MOD;
}
ll modAdd(ll a, ll b) {
  normal(a), normal(b);
  return (a + b) % MOD;
}
ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
} // if MOD is prime, modPow(b, p % (MOD-1))
ll modInverse(ll a) { return modPow(a, MOD - 2); }
/*-----*/
ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
ll power(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n;
  if (p % 2)
    return power(n, p - 1) * n;
  else {
    ll x = power(n, p / 2);
    return x * x;
  }
}
ll modpow(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
ll fact(ll num) {
  ll fac = 1;
  while (num != 1) {
    fac = fac * num;
    num--;
  }
  return fac;
}
string to_binary(int x) {
  string s;
  while (x > 0) {
    s += (x % 2 ? '1' : '0');
    x /= 2;
  }
  reverse(s.begin(), s.end());
  return s;
}
double quick_pow(double x, ll y) {
  if (!y)
    return 1.0;
  double ans = quick_pow(x, y >> 1);
  ans *= ans;
  if (y & 1)
    ans *= x;
  return ans;
}
ll nsum(ll num) { return (num * (num + 1)) / 2; }
void edge(ll u, ll v) {
  adj[u].PSB(v);
  adj[v].PSB(u);
}
/*-----*/
// exit(0); //fully exit the program.
// return for void // exit from that function.
// return 0 for ll or int // exit from that function.
map<pair<ll, ll>, ll> mp;
ll row[N], col[N];
/*-----*/
void solve() {
  ll n, h, w, m, a, b;
  string s;
  cin >> h >> w >> m;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    mp[MP(a, b)]++;
    row[a]++;
    col[b]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      ll bal = row[i] + col[j];
      if (mp[MP(i, j)])
        bal--;
      ans = max(ans, bal);
    }
  }
  cout << ans << endl;
}
/*-----*/
int main() {
  InTheNameOfGod

      ll Test = 1;
  // cin>>Test;
  while (Test--) {
    solve();
  }
  loser;
}
///////LeArN & PrAcTiCe MoRe AnD Wait FoR ThE Result///////