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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define loop(i, a, b) for (int i = a; i < b; i++)
#define scan(arr, n)                                                           \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair<int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map<int, int>
#define pb push_back
#define faster                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
// Constants
const int MOD = 1000000007; /* 1e9 + 7*/
const int MAXN = 1000005;   /*1e6 +5 */

int spf[1000009]; // stores the smallest prime factor of a number

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++)
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
}

/* Iterative Function to calculate (x^y)%p in O(log y) */
int power(int x, int y, int p) {
  int res = 1; // Initialize result
  x = x % p;   // Update x if it is more than or
               // equal to p
  if (x == 0)
    return 0; // In case x is divisible by p;
  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;
    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  mii mp, mp1;
  // int cp=m;
  vi v1, v2;
  sii s;
  while (m--) {
    int x, y;
    cin >> x >> y;
    mp[x]++;
    mp1[y]++;
    v1.pb(x);
    v2.pb(y);
    s.insert({x, y});
  }
  mii ::iterator it;
  int pos = -1, pos1 = -1;
  int maxm = -1, maxm1 = -1;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second > maxm) {
      maxm = it->second;
      pos = it->first;
    }
  }
  for (it = mp1.begin(); it != mp1.end(); it++) {
    if (it->second > maxm1) {
      maxm1 = it->second;
      pos1 = it->first;
    }
  }
  vi row, col;

  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second == maxm)
      row.pb(it->first);
  }

  for (it = mp1.begin(); it != mp1.end(); it++) {
    if (it->second == maxm1)
      col.pb(it->first);
  }
  // int max=-1;
  /*for(auto x:row)
  cout<<x<<" ";
  cout<<endl;
  for(auto x:col)
  cout<<x<<" ";
  cout<<endl;
  cout<<maxm<<endl;
  cout<<maxm1<<endl;*/
  int ans = maxm + maxm1 - 1;
  for (int i = 0; i < row.size(); i++) {
    int flag = 0;
    for (int j = 0; j < col.size(); j++) {

      auto ite = s.find({row[i], col[j]});
      if (ite == s.end()) {

        ans = maxm + maxm1;
        flag = 1;
        break;
      }
    }
    if (flag == 1)
      break;
  }
  cout << ans << endl;
}

int32_t main() {
  faster;
  /*sieve();*/
  // compute();
  // int t;cin>>t;
  int t = 1;
  while (t--)
    solve();

  /*4 4 6
  1 1
  1 4
  4 1
  4 4
  2 2
  2 3*/

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define loop(i, a, b) for (int i = a; i < b; i++)
#define scan(arr, n)                                                           \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair<int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map<int, int>
#define pb push_back
#define faster                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
// Constants
const int MOD = 1000000007; /* 1e9 + 7*/
const int MAXN = 1000005;   /*1e6 +5 */

int spf[1000009]; // stores the smallest prime factor of a number

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++)
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
}

/* Iterative Function to calculate (x^y)%p in O(log y) */
int power(int x, int y, int p) {
  int res = 1; // Initialize result
  x = x % p;   // Update x if it is more than or
               // equal to p
  if (x == 0)
    return 0; // In case x is divisible by p;
  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;
    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  mii mp, mp1;
  // int cp=m;
  vi v1, v2;
  sii s;
  while (m--) {
    int x, y;
    cin >> x >> y;
    mp[x]++;
    mp1[y]++;
    v1.pb(x);
    v2.pb(y);
    s.insert({x, y});
  }
  mii ::iterator it;
  int pos = -1, pos1 = -1;
  int maxm = -1, maxm1 = -1;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second > maxm) {
      maxm = it->second;
      pos = it->first;
    }
  }
  for (it = mp1.begin(); it != mp1.end(); it++) {
    if (it->second > maxm1) {
      maxm1 = it->second;
      pos1 = it->first;
    }
  }
  vi row, col;

  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->second == maxm)
      row.pb(it->first);
  }

  for (it = mp1.begin(); it != mp1.end(); it++) {
    if (it->second == maxm1)
      col.pb(it->first);
  }
  // int max=-1;
  /*for(auto x:row)
  cout<<x<<" ";
  cout<<endl;
  for(auto x:col)
  cout<<x<<" ";
  cout<<endl;
  cout<<maxm<<endl;
  cout<<maxm1<<endl;*/
  int ans = maxm + maxm1 - 1;
  for (int i = 0; i < row.size(); i++) {
    for (int j = 0; j < col.size(); j++) {
      auto ite = s.find({row[i], col[j]});
      if (ite == s.end())

        ans = maxm + maxm1;
    }
  }
  cout << ans << endl;
}

int32_t main() {
  faster;
  /*sieve();*/
  // compute();
  // int t;cin>>t;
  int t = 1;
  while (t--)
    solve();

  /*4 4 6
  1 1
  1 4
  4 1
  4 4
  2 2
  2 3*/

  return 0;
}