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

// Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... Args> void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
// #define int long long
#define double long double

int n, m, k;
int32_t main() {
  IOS;
  cin >> n >> m >> k;
  map<int, int> ct1, ct2;
  map<pair<int, int>, int> finmp;
  int arr[k][2];
  for (int i = 0; i < k; i++) {
    cin >> arr[i][0] >> arr[i][1];
    ct1[arr[i][0]]++;
    ct2[arr[i][1]]++;
    finmp[{arr[i][0], arr[i][1]}]++;
  }
  vector<pair<int, int>> v1, v2;
  set<pair<int, int>> s1, s2;
  for (auto i : ct1)
    s1.insert({i.second, i.first});
  for (auto i : ct2)
    s2.insert({i.second, i.first});

  for (auto i : s1)
    v1.push_back({i.second, i.first});
  for (auto i : s2)
    v2.push_back({i.second, i.first});

  int maxans = 0;
  int l1 = v1.size();
  int l2 = v2.size();

  for (int i = l1 - 1; i >= max(0, l1 - 20); i--)
    for (int j = l2 - 1; j >= 0; j--) {
      int ct1 = v1[i].second;
      int ct2 = v2[j].second;
      int ispre = 0;
      if (finmp.find({v1[i].first, v2[j].first}) != finmp.end())
        ispre = 1;
      maxans = max(maxans, ct1 + ct2 - ispre);
    }
  for (int j = l2 - 1; j >= max(0, l2 - 20); j--)
    for (int i = l1 - 1; i >= 0; i--) {
      int ct1 = v1[i].second;
      int ct2 = v2[j].second;
      int ispre = 0;
      if (finmp.find({v1[i].first, v2[j].first}) != finmp.end())
        ispre = 1;
      maxans = max(maxans, ct1 + ct2 - ispre);
    }
  print(maxans);
}


Error: TLE

Fixed submission:

// Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... Args> void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
// #define int long long
#define double long double

int n, m, k;
int32_t main() {
  IOS;
  cin >> n >> m >> k;
  map<int, int> ct1, ct2;
  map<pair<int, int>, int> finmp;
  int arr[k][2];
  for (int i = 0; i < k; i++) {
    cin >> arr[i][0] >> arr[i][1];
    ct1[arr[i][0]]++;
    ct2[arr[i][1]]++;
    finmp[{arr[i][0], arr[i][1]}]++;
  }
  vector<pair<int, int>> v1, v2;
  set<pair<int, int>> s1, s2;
  for (auto i : ct1)
    s1.insert({i.second, i.first});
  for (auto i : ct2)
    s2.insert({i.second, i.first});

  for (auto i : s1)
    v1.push_back({i.second, i.first});
  for (auto i : s2)
    v2.push_back({i.second, i.first});

  int maxans = 0;
  int l1 = v1.size();
  int l2 = v2.size();
  for (int i = l1 - 1; i >= 0; i--) {
    for (int j = l2 - 1; j >= 0; j--) {
      int ct1 = v1[i].second;
      int ct2 = v2[j].second;
      int ispre = 0;
      if (finmp.find({v1[i].first, v2[j].first}) != finmp.end())
        ispre = 1;
      int neww = ct1 + ct2 - ispre;
      maxans = max(maxans, ct1 + ct2 - ispre);
      if (maxans - neww >= 2)
        break;
    }
  }
  print(maxans);
}