alexjercan/bugnet huggingface dataset:
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1000000007;
const ll mod = 998244353;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll l[1000010];
ll r[1000010];

int main() {
  int N, K;
  vector<pair<ll, ll>> v(10);
  cin >> N >> K;
  v.resize(K);
  rep(i, K) {
    ll l, r;
    cin >> l >> r;
    v[i] = make_pair(l, r);
  }

  ll cuml = 0;
  ll cumr = 0;
  for (auto itr : v) {
    l[1 + itr.first] += 1;
    r[1 + itr.second + 1] += 1;
  }

  //    rep(i,N) {cout << l[i+1] - r[i+1] << endl;}

  for (int i = 1; i <= N; i++) {
    cuml += l[i];
    cumr += r[i];
    cuml %= mod;
    cumr %= mod;
    ll tmp;
    if (cuml - cumr < 0)
      tmp = mod + cuml - cumr;
    else
      tmp = cuml - cumr;

    for (auto itr : v) {
      l[i + itr.first] += tmp;
      r[i + itr.second + 1] += tmp;
    }
    ///	printf("i:%d cuml:%lld cumr %lld\\n",i, cuml, cumr);
  }
  ll ans = cuml - cumr;
  if (ans < 0)
    cout << ans + mod << endl;
  else
    cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1000000007;
const ll mod = 998244353;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll sq[1000010];

int main() {
  int N, K;
  set<int> st;
  cin >> N >> K;
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      st.insert(i);
  }

  sq[1] = 1;
  for (int i = 1; i <= N; i++) {
    for (auto itr : st) {
      sq[i + itr] += sq[i];
      sq[i] %= mod;
    }
  }

  cout << sq[N] << endl;

  //    for(auto itr : st) cout << itr << endl;

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
    title: D.cpp
    author: Akhil
    date: 2020-09-19
    time: 17:46:12
*/

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> p(k);
  for (int i = 0; i < k; i++) {
    cin >> p[i].first >> p[i].second;
  }
  sort(p.begin(), p.end());
  vector<int> dp(n + 1, 0), cum_dp(n + 1);
  dp[1] = 1;
  cum_dp[1] = 1;
  auto cum = [&cum_dp](int i, int j) {
    return (cum_dp[j] - ((i >= 1) ? cum_dp[i - 1] : 0) + mod) % mod;
  };
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - p[j].first >= 1) {
        dp[i] = (dp[i] + cum(i - p[j].second, i - p[j].first)) % mod;
      } else
        break;
    }
    cum_dp[i] = (cum_dp[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

/*
    title: D.cpp
    author: Akhil
    date: 2020-09-19
    time: 17:46:12
*/

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> p(k);
  for (int i = 0; i < k; i++) {
    cin >> p[i].first >> p[i].second;
  }
  vector<int> v;
  for (int i = 0; i < k; i++) {
    for (int j = p[i].first; j <= p[i].second; j++) {
      v.push_back(j);
    }
  }
  sort(v.begin(), v.end());
  vector<int> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < v.size(); j++) {
      if (i - v[j] >= 1)
        dp[i] = (dp[i] + dp[i - v[j]]) % mod;
      else
        break;
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>

const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
const int maxm = 100 + 10;
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll dp[maxn];
int vis[maxn];
int n, k;
int main() {
  cin >> n >> k;
  vector<pii> e;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    e.push_back(make_pair(l, r));
  }
  sort(e.begin(), e.end());
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < e.size(); j++) {
      int l = e[j].fi;
      int r = e[j].se;
      if (i - l < 0) {
        break;
      } else if (i - r >= 1)
        dp[i] = (dp[i] + dp[i - l] - dp[i - r - 1] + mod) % mod;
      else
        dp[i] = (dp[i] + dp[i - l]) % mod;
    }
    dp[i] = (dp[i] + dp[i - 1]) % mod;
  }
  cout << (dp[n] - dp[n - 1] + mod) % mod << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>

