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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

void solve(long long N, long long M, std::vector<long long> H,
           std::vector<long long> A, std::vector<long long> B) {
  vector<pair<ll, ll>> path(M);
  REP(i, M) { path[i] = {min(A[i] - 1, B[i] - 1), max(A[i] - 1, B[i] - 1)}; }
  sort(ALL(path));
  vector<bool> good(N, true);
  REP(i, M) {
    ll a = path[i].first;
    ll b = path[i].second;
    if (H[a] <= H[b]) {
      good[a] = false;
    }
    if (H[a] >= H[b]) {
      good[b] = false;
    }
  }
  int count = 0;
  REP(i, N) {
    if (good[i]) {
      count++;
    }
  }
  cout << count << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> H(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &H[i]);
  }
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(H), std::move(A), std::move(B));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

void solve(long long N, long long M, std::vector<long long> H,
           std::vector<long long> A, std::vector<long long> B) {
  vector<pair<ll, ll>> tower(N);
  REP(i, N) { tower[i] = {H[i], i}; }
  sort(ALL(tower));
  reverse(ALL(tower));
  vector<bool> good(N, true);
  REP(i, N) {
    ll h = tower[i].first;
    ll x = tower[i].second;
    if (good[x]) {
      REP(j, M) {
        if (x == B[j] - 1) {
          swap(B[j], A[j]);
        }
        if (x == A[j] - 1) {
          if (H[B[j] - 1] >= h) {
            good[x] = false;
            break;
          }
          if (H[B[j] - 1] <= h) {
            good[B[j] - 1] = false;
          }
        }
      }
    }
  }
  int count = 0;
  REP(i, N) {
    if (good[i]) {
      count++;
    }
  }
  cout << count << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> H(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &H[i]);
  }
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(H), std::move(A), std::move(B));
  return 0;
}