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

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>     //required

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(), a.end()
#define fr(i, x, y) for (int i = x; i < y; i++)
#define frr(i, x, y) for (int i = x - 1; i >= y; i--)
#define inf 1e18 + 1
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll power(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
ll powerm(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
    }
    a = a * a;
    a %= mod;
    b >>= 1;
  }
  return ans % mod;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

void solve() {
  int r, c, m;
  cin >> r >> c >> m;
  VI a(r, 0), b(c, 0);
  // vector<VI> dp(r,VI(c,0));
  vector<PII> arr;
  map<PII, int> mp;
  fr(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    a[x]++;
    b[y]++;
    // dp[x][y]=1;
    arr.pb({x, y});
    mp[{x, y}] = 1;
  }
  vector<PII> col, row;

  fr(i, 0, c) { col.pb({b[i], i}); }
  fr(i, 0, r) { row.pb({a[i], i}); }
  sort(ALL(row));
  sort(ALL(col));
  int ans = 0;
  frr(i, r, r - 1) {
    if (i < 0)
      break;
    frr(j, c, 0) {
      if (j < 0 || col[j].F != col.back().F)
        break;
      int val = row[i].F + col[j].F - mp[{row[i].S, col[j].S}];
      ans = max(ans, val);
    }
  }
  frr(j, c, c - 1) {
    if (j < 0)
      break;
    frr(i, r, 0) {
      if (i < 0 || row[i].F != row.back().F)
        break;
      int val = row[i].F + col[j].F - mp[{row[i].S, col[j].S}];
      ans = max(ans, val);
    }
  }
  // fr(i,0,r)
  // {
  //     fr(j,0,c)
  //     {
  //         //if(mp.count({i,j}))
  //         {
  //             ans=max(ans,a[i]+b[j]-dp[i][j]);
  //         }
  //         // else
  //         // {
  //         //     ans=max({ans,a[i]+b[j]});
  //         // }

  //     }
  // }
  cout << ans << endl;
  return;
}
int main() {
  IOS;
  ll t = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<pp-t<<":\\n";
    solve();
  }
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>     //required

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(), a.end()
#define fr(i, x, y) for (int i = x; i < y; i++)
#define frr(i, x, y) for (int i = x - 1; i >= y; i--)
#define inf 1e18 + 1
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll power(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
ll powerm(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
    }
    a = a * a;
    a %= mod;
    b >>= 1;
  }
  return ans % mod;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

void solve() {
  ll r, c, m;
  cin >> r >> c >> m;
  VL a(r, 0), b(c, 0);
  map<PLL, int> mp;
  fr(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    a[x]++;
    b[y]++;
    mp[{x, y}] = 1;
  }
  ll ans = 0;
  fr(i, 0, r) {
    fr(j, 0, c) {
      if (mp.count({i, j})) {
        ans = max(ans, (a[i] - 1 + b[j] - 1) + 1LL);
      } else {
        ans = max({ans, a[i] + b[j]});
      }
    }
  }
  cout << ans << endl;
  return;
}
int main() {
  IOS;
  ll t = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<pp-t<<":\\n";
    solve();
  }
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm