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

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

/* TEMPLATE ENDS HERE */

#define MAXN (1 << 20)
int x[MAXN], y[MAXN];
int h, w, m;

int vmax(int a[MAXN], int s) {
  int ret = 0;
  for (int i = 1; i <= s; i++) {
    ret = max(ret, a[i]);
  }
  return ret;
}

vector<int> vmaxs(int a[MAXN], int v, int l) {
  vector<int> ret;
  for (int i = 1; i <= l; i++) {
    if (a[i] == v) {
      ret.push_back(i);
    }
  }
  return ret;
}

set<pair<int, int>> s;

int r[MAXN], c[MAXN];

void print(vector<int> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << endl;
}

int cntx[MAXN];
int cnty[MAXN];

void fillv(vector<int> v, int a[MAXN]) {
  for (int i = 0; i < v.size(); i++) {
    a[v[i]] = 1;
  }
}

int main() {
  cout << fixed << setprecision(12);
  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    r[x[i]]++;
    c[y[i]]++;
    s.insert(make_pair(x[i], y[i]));
  }
  int mx = vmax(r, h);
  int my = vmax(c, w);
  vector<int> vx = vmaxs(r, mx, h);
  vector<int> vy = vmaxs(c, my, w);
  fillv(vx, cntx);
  fillv(vy, cnty);
  // print(vx);
  // print(vy);
  int ret = mx + my;
  int cnt = 0;
  int rect = (ll)vx.size() * (ll)vy.size();
  if (rect <= m) {
    for (int i = 0; i < m; i++) {
      if (cntx[x[i]] && cnty[y[i]]) {
        cnt++;
      }
    }
  }
  if (cnt == rect) {
    ret--;
  }
  cout << ret << endl;
  return 0;
}

ll MOD = ((ll)1e9 + 7);

struct mod {
  ll x;

  mod(ll tx = 0) {
    x = tx;
    x %= MOD;
  }

  mod &operator=(const ll &tx) {
    x = tx % MOD;
    return *this;
  }

  mod &operator=(const mod &m2) {
    x = m2.x;
    return *this;
  }

  mod operator+(const mod &m2) const { return mod((x + m2.x) % MOD); }

  mod &operator+=(const mod &m2) {
    x += m2.x;
    x %= MOD;
    return *this;
  }

  mod operator-(const mod &m2) const { return mod((x - m2.x + MOD) % MOD); }

  mod &operator-=(const mod &m2) {
    x -= m2.x;
    x = (x + MOD) % MOD;
    return *this;
  }

  mod operator*(const mod &m2) const { return mod((x * m2.x) % MOD); }

  mod &operator*=(const mod &m2) {
    x *= m2.x;
    x %= MOD;
    return *this;
  }

  mod mypow(ll k) const {
    mod ret = 1;
    mod cur = x;
    while (k) {
      if (k & 1) {
        ret *= cur;
      }
      cur *= cur;
      k >>= 1;
    }
    return ret;
  }

  mod inv() const { return mypow(MOD - 2); }

  mod operator/(const mod &m2) const { return (*this) * m2.inv(); }

  mod &operator/=(const mod &m2) {
    x *= m2.inv().x;
    x %= MOD;
    return *this;
  }

  bool operator<(const mod &m2) const { return x < m2.x; }
};

ostream &operator<<(ostream &os, const mod &m) { return os << m.x; }


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

/* TEMPLATE ENDS HERE */

#define MAXN (1 << 20)
int x[MAXN], y[MAXN];
int h, w, m;

int vmax(int a[MAXN], int s) {
  int ret = 0;
  for (int i = 1; i <= s; i++) {
    ret = max(ret, a[i]);
  }
  return ret;
}

vector<int> vmaxs(int a[MAXN], int v, int l) {
  vector<int> ret;
  for (int i = 1; i <= l; i++) {
    if (a[i] == v) {
      ret.push_back(i);
    }
  }
  return ret;
}

set<pair<int, int>> s;

int r[MAXN], c[MAXN];

void print(vector<int> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << endl;
}

int main() {
  cout << fixed << setprecision(12);
  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    r[x[i]]++;
    c[y[i]]++;
    s.insert(make_pair(x[i], y[i]));
  }
  int mx = vmax(r, h);
  int my = vmax(c, w);
  vector<int> vx = vmaxs(r, mx, h);
  vector<int> vy = vmaxs(c, my, w);
  // print(vx);
  // print(vy);
  int ret = mx + my;
  int cnt = 0;
  int rect = (ll)vx.size() * (ll)vy.size();
  if (rect <= m) {
    for (auto &e1 : vx) {
      for (auto &e2 : vy) {
        if (s.count(make_pair(e1, e2))) {
          cnt++;
        }
      }
    }
  }
  if (cnt == rect) {
    ret--;
  }
  cout << ret << endl;
  return 0;
}

ll MOD = ((ll)1e9 + 7);

struct mod {
  ll x;

  mod(ll tx = 0) {
    x = tx;
    x %= MOD;
  }

  mod &operator=(const ll &tx) {
    x = tx % MOD;
    return *this;
  }

  mod &operator=(const mod &m2) {
    x = m2.x;
    return *this;
  }

  mod operator+(const mod &m2) const { return mod((x + m2.x) % MOD); }

  mod &operator+=(const mod &m2) {
    x += m2.x;
    x %= MOD;
    return *this;
  }

  mod operator-(const mod &m2) const { return mod((x - m2.x + MOD) % MOD); }

  mod &operator-=(const mod &m2) {
    x -= m2.x;
    x = (x + MOD) % MOD;
    return *this;
  }

  mod operator*(const mod &m2) const { return mod((x * m2.x) % MOD); }

  mod &operator*=(const mod &m2) {
    x *= m2.x;
    x %= MOD;
    return *this;
  }

  mod mypow(ll k) const {
    mod ret = 1;
    mod cur = x;
    while (k) {
      if (k & 1) {
        ret *= cur;
      }
      cur *= cur;
      k >>= 1;
    }
    return ret;
  }

  mod inv() const { return mypow(MOD - 2); }

  mod operator/(const mod &m2) const { return (*this) * m2.inv(); }

  mod &operator/=(const mod &m2) {
    x *= m2.inv().x;
    x %= MOD;
    return *this;
  }

  bool operator<(const mod &m2) const { return x < m2.x; }
};

ostream &operator<<(ostream &os, const mod &m) { return os << m.x; }