const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
const int maxm = 100 + 10;
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll dp[maxn];
int vis[maxn];
int n, k;
int main() {
  cin >> n >> k;
  vector<int> e;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      if (!vis[j])
        e.push_back(j);
      vis[j] = 1;
    }
  }
  sort(e.begin(), e.end());
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < e.size(); j++) {
      if (i - e[j] < 1)
        break;
      int t = e[j];
      dp[i] = (dp[i] + dp[i - t]) % mod;
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1001001001;
const ll MX = 1e18;
const int mod = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<P> s(k);
  rep(i, k) cin >> s[i].first >> s[i].second;
  vector<int> dp(2 * n + 5);
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    rep(j, k) {
      dp[i + s[j].first] += dp[i];
      dp[i + s[j].second + 1] -= dp[i];
      dp[i + s[j].first] %= mod;
      dp[i + s[j].second + 1] += mod;
      dp[i + s[j].second + 1] %= mod;
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1001001001;
const ll MX = 1e18;
const int mod = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<int> s;
  rep(ki, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      s.push_back(i);
  }
  sort(s.begin(), s.end());
  vector<int> dp(n + 1);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    rep(j, s.size()) {
      if (i - s[j] < 1)
        break;
      dp[i] += dp[i - s[j]];
      dp[i] %= mod;
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  vector<pair<int, int>> lr(k);
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    lr[i] = {l, r};
  }

  vector<ll> dp(n);
  vector<ll> sdp(n + 1);
  dp[0] = 1;
  sdp[1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int left = max(0, i - lr[j].second);
      int right = max(0, i - lr[j].first + 1);
      dp[i] = (dp[i] + (sdp[right] - sdp[left])) % MOD;
    }
    sdp[i + 1] = sdp[i] + dp[i];
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  set<ll> s;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    while (l <= r) {
      s.insert(l);
      l++;
    }
  }

  vector<ll> ss;
  for (auto x : s)
    ss.push_back(x);
  sort(ss.begin(), ss.end());

  vector<ll> dp(n);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    for (auto x : ss) {
      if (i - x >= 0) {
        dp[i] = (dp[i] + dp[i - x]) % MOD;
      } else
        break;
    }
  }

  cout << dp[n - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

template <typename T> using vec2 = vector<vector<T>>;
template <typename T> using vec3 = vec2<vector<T>>;
template <typename T> using vec4 = vec3<vector<T>>;
template <typename T> using vec5 = vec4<vector<T>>;
template <typename T> using vec6 = vec5<vector<T>>;

ll MOD = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> starts;
  vector<int> ends;
  for (int i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    starts.push_back(l);
    ends.push_back(r);
  }
  vector<ll> dp(N + 1, 0);
  vector<int> ok(N + 1, 0);
  ok[0] = 1;
  dp[0] = 1;
  dp[1] = -1;
  ok[1] = -1;
  for (int i = 0; i < N; i++) {
    if (i != 0)
      ok[i] += ok[i - 1];
    if (i != 0) {
      dp[i] += dp[i - 1];
      dp[i] %= MOD;
    }
    if (ok[i] == 0)
      continue;
    for (int j = 0; j < K; j++) {
      int s = starts[j];
      int e = ends[j];
      if (i + s >= N)
        continue;
      dp[i + s] += dp[i];
      dp[i + s] %= MOD;
      ok[i + s]++;
      ok[min(i + e + 1, N)]--;
      dp[min(i + e + 1, N)] -= dp[i];
      dp[min(i + e + 1, N)] += MOD;
      dp[min(i + e + 1, N)] %= MOD;
    }
  }
  // for(int i = 0;i < N;i++){
  //     cout<<dp[i]<<endl;
  // }
  cout << dp[N - 1] << endl;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

template <typename T> using vec2 = vector<vector<T>>;
template <typename T> using vec3 = vec2<vector<T>>;
template <typename T> using vec4 = vec3<vector<T>>;
template <typename T> using vec5 = vec4<vector<T>>;
template <typename T> using vec6 = vec5<vector<T>>;

ll MOD = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> starts;
  vector<int> ends;
  for (int i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    starts.push_back(l);
    ends.push_back(r);
  }
  vector<ll> dp(N + 1, 0);
  vector<int> ok(N + 1, 0);
  ok[0] = 1;
  dp[0] = 1;
  dp[1] = -1;
  for (int i = 0; i < N; i++) {
    if (i != 0)
      ok[i] += ok[i - 1];
    if (i != 0) {
      dp[i] += dp[i - 1];
      dp[i] %= MOD;
    }
    if (ok[i] == 0)
      continue;
    for (int j = 0; j < K; j++) {
      int s = starts[j];
      int e = ends[j];
      dp[i + s] += dp[i];
      dp[i + s] %= MOD;
      ok[i + s]++;
      ok[min(i + e + 1, N)]--;
      dp[min(i + e + 1, N)] -= dp[i];
      dp[min(i + e + 1, N)] += MOD;
      dp[min(i + e + 1, N)] %= MOD;
    }
  }
  // for(int i = 0;i < N;i++){
  //     cout<<dp[i]<<endl;
  // }
  cout << dp[N - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

int n, k;
const int N = 2e5 + 10, mod = 998244353;
ll a[N][2], dp[N];

void solve() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    cin >> a[i][0] >> a[i][1];
  }
  dp[n] = 1;
  for (int i = n; i >= 1; --i) {
    for (int j = 0; j < k; ++j) {
      // this is standard dp
      // for(int x=a[j][0];x<=a[j][1];++x)
      // if(i-x>0)
      // dp[i-x] += dp[i], dp[i-x]%=mod;
      // observation is : we are incrementing whole range
      // so we can use prefix sums
      int f = a[j][0], s = a[j][1];
      int i0 = i - f, i1 = i - s - 1;
      if (i0 > 0)
        dp[i0] += dp[i] % mod, dp[i0] %= mod;
      if (i1 > 0)
        dp[i1] -= dp[i] % mod, dp[i1] = (dp[i1] + mod) % mod;
    }
    if (i < n)
      dp[i - 1] += dp[i], dp[i - 1] %= mod;
  }
  cout << dp[1] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

int n, k;
const int N = 2e5 + 10, mod = 998244353;
set<pair<int, int>> v;
ll dp[N];

void solve() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    int l, r;
    cin >> l >> r;
    v.insert({l, r});
  }
  dp[n] = 1;
  for (int i = n; i >= 1; i--)
    for (auto j : v)
      for (int x = j.first; x <= j.second; ++x)
        if (i - x >= 1 && dp[i])
          dp[i - x] = (dp[i - x] + dp[i]) % mod;
  cout << dp[1] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--)
    solve();
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod = 998244353;
ll dp[200005];
ll pre[200005];

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> v(k);
  for (int i = 0; i < k; ++i) {
    cin >> v[i].first >> v[i].second;
  }
  memset(pre, 0, sizeof pre);
  memset(dp, 0, sizeof dp);
  dp[1] = 1;
  sort(v.begin(), v.end());
  for (ll i = 2; i <= n; ++i) {
    pre[i - 1] = (pre[i - 2] + dp[i - 1]) % mod;
    for (int j = 0; j < k; j++) {

      ll low = max(0ll, i - (v[j].second + 1));
      ll high = max(0ll, i - (v[j].first));
      dp[i] = (dp[i] + pre[high] - pre[low]) % mod;
    }
    // cout<<dp[i]<<" ";
  }
  cout << (dp[n] + mod) % mod << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod = 998244353;
