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

#pragma GCC optimize(                                                          \\
    "Ofast") // Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define endl "\\n"
#define pb push_back
#define sym(s) s = "#" + s + "#";
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\\n";
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin());

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const ll MOD = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll N = 3e+5 + 7;

vll adj[N];
ll vis[N] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, k;
ll l[15], r[15];
ll dp[N], pre[N];
//<<-----Implement Functions Here---->>//
ll add(ll a, ll b) { return (a % MOD + b % MOD) % MOD; }
ll mult(ll a, ll b) { return (a % MOD * b % MOD) % MOD; }
ll subt(ll a, ll b) { return (a % MOD - b % MOD + MOD) % MOD; }

//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> k;
  for (ll i = 1; i <= k; i++)
    cin >> l[i] >> r[i];
  dp[0] = 0;
  dp[1] = 1;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= k; j++) {
      ll l1 = max(i - l[j], ll(0));
      ll r1 = max(i - r[j], ll(1));
      dp[i] = add(dp[i], subt(pre[l1], pre[r1 - 1]));
    }
    pre[i] = add(pre[i], add(pre[i - 1], dp[i]));
    // for(ll i=1;i<=n;i++)cout<<dp[i]<<" ";cout<<endl;
    // for(ll i=1;i<=n;i++)cout<<pre[i]<<" ";cout<<endl;
  }

  cout << dp[n] << endl;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }

#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
}

Error: TLE

Fixed submission:

#pragma GCC optimize(                                                          \\
    "Ofast") // Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define endl "\\n"
#define pb push_back
#define sym(s) s = "#" + s + "#";
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\\n";
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin());

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const ll MOD = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll N = 3e+5 + 7;

vll adj[N];
ll vis[N] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, k;
ll pre[N];
vll v;
ll dp[N];
//<<-----Implement Functions Here---->>//
ll func(ll ind) {
  if (ind == n)
    return 1;

  if (ind >= n + 1)
    return 0;

  if (dp[ind] != -1)
    return dp[ind];

  ll ans = 0;
  for (auto it : v)
    ans += func(ind + it);
  return dp[ind] = ans;
}

//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> k;
  for (ll i = 0; i <= n + 1; i++)
    dp[i] = -1;
  for (ll i = 1; i <= k; i++) {
    ll l, r;
    cin >> l >> r;
    pre[l]++;
    pre[r + 1]--;
  }
  for (ll i = 1; i <= n; i++)
    pre[i] += pre[i - 1];
  for (ll i = 1; i <= n; i++)
    if (pre[i] >= 1)
      v.pb(i);
  cout << func(1) % MOD;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }

#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
}