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

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

#define rep(i, a, b) for (int i = (a); i < (b); i++)

using ll = long long;
const ll mod = 998244353;

ll inv2;
ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  return modpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod;
}

struct SegTree {
  int n;
  vector<ll> node, lazy;

  SegTree(int sz) {
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1);
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      (node[k] += lazy[k]) %= mod;

      if (r - l > 1) {
        (lazy[2 * k + 1] += lazy[k] * inv2 % mod) %= mod;
        (lazy[2 * k + 2] += lazy[k] * inv2 % mod) %= mod;
      }

      lazy[k] = 0;
    }
  }

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

    if (b <= l or r <= a)
      return;
    if (a <= l and r <= b) {
      (lazy[k] += (r - l) * x % mod) %= mod;
      eval(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 get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l or r <= a)
      return 0;

    eval(k, l, r);
    if (a <= l and r <= b)
      return node[k];
    ll vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
    return (vl + vr) % mod;
  }
};

int main() {
  inv2 = modpow(2, mod - 2);

  int n, k;
  cin >> n >> k;

  vector<int> l(n), r(n);
  rep(i, 0, k) cin >> l[i] >> r[i];

  SegTree st(n);
  st.add(0, 1, 1);
  rep(i, 0, n) {
    rep(j, 0, k) { st.add(i + l[j], min(n, i + r[j] + 1), st.get(i, i + 1)); }
  }

  cout << st.get(n - 1, n) << endl;

  // rep(i, 0, n) cout << st.get(i, i + 1) << " ";
  // // cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, a, b) for (int i = (a); i < (b); i++)

using ll = long long;
const ll mod = 998244353;

ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  return modpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod;
}

struct SegTree {
  int n;
  vector<ll> node, lazy;

  SegTree(int sz) {
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1);
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      (node[k] += lazy[k]) %= mod;

      if (r - l > 1) {
        (lazy[2 * k + 1] += lazy[k] * modpow(2, mod - 2) % mod) %= mod;
        (lazy[2 * k + 2] += lazy[k] * modpow(2, mod - 2) % mod) %= mod;
      }

      lazy[k] = 0;
    }
  }

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

    if (b <= l or r <= a)
      return;
    if (a <= l and r <= b) {
      (lazy[k] += (r - l) * x % mod) %= mod;
      eval(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 get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l or r <= a)
      return 0;

    eval(k, l, r);
    if (a <= l and r <= b)
      return node[k];
    ll vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
    return (vl + vr) % mod;
  }
};

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> l(n), r(n);
  rep(i, 0, k) cin >> l[i] >> r[i];

  SegTree st(n);
  st.add(0, 1, 1);
  rep(i, 0, n) {
    rep(j, 0, k) { st.add(i + l[j], min(n, i + r[j] + 1), st.get(i, i + 1)); }
  }

  cout << st.get(n - 1, n) << endl;

  // rep(i, 0, n) cout << st.get(i, i + 1) << " ";
  // // cout << endl;

  return 0;
}