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

/*
 Author: Vedang Karwa
 */

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define fr(i, j, n) for (lli i = j; i < (n); ++i)
#define bk(i, j, n) for (lli i = j; i >= n && j >= n; --i)
#define pb push_back
#define F first
#define S second
#define endl "\\n"
#define mem(p, v) memset(p, v, sizeof(p))
#define present(c, x) ((c).find(x) != (c).end())
#define run                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
// #define ordered_set tree<lli, null_type,less<lli>,
// rb_tree_tag,tree_order_statistics_node_update>
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << endl;
#define debug4(x, y, z, a)                                                     \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << " " << #a << " " << a << endl;
#define ub upper_bound
#define lb lower_bound
typedef long long int lli;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<lli> vl;
typedef pair<lli, lli> pl;
inline lli max(lli a, lli b) { return a > b ? a : b; }
inline lli min(lli a, lli b) { return a < b ? a : b; }
const long long inf = 1e18 + 5;
const lli M = 1e9 + 7;
void show(vector<lli> v) {
  fr(i, 0, v.size()) cout << v[i] << " ";
  cout << endl;
}
void show(vector<int> v) {
  fr(i, 0, v.size()) cout << v[i] << " ";
  cout << endl;
}
void show(int a[], int arraysize) {
  for (int i = 0; i < arraysize; ++i)
    cout << a[i] << " ";
  cout << endl;
}
void show(lli a[], lli arraysize) {
  for (lli i = 0; i < arraysize; ++i)
    cout << a[i] << " ";
  cout << endl;
}
void show(bool a[], lli arraysize) {
  for (lli i = 0; i < arraysize; ++i)
    cout << a[i] << " ";
  cout << endl;
}
void show(set<int> s) {
  for (auto it = s.begin(); it != s.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
}
void show(map<lli, lli> mp) {
  for (auto it = mp.begin(); it != mp.end(); it++) {
    cout << it->first << " " << it->second << endl;
  }
  cout << endl;
}
lli modpow(lli x, lli n) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = ((res % M) * (x % M)) % M;
    x = ((x % M) * (x % M)) % M;
    n >>= 1;
  }
  return res;
}
lli power(lli x, lli n) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
}
const int N = 1e6 + 5;
int r[4] = {-1, 1, 0, 0};
int c[4] = {0, 0, -1, 1};
/////// ------ Main code starts here ------ /////
// bool visited[N];
lli dp[N];
int g[1001][1001];
int n, m;
int xs, ys, xd, yd;
int util(int i, int j) { return m * i + j; }
bool valid(int i, int j) {
  if (i >= 0 && i < n && j >= 0 && j < m && g[i][j])
    return 1;
  return 0;
}
void dfs(int s) {
  // debug4(s, a,b,dp[s]);
  multiset<pair<lli, lli>> set;
  set.insert({0, s});
  while (set.size()) {
    pl p = *set.begin();
    set.erase(set.begin());
    s = p.S;
    lli d = p.F;
    if (dp[s] < d)
      continue;
    int a = s / m, b = s % m, t;
    // debug4(s, dp[s], a,b);
    fr(i, 0, 4) {
      int x = a + r[i], y = b + c[i];
      if (valid(x, y)) {
        t = util(x, y);
        if (dp[t] > dp[s]) {
          dp[t] = dp[s];
          set.insert({dp[t], t});
        }
      }
    }
    // debug2(set.size(), s);
    fr(i, a - 2, a + 3) {
      fr(j, b - 2, b + 3) {
        t = util(i, j);
        if (valid(i, j)) {
          if (dp[t] > dp[s] + 1) {
            dp[t] = dp[s] + 1;
            set.insert({dp[t], t});
          }
        }
      }
    }
  }
}
void solve() {
  lli h, w;
  cin >> h >> w >> m;
  vl a(h, 0), b(w, 0);
  set<pl> s;
  fr(i, 0, m) {
    lli x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    s.insert({x, y});
  }
  lli m1 = 0, m2 = 0, mi1, mi2;
  vl d, e;
  fr(i, 0, h) {
    if (m1 < a[i]) {
      m1 = a[i];
      mi1 = i;
    }
  }
  fr(i, 0, h) {
    if (a[i] == m1) {
      d.pb(i);
    }
  }
  fr(i, 0, w) {
    if (m2 < b[i]) {
      m2 = b[i];
      mi2 = i;
    }
  }
  fr(i, 0, w) {
    if (b[i] == m2) {
      e.pb(i);
    }
  }
  // debug4(m1, m2, mi1, mi2);
  lli ans = 0;
  fr(i, 0, d.size()) {
    fr(j, 0, e.size()) {
      lli t = a[d[i]] + b[e[j]];
      if (s.find({d[i], e[j]}) != s.end()) {
        t--;
        ans = max(ans, t);
      } else {
        ans = max(ans, t);
        break;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  run;
  cout << fixed << setprecision(0);
  int t;
  t = 1;
  while (t) {
    solve();
    t--;
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
 Author: Vedang Karwa
 */

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define fr(i, j, n) for (lli i = j; i < (n); ++i)
#define bk(i, j, n) for (lli i = j; i >= n && j >= n; --i)
#define pb push_back
#define F first
#define S second
#define endl "\\n"
#define mem(p, v) memset(p, v, sizeof(p))
#define present(c, x) ((c).find(x) != (c).end())
#define run                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
// #define ordered_set tree<lli, null_type,less<lli>,
// rb_tree_tag,tree_order_statistics_node_update>
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << endl;
#define debug4(x, y, z, a)                                                     \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << " " << #a << " " << a << endl;
#define ub upper_bound
#define lb lower_bound
typedef long long int lli;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<lli> vl;
typedef pair<lli, lli> pl;
inline lli max(lli a, lli b) { return a > b ? a : b; }
inline lli min(lli a, lli b) { return a < b ? a : b; }
const long long inf = 1e18 + 5;
const lli M = 1e9 + 7;
void show(vector<lli> v) {
  fr(i, 0, v.size()) cout << v[i] << " ";
  cout << endl;
}
void show(vector<int> v) {
  fr(i, 0, v.size()) cout << v[i] << " ";
  cout << endl;
}
void show(int a[], int arraysize) {
  for (int i = 0; i < arraysize; ++i)
    cout << a[i] << " ";
  cout << endl;
}
void show(lli a[], lli arraysize) {
  for (lli i = 0; i < arraysize; ++i)
    cout << a[i] << " ";
  cout << endl;
}
void show(bool a[], lli arraysize) {
  for (lli i = 0; i < arraysize; ++i)
    cout << a[i] << " ";
  cout << endl;
}
void show(set<int> s) {
  for (auto it = s.begin(); it != s.end(); it++) {
    cout << *it << " ";
  }
  cout << endl;
}
void show(map<lli, lli> mp) {
  for (auto it = mp.begin(); it != mp.end(); it++) {
    cout << it->first << " " << it->second << endl;
  }
  cout << endl;
}
lli modpow(lli x, lli n) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = ((res % M) * (x % M)) % M;
    x = ((x % M) * (x % M)) % M;
    n >>= 1;
  }
  return res;
}
lli power(lli x, lli n) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
}
const int N = 1e6 + 5;
int r[4] = {-1, 1, 0, 0};
int c[4] = {0, 0, -1, 1};
/////// ------ Main code starts here ------ /////
// bool visited[N];
lli dp[N];
int g[1001][1001];
int n, m;
int xs, ys, xd, yd;
int util(int i, int j) { return m * i + j; }
bool valid(int i, int j) {
  if (i >= 0 && i < n && j >= 0 && j < m && g[i][j])
    return 1;
  return 0;
}
void dfs(int s) {
  // debug4(s, a,b,dp[s]);
  multiset<pair<lli, lli>> set;
  set.insert({0, s});
  while (set.size()) {
    pl p = *set.begin();
    set.erase(set.begin());
    s = p.S;
    lli d = p.F;
    if (dp[s] < d)
      continue;
    int a = s / m, b = s % m, t;
    // debug4(s, dp[s], a,b);
    fr(i, 0, 4) {
      int x = a + r[i], y = b + c[i];
      if (valid(x, y)) {
        t = util(x, y);
        if (dp[t] > dp[s]) {
          dp[t] = dp[s];
          set.insert({dp[t], t});
        }
      }
    }
    // debug2(set.size(), s);
    fr(i, a - 2, a + 3) {
      fr(j, b - 2, b + 3) {
        t = util(i, j);
        if (valid(i, j)) {
          if (dp[t] > dp[s] + 1) {
            dp[t] = dp[s] + 1;
            set.insert({dp[t], t});
          }
        }
      }
    }
  }
}
void solve() {
  lli h, w;
  cin >> h >> w >> m;
  vl a(h, 0), b(w, 0);
  set<pl> s;
  fr(i, 0, m) {
    lli x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    s.insert({x, y});
  }
  lli m1 = 0, m2 = 0, mi1, mi2;
  vl d, e;
  fr(i, 0, h) {
    if (m1 < a[i]) {
      m1 = a[i];
      mi1 = i;
    }
  }
  fr(i, 0, h) {
    if (a[i] == m1) {
      d.pb(i);
    }
  }
  fr(i, 0, w) {
    if (m2 < b[i]) {
      m2 = b[i];
      mi2 = i;
    }
  }
  fr(i, 0, w) {
    if (b[i] == m2) {
      e.pb(i);
    }
  }
  // debug4(m1, m2, mi1, mi2);
  lli ans = 0;
  fr(i, 0, d.size()) {
    fr(j, 0, e.size()) {
      lli t = a[d[i]] + b[e[j]];
      if (s.find({d[i], e[j]}) != s.end()) {
        t--;
      }
      ans = max(ans, t);
    }
  }
  cout << ans << endl;
}
int main() {
  run;
  cout << fixed << setprecision(0);
  int t;
  t = 1;
  while (t) {
    solve();
    t--;
  }
  return 0;
}