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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e5 + 5;
const ll maxm = 1e3 + 5;
template <class T> inline T qread(T &IEE) {
  register T x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return IEE = x * f;
}
template <class T> inline void qwrite(T a) {
  if (!a) {
    putchar('0');
    return;
  }
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    qwrite(a / 10);
  putchar(a % 10 + 48);
  return;
}
template <class T> inline T ab(T a) { return (a > 0) ? a : -a; }
template <class T> inline void swa(T &a, T &b) {
  a ^= b ^= a ^= b;
  return;
}
ll cnt, x, y, n, m;
ll h[maxn];
bool f[maxn];
int main() {
  //	freopen("1.in", "r", stdin);
  //	freopen("1.out", "w", stdout);
  qread(n);
  qread(m);
  for (int i = 1; i <= n; i++) {
    qread(h[i]);
  }
  for (int i = 1; i <= m; i++) {
    qread(x);
    qread(y);
    if (h[x] <= h[y]) {
      f[x] = 1;
    }
    if (h[y] <= h[x]) {
      f[y] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!f[i]) {
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e5 + 5;
const ll maxm = 1e3 + 5;
template <class T> inline T qread(T &IEE) {
  register T x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return IEE = x * f;
}
template <class T> inline void qwrite(T a) {
  if (!a) {
    putchar('0');
    return;
  }
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    qwrite(a / 10);
  putchar(a % 10 + 48);
  return;
}
template <class T> inline T ab(T a) { return (a > 0) ? a : -a; }
template <class T> inline void swa(T &a, T &b) {
  a ^= b ^= a ^= b;
  return;
}
ll cur, x, y, n, m;
ll head[maxn], nex[maxn], to[maxn], h[maxn];
void add(ll u, ll v) {
  nex[++cur] = head[u];
  to[cur] = v;
  head[u] = cur;
  return;
}
int main() {
  //	freopen("1.in", "r", stdin);
  //	freopen("1.out", "w", stdout);
  qread(n);
  qread(m);
  for (int i = 1; i <= n; i++) {
    qread(h[i]);
  }
  for (int i = 1; i <= m; i++) {
    qread(x);
    qread(y);
    add(x, y);
    add(y, x);
  }
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    bool f = 0;
    for (int j = head[i]; j; j = nex[j]) {
      if (h[i] <= h[to[j]]) {
        f = 1;
        break;
      }
    }
    if (!f)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}