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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pb push_back
#define F(i, a, b) for (int i = a; i < b; ++i)
#define Fl(i, a, b) for (long long i = a; i < b; ++i)
#define all(v) v.begin(), v.end()
#define X first
#define Y second
#define mp make_pair
typedef long long ll;
typedef std::vector<int> vec;
typedef std::vector<long long> vecl;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1000000007;
#define pie 3.141592653589793238462643383279

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t{}, T{};
  // cin>>t;T=t;
  t = 1;
  while (t--) {
    // cout<<"------------- #"<<T-t<<endl;
    ll n{}, m{}, ans{}, k{}, a{}, b{}, c{}, x{}, y{}, f{1};
    cin >> n >> m >> k;
    map<pll, int> mm;
    vecl row(n + 1, 0), col(m + 1, 0);
    Fl(i, 0, k) {
      cin >> a >> b;
      mm[mp(a, b)] = 1;
      row[a]++;
      col[b]++;
    }
    if (n * m == 1) {
      if (k == 1) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
      return 0;
    }
    a = max_element(all(row)) - row.begin(); //++a;
    b = max_element(all(col)) - col.begin(); //++b;
    // cout<<a<<"---"<<b<<endl;
    if (row[a] > col[b]) {
      F(j, 1, m + 1) {
        if (mm[mp(a, j)] == 1) {
          ans = max(ans, col[j] - 1);
        } else {
          ans = max(ans, col[j]);
        }
      }
      ans += row[a];
      // cout<<"=";
    } else if (row[a] < col[b]) {
      F(i, 1, n + 1) {
        if (mm[mp(i, b)] == 1) {
          ans = max(ans, row[i] - 1);
        } else {
          ans = max(ans, row[i]);
        }
      }
      ans += col[b];
    } else {
      F(j, 1, m + 1) {
        if (mm[mp(a, j)] == 1) {
          x = max(x, col[j] - 1);
        } else {
          x = max(x, col[j]);
        }
      }
      x += row[a];

      F(i, 1, n + 1) {
        if (mm[mp(i, b)] == 1) {
          y = max(y, row[i] - 1);
        } else {
          y = max(y, row[i]);
        }
      }
      y += col[b];
      ans = max(x, y);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pb push_back
#define F(i, a, b) for (int i = a; i < b; ++i)
#define Fl(i, a, b) for (long long i = a; i < b; ++i)
#define all(v) v.begin(), v.end()
#define X first
#define Y second
#define mp make_pair
typedef long long ll;
typedef std::vector<int> vec;
typedef std::vector<long long> vecl;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1000000007;
#define pie 3.141592653589793238462643383279

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t{}, T{};
  // cin>>t;T=t;
  t = 1;
  while (t--) {
    // cout<<"------------- #"<<T-t<<endl;
    ll n{}, m{}, ans{}, k{}, a{}, b{}, c{}, f{1};
    cin >> n >> m >> k;
    map<pll, int> mm;
    vecl row(n + 1, 0), col(m + 1, 0);
    Fl(i, 0, k) {
      cin >> a >> b;
      mm[mp(a, b)] = 1;
      row[a]++;
      col[b]++;
    }
    if (n * m == 1) {
      if (k == 1) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
      return 0;
    }
    Fl(i, 1, n + 1) {
      F(j, 1, m + 1) {

        if (mm[mp(i, j)] == 1) {
          ans = max(ans, row[i] + col[j] - 1);
        } else {
          ans = max(ans, row[i] + col[j]);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}