ll dp[200005];

ll cal(ll n, vector<ll> &v) {
  // cout<<n<<endl;
  ll sum = 0;
  for (auto i : v) {
    if (n - i <= 0)
      continue;
    if (dp[n - i] == -1)
      dp[n - i] = cal(n - i, v);
    sum = (sum + dp[n - i]) % mod;
  }
  return sum;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v;
  for (int i = 0; i < k; ++i) {
    ll u, b;
    cin >> u >> b;
    for (ll j = u; j <= b; j++)
      v.push_back(j);
  }
  memset(dp, -1, sizeof dp);
  dp[1] = 1;
  sort(v.begin(), v.end());
  ll ans = cal(n, v);
  cout << ans << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define ll long long
#define ar array
const ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  ll s[k][2];
  for (int i = 0; i < k; i++) {
    cin >> s[i][0] >> s[i][1];
  }
  vector<ll> p(n + 1, 0);
  p[1] = 1LL;
  for (ll i = 2; i <= n; i++) {
    p[i] += p[i - 1];
    p[i] %= mod;
    for (ll j = 0; j < k; j++) {
      ll x = s[j][0], y = s[j][1];
      if (i - x >= 1)
        p[i] += (p[i - x] - p[i - min(y, i - 1) - 1] + mod) % mod;
      p[i] %= mod;
    }
    p[i] %= mod;
  }
  cout << (p[n] - p[n - 1] + mod) % mod;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define ll long long
#define ar array
const int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  set<int> moves;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    for (int j = x; j <= y; j++) {
      moves.insert(j);
    }
  }
  vector<int> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int move : moves) {
      if (i - move >= 1)
        dp[i] += dp[i - move];
      dp[i] %= mod;
    }
  }
  cout << dp[n];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(3 * n, 0);
  ll wa = 0;

  vector<ll> l(k), r(k);
  rep(i, k) {
    cin >> l[i] >> r[i];
    ++r[i];
  }

  dp[1] = 1;
  dp[2] = -1;
  ll mod = 998244353;

  rep(i, n + 1) {
    wa += dp[i];
    // cout << wa << endl;
    wa = wa % mod;
    rep(j, k) {
      dp[i + l[j]] += wa;
      dp[i + l[j]] = dp[i + l[j]] % mod;

      dp[i + r[j]] -= wa;
      dp[i + r[j]] = dp[i + r[j]] % mod;
    }
  }

  if (wa < 0) {
    wa += mod;
  }

  cout << wa << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(2 * n, 0);
  ll wa = 0;

  vector<ll> l(k), r(k);
  rep(i, k) {
    cin >> l[i] >> r[i];
    ++r[i];
  }

  dp[1] = 1;
  ll mod = 998244353;

  rep(i, n + 1) {
    wa += dp[i];
    wa = wa % mod;
    rep(j, k) {
      dp[i + l[j]] += wa;
      dp[i + l[j]] = dp[i + l[j]] % mod;

      dp[i + r[j]] -= wa;
      dp[i + r[j]] = dp[i + r[j]] % mod;
    }
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 200010;
typedef pair<int, int> pii;
int k;
long long f[N];
vector<int> v;
pair<int, int> q[12];
long long tr[N];
int n;
int lowbit(int x) { return x & -x; }
const int mod = 998244353;
void add(int x, int d) {
  for (int i = x; i <= n; i += lowbit(i))
    tr[i] = (tr[i] + d) % mod;
}

int query(int x) {
  long long res = 0;
  for (int i = x; i; i -= lowbit(i))
    res = (res + tr[i]) % mod;
  return res;
}

int main() {
  // get_prime(1000000);
  cin >> n >> k;
  f[1] = 1;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    q[i] = {l, r};
  }
  sort(q + 1, q + 1 + k);
  // v.push_back(0);
  for (int i = 1; i <= k; i++)
    for (int j = q[i].first; j <= q[i].second; j++)
      v.push_back(j);
  add(1, 1);
  for (int i = 2; i <= n; i++) {
    // int pos=lower_bound(v.begin(),v.end(),i)-v.begin();
    //   cout<<i<<' '<<pos<<' '<<v[pos]<<endl;
    // if(v[0]>i)
    //  continue;
    // if(i-v[pos-1]-1)
    // if(pos==0)  pos++;
    for (int j = 1; j <= k; j++) {
      int rr = i - q[j].first;
      int ll = i - q[j].second;
      if (rr <= 0)
        continue;
      ll = max(ll, 1);
      f[i] = (f[i] + (query(rr) - query(ll - 1)) % mod + mod) % mod;
    }
    add(i, f[i]);
  }
  cout << f[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 200010;
typedef pair<int, int> pii;
int a[N][2];
int cnt;
long long f[N];
vector<int> v;
pair<int, int> q[12];
const int mod = 998244353;
int main() {
  // get_prime(1000000);
  int n, k;
  cin >> n >> k;
  f[1] = 1;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    q[i] = {l, r};
  }
  sort(q + 1, q + 1 + k);
  for (int i = 1; i <= k; i++)
    for (int j = q[i].first; j <= q[i].second; j++)
      v.push_back(j);
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < v.size(); j++) {
      if (i - v[j] <= 0)
        break;
      f[i] += f[i - v[j]];
      f[i] %= mod;
    }
  }
  cout << f[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int MOD = 998244353;
int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> ablestep;
  vector<int> dp(N + 5, 0), presum(N + 5, 0);
  while (K--) {
    int a, b;
    cin >> a >> b;
    ablestep.push_back({a, b});
  }
  dp[0] = 0;
  presum[0] = 0;
  dp[1] = 1;
  presum[1] = 1;
  for (int i = 1; i <= N; i++) {
    for (auto s : ablestep) {
      int l = i - s.second, r = i - s.first;
      if (l < 1)
        l = 1;
      if (l > r)
        continue;
      dp[i] += ((presum[r] - presum[l - 1]) + MOD) % MOD;

      dp[i] %= MOD;
    }
    presum[i] = (presum[i - 1] + dp[i]) % MOD;
  }
  cout << dp[N] % MOD << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> ablestep;
  vector<int> HEX(N + 5, 0);
  while (K--) {
    int a, b;
    cin >> a >> b;
    for (; a <= b; a++)
      ablestep.push_back(a);
  }
  sort(ablestep.begin(), ablestep.end());
  HEX[1] = 1;
  for (int i = 1; i <= N; i++) {
    for (auto s : ablestep) {
      if (s >= i)
        break;
      HEX[i] += HEX[i - s];
      HEX[i] %= 998244353;
    }
  }
  cout << HEX[N] << '\\n';
}