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

/*input
2 3 3
2 2
1 1
1 3
*/

#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vpll vector<pair<long long, long long>>
#define fill(a, val) memset(a, val, sizeof(a))
#define sort_unique(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define mp make_pair
#define pb push_back
#define f first
#define se second
#define all(cont) cont.begin(), cont.end()
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define INTMAX 2147483647
#define INTMIN -2147483648
#define LLMAX 9223372036854775807
#define LLMIN -9223372036854775808
#define MOD 1000000007
typedef map<long long int, long long int> mpi;
typedef set<long long int> seti;
typedef multiset<long long int> mseti;
typedef priority_queue<long long int> pq;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, n) for (long long int i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define repn(i, begin, end)                                                    \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define PI 3.1415926535
#define dbg(x) cerr << "\\n" << (#x) << " = " << (x) << endl;
// #define cerr if(false)cerr

// cout<<fixed<<setprecision(10)<<(v1+v2+v3)<<endl;

struct node {
  ll val;
};

long long int minimum = 100000000000000;

int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

struct ct_s_h {
  long long int cts, cth, index;
};

bool compare(long long int a, long long int b) {
  if (gcd(a, b) < minimum)
    return 1;
  else
    return 0;
}

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> row, col;
  map<pair<ll, ll>, ll> presence;
  for (int i = 0; i < m; i++) {
    ll r, c;
    cin >> r >> c;
    presence[mp(r, c)] = 1;
    row[r]++;
    col[c]++;
  }
  map<ll, ll>::iterator ir, ic;
  long long int ans = -1;
  long long int rmax = -1;
  vector<long long int> rownums;
  for (ir = row.begin(); ir != row.end(); ++ir) {
    if (ir->second > rmax) {
      rmax = max(rmax, ir->second);
    }
  }
  for (ir = row.begin(); ir != row.end(); ++ir) {
    if (ir->second == rmax) {
      rownums.pb(ir->first);
    }
  }
  long long int cmax = -1;
  vector<long long int> colnums;
  for (ic = col.begin(); ic != col.end(); ++ic) {
    if (ic->second > cmax) {
      cmax = max(cmax, ic->second);
    }
  }
  for (ic = col.begin(); ic != col.end(); ++ic) {
    if (ic->second == cmax) {
      colnums.pb(ic->first);
    }
  }

  ans = rmax + cmax - 1;
  for (int i = 0; i < rownums.size(); i++) {
    for (int j = 0; j < colnums.size(); j++) {
      if (presence[mp(rownums[i], colnums[j])] != 1) {
        ans = rmax + cmax;
        break;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*input
5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4
*/

#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vpll vector<pair<long long, long long>>
#define fill(a, val) memset(a, val, sizeof(a))
#define sort_unique(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define mp make_pair
#define pb push_back
#define f first
#define se second
#define all(cont) cont.begin(), cont.end()
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define INTMAX 2147483647
#define INTMIN -2147483648
#define LLMAX 9223372036854775807
#define LLMIN -9223372036854775808
#define MOD 1000000007
typedef map<long long int, long long int> mpi;
typedef set<long long int> seti;
typedef multiset<long long int> mseti;
typedef priority_queue<long long int> pq;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, n) for (long long int i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define repn(i, begin, end)                                                    \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define PI 3.1415926535
#define dbg(x) cerr << "\\n" << (#x) << " = " << (x) << endl;
// #define cerr if(false)cerr

// cout<<fixed<<setprecision(10)<<(v1+v2+v3)<<endl;

struct node {
  ll val;
};

long long int minimum = 100000000000000;

int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

struct ct_s_h {
  long long int cts, cth, index;
};

bool compare(long long int a, long long int b) {
  if (gcd(a, b) < minimum)
    return 1;
  else
    return 0;
}

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> row, col;
  map<pair<ll, ll>, ll> presence;
  for (int i = 0; i < m; i++) {
    ll r, c;
    cin >> r >> c;
    presence[mp(r, c)] = 1;
    row[r]++;
    col[c]++;
  }
  map<ll, ll>::iterator ir, ic;
  long long int ans = -1;
  for (ir = row.begin(); ir != row.end(); ++ir) {
    long long int this_itr = ir->second;
    for (ic = col.begin(); ic != col.end(); ic++) {
      long long int val;
      if (presence[mp(ir->first, ic->first)] == 1) {
        val = ic->second - 1 + this_itr;
      } else {
        val = ic->second + this_itr;
      }
      ans = max(val, ans);
    }
  }
  cout << ans << endl;
  return 0;
}