Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long H, W, M;
  cin >> H >> W >> M;
  vector<long> yoko(H);
  vector<long> tate(W);
  long h, w;
  vector<long> target[H];
  vector<long> target_[W];
  for (size_t i = 0; i < M; i++) {
    cin >> h >> w;
    h--;
    w--;
    target[h].push_back(w);
    target_[w].push_back(h);
    yoko[h]++;
    tate[w]++;
  }
  vector<long> yoko_ = yoko;
  vector<long> tate_ = tate;

  vector<long>::iterator y_iter = max_element(yoko.begin(), yoko.end());
  size_t y_index = distance(yoko.begin(), y_iter);
  for (auto w : target[y_index])
    tate[w]--;
  vector<long>::iterator t_iter = max_element(tate.begin(), tate.end());
  size_t t_index = distance(tate.begin(), t_iter);

  vector<long>::iterator t_iter_ = max_element(tate_.begin(), tate_.end());
  size_t t_index_ = distance(tate_.begin(), t_iter_);
  for (auto h : target_[t_index_])
    yoko_[h]--;
  vector<long>::iterator y_iter_ = max_element(yoko_.begin(), yoko_.end());
  size_t y_index_ = distance(yoko_.begin(), y_iter_);
  cout << max(*y_iter + *t_iter, *y_iter_ + *t_iter_) << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}

Error: 0
2:0:2
1:1:1


Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long H, W, M;
  cin >> H >> W >> M;
  vector<long> yoko(H);
  vector<long> tate(W);
  long h, w;
  std::vector<std::vector<long>> mat(H, vector<long>(W, 0));
  for (size_t i = 0; i < M; i++) {
    cin >> h >> w;
    h--;
    w--;
    mat[h][w]++;
    yoko[h]++;
    tate[w]++;
  }
  vector<long>::iterator y_iter = max_element(yoko.begin(), yoko.end());
  vector<long>::iterator t_iter = max_element(tate.begin(), tate.end());
  size_t y_index = distance(yoko.begin(), y_iter);
  if (*t_iter == 1) {
    for (size_t i = 0; i < W; i++) {
      if (mat[y_index][i])
        tate[i]--;
    }
    t_iter = max_element(tate.begin(), tate.end());
  }
  size_t t_index = distance(tate.begin(), t_iter);
  cerr << *y_iter << ":" << y_index << ":" << yoko[y_index] << endl;
  cerr << *t_iter << ":" << t_index << ":" << tate[t_index] << endl;
  if (mat[y_index][t_index] == 1)
    cout << *y_iter + *t_iter - 1 << endl;
  else
    cout << *y_iter + *t_iter << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}