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++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MOD = 998244353;

int n, k, l, r;
vector<pii> vec;
ll dp[200001];

struct segtree {
  int size;
  vector<ll> sums;

  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;
    sums.assign(2 * size, 0LL);
  }

  void build(vector<ll> &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < (int)a.size())
        sums[x] = a[lx];
      return;
    }
    int m = (lx + rx) / 2;
    build(a, 2 * x + 1, lx, m);
    build(a, 2 * x + 2, m, rx);
    sums[x] = sums[2 * x + 1] + sums[2 * x + 2];
  }

  void build(vector<ll> &a) { build(a, 0, 0, size); }

  void set(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      sums[x] = v;
      return;
    }
    int m = (lx + rx) / 2;
    if (i < m) {
      set(i, v, 2 * x + 1, lx, m);
    } else {
      set(i, v, 2 * x + 2, m, rx);
    }
    sums[x] = sums[2 * x + 1] + sums[2 * x + 2];
  }

  void set(int i, int v) { set(i, v, 0, 0, size); }

  ll sum(int l, int r, int x, int lx, int rx) {
    if (lx >= l && rx <= r)
      return sums[x];
    if (lx >= r || rx <= l)
      return 0;
    int m = (lx + rx) / 2;
    ll s1 = sum(l, r, 2 * x + 1, lx, m);
    ll s2 = sum(l, r, 2 * x + 2, m, rx);
    return s1 + s2;
  }

  ll sum(int l, int r) { return sum(l, r, 0, 0, size); }
};

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin.sync_with_stdio(0);
  cin.tie();
  cin.exceptions(cin.failbit);

  ll n, k, l, r;
  cin >> n >> k;
  vector<ll> dp(n + 1);
  dp[n] = 1;
  segtree st;
  st.init(n + 1);
  st.build(dp);

  rep(i, 1, k) {
    cin >> l >> r;
    vec.pb({l, r});
  }

  for (int i = n - 1; i >= 1; i--) {
    ll ans = 0;
    trav(s, vec) {
      if (i + s.first <= n) {
        ans = (ans +
               st.sum(i + s.first, min(n + 1, (ll)i + s.second + 1)) % MOD) %
              MOD;
      }
    }
    ans %= MOD;
    dp[i] = ans;
    st.set(i, ans);
  }

  cout << dp[1];
}

Error: TLE

Fixed submission:

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

#define rep(i, a, b) for (int i = a; i <= b; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MOD = 998244353;

int n, k, l, r;
vector<pii> seg;
ll dp[200001];

ll calc(int x) {
  if (x > n)
    return 0;
  if (x == n)
    return 1;
  if (dp[x] != -1)
    return dp[x];

  ll ans = 0;
  bool bad = false;
  trav(s, seg) {
    rep(i, s.first, s.second) {
      if (x + i > n)
        break;
      ans = (ans + calc(x + i)) % MOD;
    }
    if (bad)
      break;
  }
  return dp[x] = ans % MOD;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin.sync_with_stdio(0);
  cin.tie();
  cin.exceptions(cin.failbit);

  cin >> n >> k;
  rep(i, 1, k) {
    cin >> l >> r;
    seg.pb({l, r});
  }
  sort(all(seg));

  rep(i, 1, n) dp[i] = -1;
  cout << calc(1);
}