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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (ll i = a; i < b; i++)

ll mod = 998244353;
class SegmentTree {
private:
  int n;
  vector<ll> node, lazy;
  vector<bool> flag_lazy;

public:
  SegmentTree(int N) {
    n = 1;
    while (n < N)
      n *= 2;
    node.resize(2 * n - 1, 0);
    lazy.resize(2 * n - 1, 0);
    flag_lazy.resize(2 * n - 1, false);
  }

  void update(int i, int l, int r) {
    if (!flag_lazy[i])
      return;
    node[i] += lazy[i];
    node[i] %= mod;
    if (r - l > 1) {
      lazy[2 * i + 1] += lazy[i] / 2;
      lazy[2 * i + 2] += lazy[i] / 2;
      flag_lazy[2 * i + 1] = true;
      flag_lazy[2 * i + 2] = true;
    }
    flag_lazy[i] = false;
    lazy[i] = 0;
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    update(k, l, r);
    if (b <= l || r <= a)
      return;

    if (a <= l && r <= b) {
      lazy[k] += ((r - l) * (x));
      flag_lazy[k] = true;
      update(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = (node[2 * k + 1] + node[2 * k + 2]) % mod;
    }
  }

  ll query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    update(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k] % mod;

    ll sum_l = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll sum_r = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return (sum_l + sum_r) % mod;
  }

  void disp() {
    for (int i : node) {
      cout << i << " ";
    }
    cout << "\\n";
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  int L[K], R[K];
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  SegmentTree st(500050);
  st.add(1, 2, 1);

  for (int i = 1; i < N; i++) {
    ll x = st.query(i, i + 1);
    for (int j = 0; j < K; j++) {
      int l = i + L[j];
      int r = i + R[j] + 1;

      // st.add(min(l, N + 1), min(r, N + 1), x);
      st.add(l, r, x);
    }
    // cout << x << "\\n";
  }

  cout << st.query(N, N + 1) << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (ll i = a; i < b; i++)

ll mod = 998244353;
class SegmentTree {
private:
  int n;
  vector<ll> node, lazy;
  vector<bool> flag_lazy;

public:
  SegmentTree(int N) {
    n = 1;
    while (n < N)
      n *= 2;
    node.resize(2 * n - 1, 0);
    lazy.resize(2 * n - 1, 0);
    flag_lazy.resize(2 * n - 1, false);
  }

  void update(int i, int l, int r) {
    if (!flag_lazy[i])
      return;
    node[i] += lazy[i];
    node[i] %= mod;
    if (r - l > 1) {
      lazy[2 * i + 1] += lazy[i] / 2;
      lazy[2 * i + 2] += lazy[i] / 2;
      flag_lazy[2 * i + 1] = true;
      flag_lazy[2 * i + 2] = true;
    }
    lazy[i] = 0;
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    update(k, l, r);
    if (b <= l || r <= a)
      return;

    if (a <= l && r <= b) {
      lazy[k] += ((r - l) * (x));
      flag_lazy[k] = true;
      update(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = (node[2 * k + 1] + node[2 * k + 2]) % mod;
    }
  }

  ll query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    update(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k] % mod;

    ll sum_l = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll sum_r = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return (sum_l + sum_r) % mod;
  }

  void disp() {
    for (int i : node) {
      cout << i << " ";
    }
    cout << "\\n";
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  int L[K], R[K];
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  SegmentTree st(500050);
  st.add(1, 2, 1);

  FOR(i, 1, N) {
    ll x = st.query(i, i + 1);
    for (int j = 0; j < K; j++) {
      int l = i + L[j];
      int r = i + R[j] + 1;

      // st.add(min(l, N + 1), min(r, N + 1), x);
      st.add(l, r, x);
    }
    // cout << x << "\\n";
  }

  cout << st.query(N, N + 1) << "\\n";

  return 0;
}