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

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define Mid ((L + R) / 2)
#define pi pair<int, int>
#define pii pair<int, pi>
#define deb(x) cout << #x << "=" << x << endl
#define go                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const int N = 2e5 + 7;
int dp[5 * N], lazy[5 * N], n, k, a[20], b[20];
int mod = 998244353;
int sum(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
void Lazy(int p, int L, int R) {
  dp[p] = sum(dp[p], lazy[p]);
  if (L != R) {
    lazy[2 * p] = sum(lazy[p], lazy[2 * p]);
    lazy[2 * p + 1] = sum(lazy[p], lazy[2 * p + 1]);
  }
  lazy[p] = 0;
  return;
}
int get(int i, int p = 1, int L = 1, int R = n) {
  Lazy(p, L, R);
  if (L == R)
    return dp[p];
  if (i <= Mid)
    return get(i, 2 * p, L, Mid);
  return get(i, 2 * p + 1, Mid + 1, R);
}
void upd(int l, int r, int x, int p = 1, int L = 1, int R = n) {
  Lazy(p, L, R);
  if (l > R || r < L)
    return;
  if (L >= l && R <= r) {
    lazy[p] = x;
    Lazy(p, L, R);
    return;
  }
  upd(l, r, x, 2 * p, L, Mid);
  upd(l, r, x, 2 * p + 1, Mid + 1, R);
  return;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < k; i++)
    scanf("%d %d", a + i, b + i);
  upd(1, 1, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int L = i + a[j];
      int R = i + b[j];
      if (L > n)
        continue;
      R = min(R, n);
      upd(L, R, get(i));
    }
  }
  printf("%d", get(n));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define Mid ((L + R) / 2)
#define pi pair<int, int>
#define pii pair<int, pi>
#define deb(x) cout << #x << "=" << x << endl
#define go                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const int N = 2e5 + 7;
int dp[5 * N], lazy[5 * N], n, k, a[20], b[20];
int mod = 998244353;
int sum(int x, int y) {
  x %= mod;
  y %= mod;
  return (x + y) % mod;
}
void Lazy(int p, int L, int R) {
  dp[p] = sum(dp[p], lazy[p]);
  if (L != R) {
    lazy[2 * p] = sum(lazy[p], lazy[2 * p]);
    lazy[2 * p + 1] = sum(lazy[p], lazy[2 * p + 1]);
  }
  lazy[p] = 0;
  return;
}
int get(int i, int p = 1, int L = 1, int R = n) {
  Lazy(p, L, R);
  if (L == R)
    return dp[p];
  if (i <= Mid)
    return get(i, 2 * p, L, Mid);
  return get(i, 2 * p + 1, Mid + 1, R);
}
void upd(int l, int r, int x, int p = 1, int L = 1, int R = n) {
  Lazy(p, L, R);
  if (l > R || r < L)
    return;
  if (L >= l && R <= r) {
    lazy[p] = x;
    Lazy(p, L, R);
    return;
  }
  upd(l, r, x, 2 * p, L, Mid);
  upd(l, r, x, 2 * p + 1, Mid + 1, R);
  return;
}
int main() {
  go;
  cin >> n >> k;
  for (int i = 0; i < k; i++)
    cin >> a[i] >> b[i];
  upd(1, 1, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int L = i + a[j];
      int R = i + b[j];
      if (L > n)
        continue;
      R = min(R, n);
      upd(L, R, get(i));
    }
  }
  cout << get(n);
  return 0;
}