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

#include <bits/stdc++.h>
#include <chrono>
using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define ALL(a) (a).begin(), (a).end()
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
LL dp[400003];
LL sum[200003];
LL mod = 998244353;
static const int MAX_SIZE = 1 << 20;

LL all[2 * MAX_SIZE - 1], part[2 * MAX_SIZE - 1];

void add(int a, int b, LL x, int k = 0, int l = 0, int r = MAX_SIZE) {
  if (a <= l && r <= b) {
    all[k] += x;
  } else if (l < b && a < r) {
    part[k] += (min(b, r) - max(a, l)) * x;
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
}
LL sum2(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE) {
  if (b <= l || r <= a) {
    return (0);
  } else if (a <= l && r <= b) {
    return (all[k] * (r - l) + part[k]);
  } else {
    LL res = (min(b, r) - max(a, l)) * all[k];
    res += sum2(a, b, k * 2 + 1, l, (l + r) / 2);
    res += sum2(a, b, k * 2 + 2, (l + r) / 2, r);
    return (res);
  }
}
int main() {
  CLR(all);
  CLR(part);
  int N, K;
  cin >> N >> K;
  vector<PII> pi;
  REP(i, K) {
    int L, R;
    cin >> L >> R;
    pi.PB(MP(L, R));
  }
  CLR(dp);
  // dp[0]=1;
  add(1, 2, 1);
  FOR(i, 1, N + 1) {
    REP(k, SZ(pi)) {
      int s = pi[k].first;
      int e = pi[k].second;
      LL addval = sum2(i, i + 1) % mod;
      add(i + s, i + e + 1, addval);
    }
  }
  cout << sum2(N, N + 1) % mod << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define ALL(a) (a).begin(), (a).end()
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
LL dp[200003];
LL mod = 998244353;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> S;
  REP(i, K) {
    int L, R;
    cin >> L >> R;
    FOR(ii, L, R + 1) S.PB(ii);
  }
  SORT(S);
  CLR(dp);
  dp[1] = 1;
  FOR(i, 1, N + 1) {
    REP(k, SZ(S)) if (i >= S[k]) { dp[i] = (dp[i] + dp[i - S[k]]) % mod; }
  }
  cout << dp[N] % mod << endl;
  return 0;
}