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

/*
 *      ┌─┐       ┌─┐ + +
 *   ┌──┘ ┴───────┘ ┴──┐++
 *   │                 │
 *   │       ───       │++ + + +
 *   ███████───███████ │+
 *   │                 │+
 *   │       ─┴─       │
 *   │                 │
 *   └───┐         ┌───┘
 *       │         │
 *       │         │   + +
 *       │         │       Code is far away from bug with the animal protecting
 *       │         └──────────────┐
 *       │                        │
 *       │                        ├─┐
 *       │                        ┌─┘
 *       │                        │
 *       └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 *         │ ─┤ ─┤       │ ─┤ ─┤
 *         └──┴──┘       └──┴──┘  + + + +
 *                神兽保佑
 *               代码无BUG!
 */

#include <bits/stdc++.h>
using namespace std;
class IN {
public:
  template <typename T> IN &operator>>(T &x) {
    read(x);
    return *this;
  }
  IN &operator>>(double &x) {
    scanf("%lf", &x);
    return *this;
  }
  IN &operator>>(char *s) {
    scanf("%s", s);
    return *this;
  }
  IN &operator>>(char &c) {
    c = getchar();
    return *this;
  }

private:
  template <typename T> void read(T &x) {
    T f = 1;
    int c = getchar();
    x = 0;
    while (c < '0' || c > '9') {
      if (c == '-')
        f = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9')
      x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    x *= f;
  }
} in;
class NL {
} nl;
class BK {
} bk;
class OUT {
public:
  template <typename T> OUT &operator<<(T x) {
    write(x);
    return *this;
  }
  OUT &operator<<(const char *s) {
    printf("%s", s);
    return *this;
  }
  OUT &operator<<(char c) {
    putchar(c);
    return *this;
  }
  OUT &operator<<(BK x) {
    putchar(' ');
    return *this;
  }
  OUT &operator<<(NL x) {
    putchar('\\n');
    return *this;
  }

private:
  template <typename T> void write(T x) {
    if (x < 0)
      putchar('-'), x = -x;
    if (x >= 10)
      write(x / 10);
    putchar(x % 10 + 48);
  }
} out;

#define pb(x) push_back(x)
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define mem(a, x) memset(a, x, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;
// Smax
// int Smax() { return -INF; }
template <typename T> T Smax(T x) { return x; }
template <typename T, typename... Args> T Smax(T a, Args... args) {
  return max(a, Smax(args...));
}
// Smin
template <typename T> T Smin(T x) { return x; }
template <typename T, typename... Args> T Smin(T a, Args... args) {
  return min(a, Smin(args...));
}

/*****************************************************************************************************/
ll Pow(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res % mod;
}
ll inv(ll a, ll p) {
  if (a == 1)
    return 1;
  return (p - p / a) * inv(p % a, p) % p;
} // p为素数,a<p
ll C(ll n, ll m, ll p) {
  ll res = 1;
  for (ll i = 1, j = n - m + 1; i <= m; ++i, ++j) {
    res = res * inv(i, p) % p * j % p;
  }
  return res;
}
/*****************************************************************************************************/
/*ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&-x;return res;}
void add(ll v,int x){while(x<=n)c[x]+=v,x+=x&-x;}*/
const int p = 1e9 + 7;
const int N = 3e5 + 10;

PI a[N], b[N];
map<PI, int> mp;
int main() {
  int h, w, m;
  in >> h >> w >> m;
  rep(i, 1, h) a[i].se = i;
  rep(i, 1, w) b[i].se = i;

  rep(i, 1, m) {
    int x, y;
    in >> x >> y;
    ++a[x].fi;
    ++b[y].fi;
    mp[PI(x, y)]++;
  }
  sort(a + 1, a + 1 + h);
  sort(b + 1, b + 1 + w);

  int ans = 0;
  int ma = a[h].fi;
  int mb = b[w].fi;

  int cnt = 0;
  per(i, h, 1) {
    if (a[i].fi != ma)
      break;
    per(j, w, 1) {
      if (b[j].fi != mb)
        break;
      ans = max(ans, ma + mb - mp[PI(a[i].se, b[j].se)]);
      if (++cnt > 2000000)
        break;
    }
    if (cnt > 2000000)
      break;
  }
  out << ans;
  return 0;
}


Error: TLE

Fixed submission:

/*
 *      ┌─┐       ┌─┐ + +
 *   ┌──┘ ┴───────┘ ┴──┐++
 *   │                 │
 *   │       ───       │++ + + +
 *   ███████───███████ │+
 *   │                 │+
 *   │       ─┴─       │
 *   │                 │
 *   └───┐         ┌───┘
 *       │         │
 *       │         │   + +
 *       │         │       Code is far away from bug with the animal protecting
 *       │         └──────────────┐
 *       │                        │
 *       │                        ├─┐
 *       │                        ┌─┘
 *       │                        │
 *       └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 *         │ ─┤ ─┤       │ ─┤ ─┤
 *         └──┴──┘       └──┴──┘  + + + +
 *                神兽保佑
 *               代码无BUG!
 */

#include <bits/stdc++.h>
using namespace std;
class IN {
public:
  template <typename T> IN &operator>>(T &x) {
    read(x);
    return *this;
  }
  IN &operator>>(double &x) {
    scanf("%lf", &x);
    return *this;
  }
  IN &operator>>(char *s) {
    scanf("%s", s);
    return *this;
  }
  IN &operator>>(char &c) {
    c = getchar();
    return *this;
  }

private:
  template <typename T> void read(T &x) {
    T f = 1;
    int c = getchar();
    x = 0;
    while (c < '0' || c > '9') {
      if (c == '-')
        f = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9')
      x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    x *= f;
  }
} in;
class NL {
} nl;
class BK {
} bk;
class OUT {
public:
  template <typename T> OUT &operator<<(T x) {
    write(x);
    return *this;
  }
  OUT &operator<<(const char *s) {
    printf("%s", s);
    return *this;
  }
  OUT &operator<<(char c) {
    putchar(c);
    return *this;
  }
  OUT &operator<<(BK x) {
    putchar(' ');
    return *this;
  }
  OUT &operator<<(NL x) {
    putchar('\\n');
    return *this;
  }

private:
  template <typename T> void write(T x) {
    if (x < 0)
      putchar('-'), x = -x;
    if (x >= 10)
      write(x / 10);
    putchar(x % 10 + 48);
  }
} out;

#define pb(x) push_back(x)
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define mem(a, x) memset(a, x, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;
// Smax
// int Smax() { return -INF; }
template <typename T> T Smax(T x) { return x; }
template <typename T, typename... Args> T Smax(T a, Args... args) {
  return max(a, Smax(args...));
}
// Smin
template <typename T> T Smin(T x) { return x; }
template <typename T, typename... Args> T Smin(T a, Args... args) {
  return min(a, Smin(args...));
}

/*****************************************************************************************************/
ll Pow(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res % mod;
}
ll inv(ll a, ll p) {
  if (a == 1)
    return 1;
  return (p - p / a) * inv(p % a, p) % p;
} // p为素数,a<p
ll C(ll n, ll m, ll p) {
  ll res = 1;
  for (ll i = 1, j = n - m + 1; i <= m; ++i, ++j) {
    res = res * inv(i, p) % p * j % p;
  }
  return res;
}
/*****************************************************************************************************/
/*ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&-x;return res;}
void add(ll v,int x){while(x<=n)c[x]+=v,x+=x&-x;}*/
const int p = 1e9 + 7;
const int N = 3e5 + 10;

PI a[N], b[N];
map<PI, int> mp;
int main() {
  int h, w, m;
  in >> h >> w >> m;
  rep(i, 1, h) a[i].se = i;
  rep(i, 1, w) b[i].se = i;

  rep(i, 1, m) {
    int x, y;
    in >> x >> y;
    ++a[x].fi;
    ++b[y].fi;
    mp[PI(x, y)]++;
  }
  sort(a + 1, a + 1 + h);
  sort(b + 1, b + 1 + w);

  int ans = 0;
  int ma = a[h].fi;
  int mb = b[w].fi;

  per(i, h, 1) {
    if (a[i].fi != ma)
      break;
    per(j, w, 1) {
      if (b[j].fi != mb)
        break;
      ans = max(ans, ma + mb - mp[PI(a[i].se, b[j].se)]);
    }
  }
  out << ans;
  return 0;
}