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

// Author: ZindagiMeBasAishHiChahiye
// WA/TLE
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\\n'
#define mii map<ll int, ll int>
#define msi map<string, ll int>
#define mis map<ll int, string>
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define repp(i, a, b) for (ll int i = b - 1; i >= a; i--)
#define tr(container, it)                                                      \\
  for (__typeof__(container.begin()) it = container.begin();                   \\
       it != container.end(); it++)
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define mpi map<pair<ll int, ll int>, ll int>
#define vii vector<pair<ll, pair<ll, ll>>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
using namespace std;
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
#define ll long long
#define N 1000005
// ll po(ll a,ll b,ll m){
// 	a%=m;
// 	if(b==0) return 1;
// 	if(b%2) return (a*(po((a*a)%m,b/2,m)%m)%m)%m;
// 	return po((a*a)%m,b/2,m)%m;
// }

bool check(ll a, ll b, ll x, ll y) {
  if (a < 0 || b < 0)
    return false;
  if (a >= x || b >= y)
    return false;
  return true;
}
ll x[1004][1004];
bool vis[N];
string s[1000];
set<int> st[N];
vi ad[N];
void dfs(ll i, ll j, ll c, ll h, ll w) {
  if (x[i][j] || s[i][j] == '#')
    return;
  x[i][j] = c;
  if (check(i, j + 1, h, w))
    dfs(i, j + 1, c, h, w);
  if (check(i, j - 1, h, w))
    dfs(i, j - 1, c, h, w);
  if (check(i + 1, j, h, w))
    dfs(i + 1, j, c, h, w);
  if (check(i - 1, j, h, w))
    dfs(i - 1, j, c, h, w);
}

void bfs(ll a, ll b, ll &an) {
  queue<pii> q;
  q.push({a, 0});
  vis[a] = true;
  while (!q.empty()) {
    pii pp = q.front();
    q.pop();
    for (auto x : ad[pp.F]) {
      if (!vis[x]) {
        vis[x] = true;
        q.push({x, pp.S + 1});
        if (x == b) {
          an = pp.S + 1;
          return;
        }
      }
    }
  }
}
void solve() {
  ll h, w, M;
  cin >> h >> w >> M;
  vi ro(h, 0);
  vector<pii> co(w);
  rep(i, 0, w) {
    co[i].F = 0;
    co[i].S = i;
  }
  map<pii, ll> m;
  rep(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    m[{a, b}] = 1;
    ro[a]++;
    co[b].F++;
  }
  sort(all(co), greater<pii>());
  // cout<<co[1].F;
  ll an = 0;
  rep(i, 0, h) {
    rep(j, 0, w) {
      // cout<<i<<j<<ro[i]<<endl;
      ll c = ro[i] + co[j].F;
      if (m[{i, co[j].S}])
        an = max(an, c - 1);
      else {
        an = max(an, c);
        break;
      }
    }
  }
  cout << an;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll TESTS = 1, z = 1;
  // cin>>TESTS;
  while (TESTS--) {
    // time
    // cout<<"Case #"<<z<<": ";
    // z++;
    solve();
    time
  }
  return 0;
}

Error: TLE

Fixed submission:

// Author: ZindagiMeBasAishHiChahiye
// WA/TLE
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\\n'
#define mii map<ll int, ll int>
#define msi map<string, ll int>
#define mis map<ll int, string>
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define repp(i, a, b) for (ll int i = b - 1; i >= a; i--)
#define tr(container, it)                                                      \\
  for (__typeof__(container.begin()) it = container.begin();                   \\
       it != container.end(); it++)
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define mpi map<pair<ll int, ll int>, ll int>
#define vii vector<pair<ll, pair<ll, ll>>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
using namespace std;
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
#define ll long long
#define N 1000005
// ll po(ll a,ll b,ll m){
// 	a%=m;
// 	if(b==0) return 1;
// 	if(b%2) return (a*(po((a*a)%m,b/2,m)%m)%m)%m;
// 	return po((a*a)%m,b/2,m)%m;
// }

bool check(ll a, ll b, ll x, ll y) {
  if (a < 0 || b < 0)
    return false;
  if (a >= x || b >= y)
    return false;
  return true;
}
ll x[1004][1004];
bool vis[N];
string s[1000];
set<int> st[N];
vi ad[N];
void dfs(ll i, ll j, ll c, ll h, ll w) {
  if (x[i][j] || s[i][j] == '#')
    return;
  x[i][j] = c;
  if (check(i, j + 1, h, w))
    dfs(i, j + 1, c, h, w);
  if (check(i, j - 1, h, w))
    dfs(i, j - 1, c, h, w);
  if (check(i + 1, j, h, w))
    dfs(i + 1, j, c, h, w);
  if (check(i - 1, j, h, w))
    dfs(i - 1, j, c, h, w);
}

void bfs(ll a, ll b, ll &an) {
  queue<pii> q;
  q.push({a, 0});
  vis[a] = true;
  while (!q.empty()) {
    pii pp = q.front();
    q.pop();
    for (auto x : ad[pp.F]) {
      if (!vis[x]) {
        vis[x] = true;
        q.push({x, pp.S + 1});
        if (x == b) {
          an = pp.S + 1;
          return;
        }
      }
    }
  }
}
void solve() {
  ll h, w, M;
  cin >> h >> w >> M;
  vi ro(h, 0), co(w, 0);
  map<pii, ll> m;
  rep(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    m[{a, b}] = 1;
    ro[a]++;
    co[b]++;
  }
  ll an = 0;
  rep(i, 0, h) {
    rep(j, 0, w) {
      ll c = ro[i] + co[j];
      if (m[{i, j}])
        c--;
      an = max(an, c);
    }
  }
  cout << an;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll TESTS = 1, z = 1;
  // cin>>TESTS;
  while (TESTS--) {
    // time
    // cout<<"Case #"<<z<<": ";
    // z++;
    solve();
    time
  }
  return 0;
}