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

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define mkp make_pair
#define pi pair<int, int>
#define push push
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define repd(i, l, r) for (int i = (int)(l); i >= (int)(r); i--)
int power(int x, unsigned int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int powermod(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
#define print2d(mat, n, m)                                                     \\
  {                                                                            \\
    for (int i = 0; i < (int)(n); i++) {                                       \\
      for (int j = 0; j < (m); j++) {                                          \\
        cout << mat[i][j] << " ";                                              \\
      }                                                                        \\
      cout << endl;                                                            \\
    }                                                                          \\
  }
#define print1d(mat, n)                                                        \\
  {                                                                            \\
    for (int i = 0; i < (int)(n); i++)                                         \\
      cout << mat[i] << " ";                                                   \\
    cout << endl;                                                              \\
  }
#define clr(a, x) memset(a, x, sizeof(a))
#define rr(v) for (auto &val : v)
#define print(v)                                                               \\
  for (const auto itr : v) {                                                   \\
    cout << itr << ' ';                                                        \\
  }                                                                            \\
  cout << endl;
#define init(v, x)                                                             \\
  for (auto &itr : v) {                                                        \\
    itr = x;                                                                   \\
  }
#define minpq(x) x, vector<x>, greater<x>
#define ln length()
#define sz size()
#define inf 1e18;
const int mod = 1e9 + 7;

void solve() {
  int h, w, m1, r, c;
  map<int, set<int>> mc, mr;
  cin >> h >> w;
  cin >> m1;
  rep(i, 0, m1) {
    cin >> r >> c;
    mc[c].insert(r);
    mr[r].insert(c);
  }
  //   return ;
  multiset<pair<int, set<int>>, greater<pair<int, set<int>>>> m;
  rr(mc) { m.insert({val.se.sz, val.se}); }
  int ans = 0;
  rep(i, 1, h + 1) {
    //   cout<<i<<ans<<endl;
    if (mr[i].sz > 0) {
      int s = (*m.begin()).fi;
      rr(m) {
        if (val.fi != s) {
          break;
        }
        if (val.se.find(i) == val.se.end()) {
          ans = max(ans, (int)mr[i].sz + val.fi);
          break;
        } else {
          ans = max(ans, (int)mr[i].sz + val.fi - 1);
        }
        // break;
      }
    }
  }
  cout << ans << endl;
}

int32_t main() {
  fastIO
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define mkp make_pair
#define pi pair<int, int>
#define push push
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define repd(i, l, r) for (int i = (int)(l); i >= (int)(r); i--)
int power(int x, unsigned int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int powermod(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
#define print2d(mat, n, m)                                                     \\
  {                                                                            \\
    for (int i = 0; i < (int)(n); i++) {                                       \\
      for (int j = 0; j < (m); j++) {                                          \\
        cout << mat[i][j] << " ";                                              \\
      }                                                                        \\
      cout << endl;                                                            \\
    }                                                                          \\
  }
#define print1d(mat, n)                                                        \\
  {                                                                            \\
    for (int i = 0; i < (int)(n); i++)                                         \\
      cout << mat[i] << " ";                                                   \\
    cout << endl;                                                              \\
  }
#define clr(a, x) memset(a, x, sizeof(a))
#define rr(v) for (auto &val : v)
#define print(v)                                                               \\
  for (const auto itr : v) {                                                   \\
    cout << itr << ' ';                                                        \\
  }                                                                            \\
  cout << endl;
#define init(v, x)                                                             \\
  for (auto &itr : v) {                                                        \\
    itr = x;                                                                   \\
  }
#define minpq(x) x, vector<x>, greater<x>
#define ln length()
#define sz size()
#define inf 1e18;
const int mod = 1e9 + 7;

void solve() {
  int h, w, m1, r, c;
  map<int, set<int>> mc, mr;
  cin >> h >> w;
  cin >> m1;
  rep(i, 0, m1) {
    cin >> r >> c;
    mc[c].insert(r);
    mr[r].insert(c);
  }
  //   return ;
  multiset<pair<int, set<int>>, greater<pair<int, set<int>>>> m;
  rr(mc) { m.insert({val.se.sz, val.se}); }
  int ans = 0;
  rep(i, 1, h + 1) {
    //   cout<<i<<ans<<endl;
    if (mr[i].sz > 0) {
      int s = (*m.begin()).fi;
      rr(m) {
        if (val.fi != s) {
          break;
        }
        if (val.se.find(i) == val.se.end()) {
          ans = max(ans, (int)mr[i].sz + val.fi);
        } else {
          ans = max(ans, (int)mr[i].sz + val.fi - 1);
        }
      }
    }
  }
  cout << ans << endl;
}

int32_t main() {
  fastIO
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}