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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;

//      DEEP

using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define pb push_back
#define endl '\\n'
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef set<int> si;

/*
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
*/
/*
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
*/

const ll mod = 1e9 + 7;
const ll modl = 998244353;
const double pie = 3.1415926535;
// power
ull power(ull x, ull y) {
  if (y == 0)
    return 1;
  else {
    if (y % 2 == 0)
      return power(x * x, y / 2);
    else
      return x * power(x * x, (y - 1) / 2);
  }
}

//                    Modular Exponentiation

ll mod_power(ll x, ll y, ll m) {
  ll r = 1; // Initialize result

  x = x % m; // Update x if it is more than or equal to m

  if (x == 0)
    return 0; // In case x is divisible by m;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      r = (r * x) % m;

    // y must be even now
    y = y / 2;
    x = (x * x) % m;
  }
  return r;
}

// GCD
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
// SIEVE

/*
bool isPrime[100001];
void sieve(int N) {

        for(int i = 0; i <= N;++i) {
            isPrime[i]=true;
        }
        isPrime[0]=false;
        isPrime[1]=false;

        for(int i = 2; i * i <= N; ++i) {
             if(isPrime[i] == true) {
                 for(int j = i * i; j <= N ;j += i)
                     isPrime[j] = false;
            }
        }
    }
*/

int main() {
  boost;
  int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    int h, w, m;
    cin >> h >> w >> m;
    map<int, int> r;
    map<int, int> c;
    set<pi> s;
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      r[x]++;
      c[y]++;
      s.insert(mp(x, y));
    }
    int ans = 0;
    auto it1 = r.begin();
    int sum1 = 0;
    set<int> s1;
    set<int> s2;
    for (it1; it1 != r.end(); it1++) {
      if (it1->second > sum1) {
        sum1 = it1->second;
      }
    }
    it1 = r.begin();
    for (it1; it1 != r.end(); it1++) {
      if (it1->second == sum1) {
        s1.insert(it1->first);
      }
    }

    auto it2 = c.begin();
    int sum2 = 0;
    for (it2; it2 != c.end(); it2++) {
      if (it2->second > sum2) {
        sum2 = it2->second;
      }
    }
    it2 = c.begin();
    for (it2; it2 != c.end(); it2++) {
      if (it2->second == sum2) {
        s2.insert(it2->first);
      }
    }
    // int ans=0;
    auto ip1 = s1.begin();
    bool ok = false;
    for (ip1; ip1 != s1.end(); ip1++) {
      auto ip2 = s2.begin();
      for (ip2; ip2 != s2.end(); ip2++) {
        if (s.find(mp(*ip1, *ip2)) == s.end()) {
          ans = sum1 + sum2;
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
    if (!ok)
      ans = sum1 + sum2 - 1;
    cout << ans << endl;

    // cout<<sum1+sum2-1<<endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;

//      DEEP

using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define pb push_back
#define endl '\\n'
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef set<int> si;

/*
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
*/
/*
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
*/

const ll mod = 1e9 + 7;
const ll modl = 998244353;
const double pie = 3.1415926535;
// power
ull power(ull x, ull y) {
  if (y == 0)
    return 1;
  else {
    if (y % 2 == 0)
      return power(x * x, y / 2);
    else
      return x * power(x * x, (y - 1) / 2);
  }
}

//                    Modular Exponentiation

ll mod_power(ll x, ll y, ll m) {
  ll r = 1; // Initialize result

  x = x % m; // Update x if it is more than or equal to m

  if (x == 0)
    return 0; // In case x is divisible by m;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      r = (r * x) % m;

    // y must be even now
    y = y / 2;
    x = (x * x) % m;
  }
  return r;
}

// GCD
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
// SIEVE

/*
bool isPrime[100001];
void sieve(int N) {

        for(int i = 0; i <= N;++i) {
            isPrime[i]=true;
        }
        isPrime[0]=false;
        isPrime[1]=false;

        for(int i = 2; i * i <= N; ++i) {
             if(isPrime[i] == true) {
                 for(int j = i * i; j <= N ;j += i)
                     isPrime[j] = false;
            }
        }
    }
*/

int main() {
  boost;
  int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    int h, w, m;
    cin >> h >> w >> m;
    map<int, int> r;
    map<int, int> c;
    set<pi> s;
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      r[x]++;
      c[y]++;
      s.insert(mp(x, y));
    }
    int ans = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        int sum = r[i] + c[j];
        if (s.find(mp(i, j)) != s.end())
          sum--;
        ans = max(sum, ans);
      }
    }
    cout << ans << endl;
  }
}