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

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define RFOR(i, j, n) for (int i = j - 1; i >= n; i--)
#define PREC(n) fixed << setprecision(n)
#define print_array(v)                                                         \\
  REP(__k, v.size()) {                                                         \\
    cout << v[__k];                                                            \\
    if (__k != v.size() - 1)                                                   \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define _LIBCPP_DEBUG 0
using namespace std;
int64_t MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m, cnt = 0;
  cin >> n >> m;

  vector<int> vec(n);
  vector<bool> ok(n, true);
  REP(i, n) cin >> vec[i];

  REP(j, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (vec[a] <= vec[b])
      ok[a] = false;
    if (vec[a] >= vec[b])
      ok[b] = false;
  }
  REP(i, n) if (ok[i]) cnt++;
  cout << cnt << endl;
  return 0;
}
// templete by private_yusuke

Error: TLE

Fixed submission:

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define RFOR(i, j, n) for (int i = j - 1; i >= n; i--)
#define PREC(n) fixed << setprecision(n)
#define print_array(v)                                                         \\
  REP(__k, v.size()) {                                                         \\
    cout << v[__k];                                                            \\
    if (__k != v.size() - 1)                                                   \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define _LIBCPP_DEBUG 0
using namespace std;
int64_t MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m, cnt = 0;
  cin >> n >> m;

  vector<int> h(n); // 高さ
  REP(i, n) { cin >> h[i]; /*cout << h[i] << " ";*/ }
  // cout << endl;

  vector<pair<int, int>> r(m);
  REP(j, m) {
    cin >> r[j].first >> r[j].second; // 道
    r[j].first--;
    r[j].second--;
    // cout << r[j].first << " " << r[j].second << endl;
  }

  REP(i, n) {
    bool flag = true;
    REP(j, m) {
      if (r[j].first == i) {
        if (h[i] <= h[r[j].second])
          flag = false;
      }
      if (r[j].second == i) {
        if (h[i] <= h[r[j].first])
          flag = false;
      }
    }
    if (flag)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
// templete by private_yusuke