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

// in case of multiple test cases and N=1e5 don't intialize arrays as global
// except for graph vector-array always check whether or not you are doing mod
// of a negative number always use 1LL instead of 1
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi unordered_map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
// just comment the line below in case of interactive problems
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 3e5 + 5;
const int MOD = 1e9 + 7;

int n, m, k;
int row[N];
int col[N];

int32_t main() {
  IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    cin >> n >> m >> k;
    int maxr = 0;
    int maxc = 0;
    mii mp;
    rep(i, 1, k) {
      int x, y;
      cin >> x >> y;
      row[x]++;
      col[y]++;
      maxr = max(maxr, row[x]);
      maxc = max(maxc, col[y]);
      mp[{x, y}] = 1;
    }
    vi all_rows;
    vi all_cols;
    rep(i, 1, n) {
      if (row[i] == maxr)
        all_rows.pb(i);
    }
    rep(i, 1, m) {
      if (col[i] == maxc)
        all_cols.pb(i);
    }
    for (int x : all_rows) {
      for (int y : all_cols) {
        if (mp[{x, y}] == 0) {
          cout << maxr + maxc;
          return 0;
        }
      }
    }
    cout << maxr + maxc - 1;
  }
}

Error: TLE

Fixed submission:

// in case of multiple test cases and N=1e5 don't intialize arrays as global
// except for graph vector-array always check whether or not you are doing mod
// of a negative number always use 1LL instead of 1
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi unordered_map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
// just comment the line below in case of interactive problems
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 3e5 + 5;
const int MOD = 1e9 + 7;

int h, w, n;

int func(int x, vii v) {
  mi mp;
  int ans = 0;
  for (auto it : v) {
    if (it.F == x) {
      ans++;
      continue;
    }
    mp[it.S]++;
  }
  int maxx = 0;
  for (auto it : mp)
    maxx = max(maxx, it.S);
  return ans + maxx;
}

int solve(vii v) {
  mi mp;
  int maxx = -1;
  rep(i, 0, v.size() - 1) {
    mp[v[i].F]++;
    maxx = max(maxx, mp[v[i].F]);
  }
  vi cur;
  for (auto it : mp) {
    if (it.S == maxx)
      cur.pb(it.F);
  }
  int ans = 0;
  for (int x : cur) {
    ans = max(ans, func(x, v));
  }
  return ans;
}

int32_t main() {
  IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    cin >> h >> w >> n;
    vii v;
    rep(i, 1, n) {
      int x, y;
      cin >> x >> y;
      v.pb({x, y});
    }
    int ans = solve(v);
    for (int i = 0; i < n; i++)
      swap(v[i].F, v[i].S);
    ans = max(ans, solve(v));
    cout << ans;
  }
}