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

#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int charToInt(char c) {
  char zero_num = '0';
  return (int)c - (int)zero_num;
}

int search_val(vec_int &A, int n) {
  // 整数nを配列Aの中から探して、その最初のindexを渡す
  // ある要素iがA[i] = nだった場合には、
  // lowerbound(A.begin(),A.end(),n) = i
  // upperbound(A.begin(),A.end(),n) > i
  // となることを利用する。
  auto iter1 = lower_bound(A.begin(), A.end(), n);
  auto iter2 = upper_bound(A.begin(), A.end(), n);
  if (iter1 == iter2) {
    // この時にはnがAの中に存在しない
    return -1;
  }

  return distance(A.begin(), iter1);
}
signed main() {
  int H, W, M;
  cin >> H >> W >> M;
  vec_int h(M), w(M);
  rep(i, M) cin >> h.at(i) >> w.at(i);
  vec_int h2(M), w2(M);
  rep(i, M) {
    h2.at(i) = h.at(i);
    w2.at(i) = w.at(i);
  }

  sort(h.begin(), h.end());
  sort(w.begin(), w.end());

  if (M == 1) {
    cout << 1 << endl;
    return 0;
  }

  int max_num_h, max_num_w;

  int prev_h = h.at(0);
  int temp_num_h = 1;
  max_num_h = 1;
  for (int i = 1; i < M; i++) {
    if (prev_h == h.at(i)) {
      temp_num_h++;
      if (i == M - 1) {
        if (temp_num_h > max_num_h) {
          max_num_h = temp_num_h;
        }
      }
    } else {
      if (temp_num_h > max_num_h) {
        max_num_h = temp_num_h;
      }
      temp_num_h = 1;
      prev_h = h.at(i);
    }
  }

  int prev_w = w.at(0);
  int temp_num_w = 1;
  max_num_w = 1;

  for (int i = 1; i < M; i++) {
    if (prev_w == w.at(i)) {
      temp_num_w++;
      if (i == M - 1) {
        if (temp_num_w > max_num_w) {
          max_num_w = temp_num_w;
        }
      }
    } else {
      if (temp_num_w > max_num_w) {
        max_num_w = temp_num_w;
      }
      temp_num_w = 1;
      prev_w = w.at(i);
    }
  }

  vec_int candidates_h;
  prev_h = h.at(0);
  temp_num_h = 1;
  for (int i = 1; i < M; i++) {
    if (prev_h == h.at(i)) {
      temp_num_h++;
      if (i == M - 1) {
        if (temp_num_h == max_num_h) {
          candidates_h.push_back(prev_h);
        }
      }
    } else {
      if (temp_num_h == max_num_h) {
        candidates_h.push_back(prev_h);
      }
      temp_num_h = 1;
      prev_h = h.at(i);
      if (i == M - 1) {
        if (temp_num_h == max_num_h) {
          candidates_h.push_back(prev_h);
        }
      }
    }
  }

  vec_int candidates_w;
  prev_w = w.at(0);
  temp_num_w = 1;
  for (int i = 1; i < M; i++) {
    if (prev_w == w.at(i)) {
      temp_num_w++;
      if (i == M - 1) {
        if (temp_num_w == max_num_w) {
          candidates_w.push_back(prev_w);
        }
      }
    } else {
      if (temp_num_w == max_num_w) {
        candidates_w.push_back(prev_w);
      }
      temp_num_w = 1;
      prev_w = w.at(i);
      if (i == M - 1) {
        if (temp_num_w == max_num_w) {
          candidates_w.push_back(prev_w);
        }
      }
    }
  }

  sort(candidates_h.begin(), candidates_h.end());
  sort(candidates_w.begin(), candidates_w.end());
  int combination = candidates_h.size() * candidates_w.size();

  int index = 0;
  for (int i = 0; i < M; i++) {
    int hpos = h2.at(i);
    int wpos = w2.at(i);
    int flag = 1;
    if (search_val(candidates_h, hpos) == -1)
      flag = 0;
    if (search_val(candidates_w, wpos) == -1)
      flag = 0;
    if (flag == 1)
      index++;
  }
  if (index == combination) {
    cout << max_num_h + max_num_w - 1 << endl;
  } else {
    cout << max_num_h + max_num_w << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int charToInt(char c) {
  char zero_num = '0';
  return (int)c - (int)zero_num;
}

signed main() {
  int H, W, M;
  cin >> H >> W >> M;
  vec_int h(M), w(M);
  rep(i, M) cin >> h.at(i) >> w.at(i);
  vec_int h2(M), w2(M);
  rep(i, M) {
    h2.at(i) = h.at(i);
    w2.at(i) = w.at(i);
  }

  sort(h.begin(), h.end());
  sort(w.begin(), w.end());

  if (M == 1) {
    cout << 1 << endl;
    return 0;
  }

  int max_num_h, max_num_w;

  int prev_h = h.at(0);
  int temp_num_h = 1;
  max_num_h = 1;
  for (int i = 1; i < M; i++) {
    if (prev_h == h.at(i)) {
      temp_num_h++;
      if (i == M - 1) {
        if (temp_num_h > max_num_h) {
          max_num_h = temp_num_h;
        }
      }
    } else {
      if (temp_num_h > max_num_h) {
        max_num_h = temp_num_h;
      }
      temp_num_h = 1;
      prev_h = h.at(i);
    }
  }

  int prev_w = w.at(0);
  int temp_num_w = 1;
  max_num_w = 1;

  for (int i = 1; i < M; i++) {
    if (prev_w == w.at(i)) {
      temp_num_w++;
      if (i == M - 1) {
        if (temp_num_w > max_num_w) {
          max_num_w = temp_num_w;
        }
      }
    } else {
      if (temp_num_w > max_num_w) {
        max_num_w = temp_num_w;
      }
      temp_num_w = 1;
      prev_w = w.at(i);
    }
  }

  vec_int candidates_h;
  prev_h = h.at(0);
  temp_num_h = 1;
  for (int i = 1; i < M; i++) {
    if (prev_h == h.at(i)) {
      temp_num_h++;
      if (i == M - 1) {
        if (temp_num_h == max_num_h) {
          candidates_h.push_back(prev_h);
        }
      }
    } else {
      if (temp_num_h == max_num_h) {
        candidates_h.push_back(prev_h);
      }
      temp_num_h = 1;
      prev_h = h.at(i);
      if (i == M - 1) {
        if (temp_num_h == max_num_h) {
          candidates_h.push_back(prev_h);
        }
      }
    }
  }

  vec_int candidates_w;
  prev_w = w.at(0);
  temp_num_w = 1;
  for (int i = 1; i < M; i++) {
    if (prev_w == w.at(i)) {
      temp_num_w++;
      if (i == M - 1) {
        if (temp_num_w == max_num_w) {
          candidates_w.push_back(prev_w);
        }
      }
    } else {
      if (temp_num_w == max_num_w) {
        candidates_w.push_back(prev_w);
      }
      temp_num_w = 1;
      prev_w = w.at(i);
      if (i == M - 1) {
        if (temp_num_w == max_num_w) {
          candidates_w.push_back(prev_w);
        }
      }
    }
  }

  sort(candidates_h.begin(), candidates_h.end());
  sort(candidates_w.begin(), candidates_w.end());
  int combination = candidates_h.size() * candidates_w.size();

  int index = 0;
  for (int i = 0; i < M; i++) {
    int hpos = h2.at(i);
    int wpos = w2.at(i);
    int flag = 1;
    if (find(candidates_h.begin(), candidates_h.end(), hpos) ==
        candidates_h.end())
      flag = 0;
    if (find(candidates_w.begin(), candidates_w.end(), wpos) ==
        candidates_w.end())
      flag = 0;
    if (flag == 1)
      index++;
  }
  if (index == combination) {
    cout << max_num_h + max_num_w - 1 << endl;
  } else {
    cout << max_num_h + max_num_w << endl;
  }

  return 0;
}