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

#define errOut(s)                                                              \\
  { std::cerr << __LINE__ << ":" << #s << ":" << (s) << std::endl; }
#define errOutSeparator(s)                                                     \\
  { std::cerr << __LINE__ << ":" << #s << ":" << (s) << "======" << std::endl; }

#include <bits/stdc++.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
// #include <boost/multi_array.hpp>
// #include <boost/optional.hpp>
// #include <boost/range/adaptors.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/range/irange.hpp>
#pragma clang diagnostic pop

using namespace std;
using range_t = struct {
  int64_t l;
  int64_t r;
};
std::ostream &operator<<(std::ostream &os, const range_t &r) {
  os << "Range L:" << r.l << " R:" << r.r << ";";
  return os;
}

static constexpr int64_t MaxN = 200000;
static constexpr int64_t MaxK = 10;
static constexpr int64_t MOD = 998244353;
static std::vector<range_t> rgs;
static std::array<int64_t, MaxN> diffs;
static std::array<int64_t, MaxN> values;
static int64_t n;

int64_t solve(void) {
  std::sort(rgs.begin(), rgs.end(),
            [](auto const &lhv, auto const &rhv) { return lhv.l < rhv.l; });
  values[0] = 1;
  diffs[0] = 1;
  diffs[1] = -1;
  for (auto i = 0; i < n; i++) {
    if (i > 0) {
      values[i] = values[i - 1] + diffs[i];
      values[i] %= MOD;
    }
    for (auto const &rg : rgs) {
      if (i + rg.l >= n) {
        break;
      }
      diffs[i + rg.l] += values[i];
      diffs[i + rg.l] %= MOD;
      if (i + rg.r + 1 >= n) {
        break;
      }
      diffs[i + rg.r + 1] -= values[i];
      if (diffs[i + rg.r + 1] < 0) {
        diffs[i + rg.r + 1] += MOD;
      } else {
        diffs[i + rg.r + 1] %= MOD;
      }
    }
  }
  return values[n - 1];
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cin >> n;
  int64_t k;
  std::cin >> k;
  rgs.resize(k);
  for (int i = 0; i < k; i++) {
    std::cin >> rgs[i].l;
    std::cin >> rgs[i].r;
  }
  auto const res = solve();
  std::cout << res << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#define errOut(s)                                                              \\
  { std::cerr << __LINE__ << ":" << #s << ":" << (s) << std::endl; }
#define errOutSeparator(s)                                                     \\
  { std::cerr << __LINE__ << ":" << #s << ":" << (s) << "======" << std::endl; }

#include <bits/stdc++.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
// #include <boost/multi_array.hpp>
// #include <boost/optional.hpp>
// #include <boost/range/adaptors.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/range/irange.hpp>
#pragma clang diagnostic pop

using namespace std;
using range_t = struct {
  int64_t l;
  int64_t r;
};
std::ostream &operator<<(std::ostream &os, const range_t &r) {
  os << "Range L:" << r.l << " R:" << r.r << ";";
  return os;
}

using reachable_t = struct {
  int64_t r;
  int64_t v;
};
std::ostream &operator<<(std::ostream &os, const reachable_t &r) {
  os << "Reachable R:" << r.r << " V:" << r.v << ";";
  return os;
}

static constexpr int64_t MaxN = 200000;
static constexpr int64_t MaxK = 10;
static constexpr int64_t MOD = 998244353;
static std::vector<range_t> rgs;
static std::array<reachable_t, MaxN> reachables;
static int64_t n;

void addToReachable(int64_t l, int64_t r, int64_t v) {
  r = std::min(r, n - 1);
  if (l > r) {
    return;
  }
  if (reachables[l].v == 0) {
    reachables[l].r = r;
    reachables[l].v = v;
    return;
  }
  if (reachables[l].r == r) {
    reachables[l].v += v;
    reachables[l].v %= MOD;
    return;
  }

  if (reachables[l].r > r) {
    addToReachable(r + 1, reachables[l].r, reachables[l].v);
    reachables[l].r = r;
    reachables[l].v += v;
    return;
  }

  addToReachable(reachables[l].r + 1, r, v);
  reachables[l].v += v;
  reachables[l].v %= MOD;
}

int64_t solve(void) {
  std::sort(rgs.begin(), rgs.end(),
            [](auto const &lhv, auto const &rhv) { return lhv.l < rhv.l; });
  reachables[0].r = 0;
  reachables[0].v = 1;
  for (auto i = 0; i < n - 1; i++) {
    auto const &r = reachables[i];
    if (r.v == 0) {
      continue;
    }
    if (r.r != i) {
      addToReachable(i + 1, r.r, r.v);
    }
    for (auto const &rg : rgs) {
      if (i + rg.l >= n) {
        break;
      }
      addToReachable(i + rg.l, i + rg.r, r.v);
    }
  }
  return reachables[n - 1].v;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cin >> n;
  int64_t k;
  std::cin >> k;
  rgs.resize(k);
  for (int i = 0; i < k; i++) {
    std::cin >> rgs[i].l;
    std::cin >> rgs[i].r;
  }
  auto const res = solve();
  std::cout << res << std::endl;
  return 0;
}