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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N = 3e5 + 10;
const int mod = 1e9 + 7;
int a[N], b[N];
ll maxs = -9999;
ll sum;
struct A {
  int x, y;
};
map<pair<int, int>, int> mp;
int main() {
  int h, w, m;
  int q[N], t[N]; // 记录最多障碍物的行和列。
  int cnt1, cnt2; // 记录有多少条最多的行和列。
  cin >> h >> w >> m;
  int sum1 = 0, sum2 = 0; // 求最大的行和列。
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x, y)]++;
    a[x]++;
    b[y]++;
  }
  for (int i = 1; i <= h; i++) {
    if (sum1 < a[i]) {
      cnt1 = 0;
      memset(q, 0, sizeof(q));
      sum1 = max(sum1, a[i]);
      q[cnt1++] = i;
      while (sum1 == a[i + 1]) {
        q[cnt1++] = i;
        i++;
      }
    }
    if (sum1 == a[i]) {
      q[cnt1++] = i;
      while (sum1 == a[i + 1]) {
        q[cnt1++] = i;
        i++;
      }
    }
  }
  for (int i = 1; i <= w; i++) {
    if (sum2 < b[i]) {
      cnt2 = 0;
      memset(t, 0, sizeof(t));
      sum2 = max(sum2, b[i]);
      t[cnt2++] = i;
      while (sum2 == b[i + 1]) {
        t[cnt2++] = i;
        i++;
      }
    }
    if (sum2 == b[i]) {
      t[cnt2++] = i;
      while (sum2 == b[i + 1]) {
        t[cnt2++] = i;
        i++;
      }
    }
  }
  sum = sum1 + sum2;
  int xx = 0;
  for (int i = 0; i < cnt1; i++) {
    for (int j = 0; j < cnt2; j++) {
      int x1, y1;
      x1 = q[i];
      y1 = t[j];
      if (mp[make_pair(x1, y1)] == 0) // 判断是否交点有障碍物
      {
        xx = 1;
        break;
      }
    }
    if (xx == 1) {
      break;
    }
  }
  if (xx == 0) {
    sum--;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*痛みを受け取れ，痛みを知れ*/
#include <bits/stdc++.h>
#define f(i, a, b) for (register int i = a; i <= b; ++i)
#define ff(i, a, b) for (register int i = a; i >= b; --i)
#define debug(x) cerr << #x << " : " << x << " " << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double tiaohe = 0.57721566490153286060651209;
ll oula(ll x) {
  ll res = x;
  f(i, 2, x / i) {
    if (x % i == 0) {
      res = res / i * (i - 1);
      while (x % i == 0)
        x /= i;
    }
  }
  if (x > 1)
    res = res / x * (x - 1);
  return res;
}
ll quickmod(ll a, ll n, ll m) {
  ll s = 1;
  while (n) {
    if (n & 1) {
      s = s * a % m;
    }
    a = (a * a) % m;
    n = n / 2;
  }
  return s;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) {
  if (!b) {
    d = a, x = 1, y = 0;
  } else {
    ex_gcd(b, a % b, y, x, d);
    y -= x * (a / b);
  }
}
ll inv(ll t, ll p) {
  ll d, x, y;
  ex_gcd(t, p, x, y, d);
  return d == 1 ? (x % p + p) % p : -1;
}
bool isPrime(ll x) {
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  for (ll i = 2; i * i <= x; i++)
    if (x % i == 0)
      return false;
  return true;
}
// double a = log(n) +tiaohe + 1.0 / (2 * n);
double eqa = (1 + sqrt(5.0)) / 2.0;
const int N = 3e5 + 5;
inline int in() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
} // 快读
int r[N], c[N];
int main() {
  // freopen("in.txt", "r", stdin);
  int h, w, m;
  while (cin >> h >> w >> m) {
    int x, y;
    map<pii, int> mp;
    f(i, 1, m) {
      x = in();
      y = in();
      r[x]++;
      c[y]++;
      mp[{x, y}]++;
    }
    int mx = 0;
    vector<int> v;
    f(i, 1, h) {
      if (r[i] > mx) {
        v.clear();
        mx = r[i];
        v.emplace_back(i);
      } else if (r[i] == mx) {
        v.emplace_back(i);
      }
    }
    int mx2 = 0;
    vector<int> v2;
    f(j, 1, w) {
      if (c[j] > mx2) {
        mx2 = c[j];
        v2.clear();
        v2.emplace_back(j);
      } else if (c[j] == mx2) {
        v2.emplace_back(j);
      }
    }
    int ans = 0;
    for (auto i : v) {
      for (auto j : v2) {
        ans = max(ans, mx + mx2 - mp[{i, j}]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}