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

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

#define timesaver                                                              \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define F first
#define S second
#define nl '\\n'

#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mem(a, val) memset(a, val, sizeof(a))
#define deci(x) cout << fixed << setprecision(x);
#define bitcount(x) __builtin_popcountll(x)

const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

const int MAX = 2 * 1000 * 100 + 10;
const ll INF = 1e18;
const int MOD = 998244353;

vector<ll> jumps;
ll ar[MAX], dp[MAX], n, k;

ll BIT[MAX];

void update(ll pos, ll val) {
  while (pos > 0 and pos < MAX) {
    BIT[pos] += val;
    if (BIT[pos] >= MOD)
      BIT[pos] %= MOD;
    pos += (pos & -pos);
  }
}

ll get(ll pos) {
  ll ans = 0;
  while (pos > 0) {
    ans += BIT[pos];
    if (ans >= MOD)
      ans %= MOD;
    pos -= (pos & -pos);
  }
  return ans;
}

void sol() {
  cin >> n >> k;
  vector<pair<ll, ll>> v(k);
  for (auto &i : v)
    cin >> i.F >> i.S;
  update(1, 1);
  for (ll i = 2; i <= n; i++) {
    ll tmp = 0;
    for (auto k : v) {
      ll r = i - k.F, l = i - k.S;
      ll ans = (get(r) - get(l - 1) + MOD) % MOD;
      tmp += ans;
      if (tmp >= MOD)
        tmp %= MOD;
    }
    update(i, tmp);
  }
  cout << (get(n) - get(n - 1) + MOD) % MOD << nl;
}

signed main() {
  timesaver;
  //  int t = 1 ; cin >> t ; while(t--)
  { sol(); }
}

Error: TLE

Fixed submission:

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

#define timesaver                                                              \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ldb;

#define mp make_pair
#define pb push_back
#define F first
#define S second
#define nl '\\n'

#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mem(a, val) memset(a, val, sizeof(a))
#define deci(x) cout << fixed << setprecision(x);
#define bitcount(x) __builtin_popcountll(x)

const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

const int MAX = 2 * 1000 * 100 + 10;
const ll INF = 1e18;
const int MOD = 998244353;

vector<ll> jumps;
ll ar[MAX], dp[MAX], n, k;

ll sol(ll pos) {
  if (!pos)
    return 1;
  ll &ans = dp[pos];
  if (ans != -1)
    return ans;
  ans = 0;
  for (ll i : jumps) {
    if (i > pos)
      break;
    ans += sol(pos - i);
    if (ans >= MOD)
      ans %= MOD;
  }
  return ans;
}

void sol() {
  cin >> n >> k;
  for (ll i = 0; i < k; i++) {
    ll l, r;
    cin >> l >> r;
    ar[l]++, ar[r + 1]--;
  }
  for (ll i = 1; i < MAX; i++)
    ar[i] += ar[i - 1];
  for (ll i = 0; i < MAX; i++)
    if (ar[i])
      jumps.pb(i);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    if (!dp[i])
      continue;
    for (int j : jumps) {
      dp[i + j] += dp[i];
      if (dp[i + j] >= MOD)
        dp[i + j] %= MOD;
    }
  }
  cout << dp[n - 1] << nl;
}

signed main() {
  timesaver;
  //  int t = 1 ; cin >> t ; while(t--)
  { sol(); }
}