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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define fori(t) for (int i = 0; i < t; ++i)
#define forj(n) for (int j = 0; j < n; ++j)
#define fork(n) for (int k = 0; k < n; ++k)
#define forai(a) for (auto &i : a)
#define foraj(a) for (auto &j : a)
#define forak(a) for (auto &k : a)
#define forp(s, n, d) for (int p = s; p < n; p += d)
#define fast                                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  std::ios_base::sync_with_stdio(0);
#define pb(a) push_back(a);
#define pob(a) pop_back(a);
#define sorti(a) sort(a.begin(), a.end());
#define sortm(a, n) sort(a, a + n);
#define reversi(a) reverse(a.begin(), a.end());
#define fi first
#define se second
#define yes cout << "Yes\\n";
#define no cout << "No\\n";
#define ans(a) cout << a << '\\n';
#define res(a, n) a.resize(n);
#define read(a, n)                                                             \\
  a.resize(n);                                                                 \\
  fori(n) cin >> a[i];
// #define int long long
// #define double long double

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<long long, long long> llll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<int, pair<int, int>>> viii;
typedef pair<int, pair<int, int>> iii;
typedef vector<pair<long long, long long>> vllll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<bool> vb;
typedef pair<char, char> cc;
typedef pair<pair<char, char>, char> ccc;
typedef vector<char> vc;
typedef pair<int, char> ic;
typedef pair<ii, char> iic;
typedef vector<bool> vb;
typedef set<int> si;
typedef set<ii> sii;
typedef unsigned long long ull;
typedef pair<ull, ull> ullll;
typedef pair<double, double> dd;
typedef vector<dd> vdd;
typedef vector<pair<ull, ull>> vullll;
typedef pair<bool, bool> bb;
typedef vector<bb> vbb;
typedef vector<vector<ii>> vvii;
typedef vector<vector<iii>> vviii;

const int INF = 2147483647;
const ll LONGINF = 9223372036854775807;
const ll MOD = 1e9 + 7;

vi row, column;
vii ins;
si maxRows, maxCols;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  res(row, h) res(column, w) int maxRow = 0, maxCol = 0;
  while (m--) {
    int hi, wi;
    cin >> hi >> wi;
    ++row[hi - 1];
    ++column[wi - 1];
    maxRow = max(maxRow, row[hi - 1]);
    maxCol = max(maxCol, column[wi - 1]);
    ins.push_back({hi - 1, wi - 1});
  }
  fori(h) if (row[i] == maxRow) maxRows.insert(i);
  fori(w) if (column[i] == maxCol) maxCols.insert(i);
  ll countBoms = 0;
  forai(ins) {
    if (maxRows.find(i.fi) != maxRows.end() &&
        maxCols.find(i.se) != maxCols.end())
      ++countBoms;
  }
  if ((((long long)maxRows.size()) * ((long long)maxCols.size())) == countBoms)
    ans(maxRow + maxCol - 1) else ans(maxRow + maxCol)
}

signed main() {
  fast bool mult = false;
  if (mult) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define fori(t) for (int i = 0; i < t; ++i)
#define forj(n) for (int j = 0; j < n; ++j)
#define fork(n) for (int k = 0; k < n; ++k)
#define forai(a) for (auto &i : a)
#define foraj(a) for (auto &j : a)
#define forak(a) for (auto &k : a)
#define forp(s, n, d) for (int p = s; p < n; p += d)
#define fast                                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  std::ios_base::sync_with_stdio(0);
#define pb(a) push_back(a);
#define pob(a) pop_back(a);
#define sorti(a) sort(a.begin(), a.end());
#define sortm(a, n) sort(a, a + n);
#define reversi(a) reverse(a.begin(), a.end());
#define fi first
#define se second
#define yes cout << "Yes\\n";
#define no cout << "No\\n";
#define ans(a) cout << a << '\\n';
#define res(a, n) a.resize(n);
#define read(a, n)                                                             \\
  a.resize(n);                                                                 \\
  fori(n) cin >> a[i];
#define int long long
// #define double long double

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<long long, long long> llll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<int, pair<int, int>>> viii;
typedef pair<int, pair<int, int>> iii;
typedef vector<pair<long long, long long>> vllll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<bool> vb;
typedef pair<char, char> cc;
typedef pair<pair<char, char>, char> ccc;
typedef vector<char> vc;
typedef pair<int, char> ic;
typedef pair<ii, char> iic;
typedef vector<bool> vb;
typedef set<int> si;
typedef set<ii> sii;
typedef unsigned long long ull;
typedef pair<ull, ull> ullll;
typedef pair<double, double> dd;
typedef vector<dd> vdd;
typedef vector<pair<ull, ull>> vullll;
typedef pair<bool, bool> bb;
typedef vector<bb> vbb;
typedef vector<vector<ii>> vvii;
typedef vector<vector<iii>> vviii;

const int INF = 2147483647;
const ll LONGINF = 9223372036854775807;
const ll MOD = 1e9 + 7;

vi row, column;
sii ins;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  res(row, h) res(column, w) while (m--) {
    int hi, wi;
    cin >> hi >> wi;
    ++row[hi - 1];
    ++column[wi - 1];
    ins.insert({hi - 1, wi - 1});
  }
  int maxRes = 0;
  fori(h) {
    forj(w) {
      maxRes =
          max(maxRes, row[i] + column[j] - (ins.find({i, j}) != ins.end()));
    }
  }
  ans(maxRes)
}

signed main() {
  fast bool mult = false;
  if (mult) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else
    solve();
  return 0;
}