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

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MODADD(ADD_X, ADD_Y) (ADD_X + ADD_Y) % MOD;
#define MODSUB(SUB_X, SUB_Y)  (SUB_X-SUB_Y)+MOD)%MOD;
#define MODMUL(MUL_X, MUL_Y) (MUL_X * MUL_Y) % MOD;
#define LCM(LCM_X, LCM_Y) (LCM_X * LCM_Y) / __gcd(LCM_X, LCM_Y);
typedef long long int ll;
typedef unsigned long long int ull;
const ll MOD = 998244353;
const int MAX = 1e6;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
ll tree[MAX];
ll query(int idx) {
  ll sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= idx & (-idx);
  }
  return sum;
}
void update(int idx, int x,
            int n) // n is the size of the array, x is the number to add
{
  while (idx <= n) {
    tree[idx] += x;
    idx += idx & (-idx);
  }
}
int x[20], y[20];
ll dp[MAX];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int n,
      k;
  cin >> n >> k;
  for (int i = 0; i < k; i++)
    cin >> x[i] >> y[i];
  dp[n] = 1;
  update(n, 1, n);
  for (int i = n - 1; i > 0; i--) {
    for (int j = 0; j < k; j++) {

      int l = i + x[j];
      int r = i + y[j];
      if (l > n)
        continue;
      r = min(r, n);
      ll ways = query(r) - query(l - 1);
      // cout<<i<<" "<<ways<<endl;
      dp[i] = MODADD(dp[i], ways);
    }
    update(i, dp[i], n);
  }
  cout << dp[1] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MODADD(ADD_X, ADD_Y) (ADD_X + ADD_Y) % MOD;
#define MODSUB(SUB_X, SUB_Y)  (SUB_X-SUB_Y)+MOD)%MOD;
#define MODMUL(MUL_X, MUL_Y) (MUL_X * MUL_Y) % MOD;
#define LCM(LCM_X, LCM_Y) (LCM_X * LCM_Y) / __gcd(LCM_X, LCM_Y);
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)998244353;
const int MAX = 1e6;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int arr[MAX], n, k;
ll ans[MAX];
ll dp[MAX];
ll recur(int pos) {
  if (pos == n)
    return 1;
  else if (pos > n)
    return 0;
  ll &ret = dp[pos];
  if (~ret)
    return ret;
  ret = 0;
  for (int i = 1; i + pos <= n; i++) {
    if (!arr[i])
      continue;
    ret += recur(pos + i);
    ret %= MOD;
  }
  return ret;
}
int main() {
  FastRead
          // freopen("output.txt", "w", stdout);
          cin >>
      n >> k;
  while (k--) {
    int l, r;
    cin >> l >> r;
    arr[l]++;
    arr[r + 1]--;
  }
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++)
    arr[i] += arr[i - 1];
  cout << recur(1) << endl;

  return 0;
}