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

#include <bits/stdc++.h>
using namespace std;
using ll = int;
using ull = uint;

ull hashfunc(int64_t v) { return (ull)(v % 1000033LL); }

int64_t makekey(ll h, ll w) { return 1000037LL * h + 1000721LL * w + (h * w); }

const uint8_t HASH_EMPTY = 0;
const uint8_t HASH_EXIST = 1;
const uint8_t HASH_DELETED = 2;
template <class K, class V> struct bunordered_map_elem {
  uint8_t exist; // 0:存在しない, 1:存在する, 2:削除後
  K key;
  V value;
};
template <class K, class V> class bunordered_map {
public:
  bunordered_map_elem<K, V> *table;
  ull cap;
  ull length;
  bunordered_map() {
    cap = 97;
    table = new bunordered_map_elem<K, V>[cap];
    for (ull i = 0; i < cap; i++) {
      table[i].exist = HASH_EMPTY;
    }
    length = 0;
  }
  bunordered_map(ull init_cap) {
    if (init_cap == 0) {
      throw exception();
    }
    cap = init_cap;
    table = new bunordered_map_elem<K, V>[cap];
    for (ull i = 0; i < cap; i++) {
      table[i].exist = HASH_EMPTY;
    }
    length = 0;
  }
  ~bunordered_map() { delete table; }
  void insert(K key, V value) {
    ull idx = hashfunc(key);
    while (true) {
      auto elem = table[idx];
      if (elem.exist == HASH_EMPTY || elem.exist == HASH_DELETED) {
        // 新規登録
        table[idx] = bunordered_map_elem<K, V>{
          exist : HASH_EXIST,
          key : key,
          value : value
        };
        length++;
        break;
      } else if (elem.exist == 1 && elem.key == key) {
        // 上書き
        table[idx] = bunordered_map_elem<K, V>{
          exist : HASH_EXIST,
          key : key,
          value : value
        };
        break;
      } else {
        idx++;
        if (idx >= cap) {
          idx = 0;
        }
      }
    }
  }
  ull get_index(K key) {
    ull idx = hashfunc(key);
    while (true) {
      auto elem = table[idx];
      if (elem.exist == HASH_EMPTY) {
        return cap + 1;
      } else if (elem.exist == HASH_EXIST && elem.key == key) {
        return idx;
      } else {
        idx++;
        if (idx >= cap) {
          idx = 0;
        }
      }
    }
  }
  uint8_t count(K key) {
    if (get_index(key) == cap + 1) {
      return 0;
    } else {
      return 1;
    }
  }
};

ll maxlist(vector<ll> &maxindices, vector<ll> &counts) {
  ll maxval = -1;
  for (ll i = 0; i < (ll)counts.size(); i++) {
    if (maxval == counts[i]) {
      maxindices.push_back(i);
    } else if (maxval < counts[i]) {
      maxval = counts[i];
      maxindices.clear();
      maxindices.push_back(i);
    }
  }
  return maxval;
}

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> hcount(H, 0);
  vector<ll> wcount(W, 0);

  bunordered_map<int64_t, bool> exist(5000011);
  for (ll i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    hcount[h]++;
    wcount[w]++;
    exist.insert(makekey(h, w), true);
  }
  vector<ll> max_hi;
  ll max_h = maxlist(max_hi, hcount);
  vector<ll> max_wi;
  ll max_w = maxlist(max_wi, wcount);

  for (auto hi : max_hi) {
    for (auto wi : max_wi) {
      if (!exist.count(makekey(hi, wi))) {
        cout << max_h + max_w << endl;
        return 0;
      }
    }
  }
  cout << max_h + max_w - 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int;
using ull = uint;

ull hashfunc(int64_t v) { return (ull)(v % 1000033LL); }

int64_t makekey(ll h, ll w) { return 1000037LL * h + w; }

const uint8_t HASH_EMPTY = 0;
const uint8_t HASH_EXIST = 1;
const uint8_t HASH_DELETED = 2;
template <class K, class V> struct bunordered_map_elem {
  uint8_t exist; // 0:存在しない, 1:存在する, 2:削除後
  K key;
  V value;
};
template <class K, class V> class bunordered_map {
public:
  bunordered_map_elem<K, V> *table;
  ull cap;
  ull length;
  bunordered_map() {
    cap = 97;
    table = new bunordered_map_elem<K, V>[cap];
    for (ull i = 0; i < cap; i++) {
      table[i].exist = HASH_EMPTY;
    }
    length = 0;
  }
  bunordered_map(ull init_cap) {
    if (init_cap == 0) {
      throw exception();
    }
    cap = init_cap;
    table = new bunordered_map_elem<K, V>[cap];
    for (ull i = 0; i < cap; i++) {
      table[i].exist = HASH_EMPTY;
    }
    length = 0;
  }
  ~bunordered_map() { delete table; }
  void insert(K key, V value) {
    ull idx = hashfunc(key);
    while (true) {
      auto elem = table[idx];
      if (elem.exist == HASH_EMPTY || elem.exist == HASH_DELETED) {
        // 新規登録
        table[idx] = bunordered_map_elem<K, V>{
          exist : HASH_EXIST,
          key : key,
          value : value
        };
        length++;
        break;
      } else if (elem.exist == 1 && elem.key == key) {
        // 上書き
        table[idx] = bunordered_map_elem<K, V>{
          exist : HASH_EXIST,
          key : key,
          value : value
        };
        break;
      } else {
        idx++;
        if (idx >= cap) {
          idx = 0;
        }
      }
    }
  }
  ull get_index(K key) {
    ull idx = hashfunc(key);
    while (true) {
      auto elem = table[idx];
      if (elem.exist == HASH_EMPTY) {
        return cap + 1;
      } else if (elem.exist == HASH_EXIST && elem.key == key) {
        return idx;
      } else {
        idx++;
        if (idx >= cap) {
          idx = 0;
        }
      }
    }
  }
  uint8_t count(K key) {
    if (get_index(key) == cap + 1) {
      return 0;
    } else {
      return 1;
    }
  }
};

ll maxlist(vector<ll> &maxindices, vector<ll> &counts) {
  ll maxval = -1;
  for (ll i = 0; i < (ll)counts.size(); i++) {
    if (maxval == counts[i]) {
      maxindices.push_back(i);
    } else if (maxval < counts[i]) {
      maxval = counts[i];
      maxindices.clear();
      maxindices.push_back(i);
    }
  }
  return maxval;
}

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> hcount(H, 0);
  vector<ll> wcount(W, 0);

  bunordered_map<int64_t, bool> exist(5000011);
  for (ll i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    hcount[h]++;
    wcount[w]++;
    exist.insert(makekey(h, w), true);
  }
  vector<ll> max_hi;
  ll max_h = maxlist(max_hi, hcount);
  vector<ll> max_wi;
  ll max_w = maxlist(max_wi, wcount);

  for (auto hi : max_hi) {
    for (auto wi : max_wi) {
      if (!exist.count(makekey(hi, wi))) {
        cout << max_h + max_w << endl;
        return 0;
      }
    }
  }
  cout << max_h + max_w - 1 << endl;
}