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

#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type)                                          \\
  memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end)                                    \\
  fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end() {
#ifdef ONLINE
  printf("\\n\\nTime used: %.6lf(s)\\n", ((double)TIME__END - TIME__START) / 1000);
  system("pause");
#endif
}
int n, k;
ll dp[N], sum[N];
int l[N], r[N];

inline void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; ++i)
    scanf("%d%d", &l[i], &r[i]);
  dp[1] = sum[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j)
      dp[i] += (sum[max(0, i - l[j])] - sum[max(0, i - r[j] - 1)]) % mod,
          dp[i] %= mod, dp[i] += mod, dp[i] %= mod;
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
}

int main() {
  TIME__START = clock();
  int Test = 1;
  // scanf("%d", &Test);
  while (Test--) {
    solve();
    // if (Test)
    //     putchar('\\n');
  }
  TIME__END = clock();
  program_end();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type)                                          \\
  memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end)                                    \\
  fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end() {
#ifdef ONLINE
  printf("\\n\\nTime used: %.6lf(s)\\n", ((double)TIME__END - TIME__START) / 1000);
  system("pause");
#endif
}
vector<int> S;
int n, k;
ll dp[N];

inline void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    for (int j = l; j <= r; ++j)
      S.push_back(j);
  }
  vecupsort(S);
  S.resize(unique(vecall(S)) - S.begin());
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (auto j : S) {
      if (i - j <= 0)
        break;
      dp[i] += dp[i - j], dp[i] %= mod;
    }
  }
  cout << dp[n];
}

int main() {
  TIME__START = clock();
  int Test = 1;
  // scanf("%d", &Test);
  while (Test--) {
    solve();
    // if (Test)
    //     putchar('\\n');
  }
  TIME__END = clock();
  program_end();
  return 0;
}