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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// MACROS
#define _crt_secure_no_warnings
#define FREEELO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F3F3F3F3FLL
#define MAX 200001
#define MOD 998244353
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
struct node {
  ll sum, add;
  node() { add = 0; }
} st[4 * MAX];
ll dp[MAX];
void Build(ll v, ll l, ll r) {
  if (l == r) {
    st[v].sum = dp[l];
    st[v].add = 0;
    return;
  }
  ll mid = (l + r) / 2;
  Build(2 * v, l, mid);
  Build(2 * v + 1, mid + 1, r);
  st[v].sum = st[2 * v].sum + st[2 * v + 1].sum;
  st[v].add = 0;
}

void Push(ll v, ll l, ll mid, ll r) {
  if (st[v].add != 0) {
    st[2 * v].add += st[v].add % MOD;
    st[2 * v + 1].add += st[v].add % MOD;
    st[2 * v].sum += ((mid - l + 1) * st[v].add) % MOD;
    st[2 * v + 1].sum += ((r - mid) * st[v].add) % MOD;
    st[v].add = 0;
  }
}

void Update(ll v, ll l, ll r, ll i, ll j, ll d) {
  if (i > j)
    return;
  if (l == i && r == j) {
    st[v].add = (st[v].add + d) % MOD;
    st[v].sum = (st[v].sum + 1LL * (j - i + 1) * d % MOD) % MOD;
    return;
  }
  ll mid = (l + r) / 2;
  Push(v, l, mid, r);
  Update(2 * v, l, mid, i, min(mid, j), d);
  Update(2 * v + 1, mid + 1, r, max(mid + 1, i), j, d);
  st[v].sum = (st[2 * v].sum % MOD + st[2 * v + 1].sum % MOD) % MOD;
}

ll Sum(ll v, ll l, ll r, ll i, ll j) {
  if (i > j)
    return 0;
  if (l == i && r == j)
    return st[v].sum;
  ll mid = (l + r) / 2;
  Push(v, l, mid, r);
  return (Sum(2 * v, l, mid, i, min(mid, j)) % MOD +
          Sum(2 * v + 1, mid + 1, r, max(i, mid + 1), j) % MOD) %
         MOD;
}

umii m;
int main() {
  FREEELO
  int n, k;
  cin >> n >> k;
  dp[1] = 1;
  vector<ii> steps(k + 1);
  loop(i, k) { cin >> steps[i].F >> steps[i].S; }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  Build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < sz(steps); j++) {
      int k1 = i + steps[j].F;
      if (k1 > n)
        continue;
      int k2 = i + steps[j].S;
      if (k2 > n)
        k2 = n;
      Update(1, 1, n, k1, k2, Sum(1, 1, n, i, i));
    }
  }
  cout << Sum(1, 1, n, n, n) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// MACROS
#define _crt_secure_no_warnings
#define FREEELO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 200001
#define MOD 998244353
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
ll dp[MAX];
umii m;
int main() {
  FREEELO
  int n, k;
  cin >> n >> k;
  dp[1] = 1;
  vector<ii> steps(k + 1);
  loop(i, k) { cin >> steps[i].F >> steps[i].S; }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < sz(steps); j++) {
      for (int k = steps[j].F; k <= steps[j].S; k++) {
        if (i - k < 0)
          break;
        dp[i] = (dp[i] + dp[i - k]) % MOD;
      }
    }
  }
  cout << dp[n] << endl;
}