Problem p03666 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
// -----------------------------------------------------------------------------

typedef pair<LL, LL> P;
typedef vector<P> VT;
const LL INF = 1E9;

struct E {
  LL pos;
  int t;
};

int main() {
  LL n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  VT now;
  now.emplace_back(0, 0);
  FOR(i, 1, n) {
    vector<E> events;
    for (auto &p : now) {
      events.push_back({p.first - d, 1});
      events.push_back({p.second - c, -1});
      events.push_back({p.first + c, 1});
      events.push_back({p.second + d, -1});
    }
    now.clear();
    sort(events.begin(), events.end(), [](const E &a, const E &b) {
      return a.pos < b.pos || (a.pos == b.pos && a.t < b.t);
    });
    LL st, ed;
    for (int i = 0, j, cc = 0; i < (int)events.size(); i = j) {
      if (cc == 0)
        st = events[i].pos;
      for (j = i; events[j].pos == events[i].pos && j < (int)events.size();
           ++j) {
        cc += events[j].t;
        ed = events[j].pos;
      }
      if (cc == 0 && (st < INF && ed > -INF))
        now.emplace_back(st, ed);
    }
  }
  LL x = (b - a);
  for (auto &p : now) {
    if (x <= p.second && x >= p.first) {
      puts("YES");
      return 0;
    }
  }
  puts("NO");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
// -----------------------------------------------------------------------------

typedef pair<LL, LL> P;
typedef vector<P> VT;
const LL INF = 1E9;

struct E {
  LL pos;
  int t;
};

int main() {
  LL n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  n--;
  LL x = b - a;
  FOR(i, 0, n + 1) {
    if (x >= i * c - (n - i) * d && x <= i * d - (n - i) * c) {
      puts("YES");
      return 0;
    }
  }
  puts("NO");
}