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

#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define all(vec) vec.begin(), vec.end()
#define ff first
#define ss second
#define pi 3.141592653589
#define inf 4e18
#define MOD 998244353
#define pb push_back
#define endl "\\n"
#define for1(low, high, k) for (int i = low; i < high; i += k)
#define for2(low, high, k) for (int j = low; j < high; j += k)
#define rev(high, low, k) for (int i = high; i >= low; i -= k)

lli T[800004];
lli S[800004];
lli E[800004];
lli a[200004];

void build(int node, int low, int high) {
  S[node] = low;
  E[node] = high;
  if (low == high) {
    T[node] = a[low] % MOD;
    return;
  } else {
    int mid = low + (high - low) / 2;
    build(2 * node, low, mid);
    build(2 * node + 1, mid + 1, high);
    T[node] = (T[2 * node] % MOD + T[2 * node + 1] % MOD) % MOD;
  }
  return;
}

void update(int node, int index, int value) {
  if (S[node] == E[node]) {
    T[node] = value % MOD;
    a[index] = value % MOD;
    return;
  } else {
    if (index <= (S[node] + E[node]) / 2)
      update(2 * node, index, value);
    else
      update(2 * node + 1, index, value);
    T[node] = (T[2 * node] % MOD + T[2 * node + 1] % MOD) % MOD;
  }
  return;
}

lli query(int node, int l, int r) {
  if (S[node] > r || E[node] < l)
    return 0;
  else if (S[node] >= l && E[node] <= r)
    return T[node];
  else {
    lli left = query(2 * node, l, r);
    lli right = query(2 * node + 1, l, r);
    return (left + right) % MOD;
  }
  return 0;
}

int main() {
  fast;
  lli n, k, x, y;
  cin >> n >> k;
  vector<pii> uni(k);
  for (int i = 0; i < k; i++) {
    cin >> uni[i].ff >> uni[i].ss;
  }
  sort(all(uni));
  a[n] = 1;
  build(1, 1, n);
  for (int i = n - 1; i >= 1; i--) {
    int val = 0;
    for (int j = 0; j < k; j++) {
      if (i + uni[j].ff <= n) {
        val =
            (val % MOD + query(1, i + uni[j].ff, min(i + uni[j].ss, n))) % MOD;
      } else
        break;
    }
    update(1, i, val);
  }
  cout << a[1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define all(vec) vec.begin(), vec.end()
#define ff first
#define ss second
#define pi 3.141592653589
#define inf 4e18
#define MOD 998244353
#define pb push_back
#define endl "\\n"
#define for1(low, high, k) for (int i = low; i < high; i += k)
#define for2(low, high, k) for (int j = low; j < high; j += k)
#define rev(high, low, k) for (int i = high; i >= low; i -= k)

int main() {
  fast;
  lli n, k, x, y;
  cin >> n >> k;
  set<int> s;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    for (int j = x; j <= y; j++)
      s.insert(j);
  }
  vector<lli> dp(n + 1, 0);
  dp[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    for (auto j : s) {
      if (i + j <= n)
        dp[i] = (dp[i] % MOD + dp[i + j] % MOD) % MOD;
      else
        break;
    }
  }
  cout << dp[1] % MOD << endl;
}