alexjercan/bugnet huggingface dataset:
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define MOD 998244353
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v, tmp;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    tmp.push_back({x, y});
  }
  sort(all(tmp));
  int px = 0, py = 0;
  for (auto g : tmp) {
    if (g.ff <= py) {
      py = g.ss;
    } else {
      if (px != 0 && py != 0)
        v.push_back({px, py});
      px = g.ff;
      py = g.ss;
    }
  }
  v.push_back({px, py});
  vector<int> A(n + 1);
  A[1] = 1;
  A[2] = -1;
  for (int i = 1; i <= n; i++) {
    A[i] += A[i - 1];
    if (A[i] >= MOD)
      A[i] -= MOD;
    for (auto g : v) {
      if (i + g.ff <= n) {
        A[i + g.ff] += A[i];
        if (A[i + g.ff] >= MOD)
          A[i + g.ff] -= MOD;
      }
      if (i + g.ss + 1 <= n) {
        A[i + g.ss + 1] -= A[i];
        if (A[i + g.ss + 1] < 0)
          A[i + g.ss + 1] += MOD;
      }
    }
  }
  cout << A[n];
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define MOD 998244353
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v, tmp;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    tmp.push_back({x, y});
  }
  sort(all(tmp));
  int px = 0, py = 0;
  for (auto g : tmp) {
    if (g.ff <= py) {
      py = g.ss;
    } else {
      if (px != 0 && py != 0)
        v.push_back({px, py});
      px = g.ff;
      py = g.ss;
    }
  }
  v.push_back({px, py});
  vector<int> A(n + 1);
  A[1] = 1;
  A[2] = -1;
  for (int i = 1; i <= n; i++) {
    A[i] += A[i - 1];
    if (A[i] >= MOD)
      A[i] -= MOD;
    for (auto g : v) {
      A[i + g.ff] += A[i];
      if (A[i + g.ff] >= MOD)
        A[i + g.ff] -= MOD;
      A[i + g.ss + 1] -= A[i];
      if (A[i + g.ss + 1] < 0)
        A[i + g.ss + 1] += MOD;
    }
  }
  cout << A[n];
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

/*
                ******

        Author: Nguyen Phuc Chuong

                ******
*/

#include <bits/stdc++.h>

using namespace std;

long long a[10][2];
long long dp[200000];
const long long mod = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  dp[0] = 1;
  dp[1] = -1;
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    }
    for (int j = 0; j < k; j++) {
      if (i + a[j][0] < n) {
        dp[i + a[j][0]] = (dp[i + a[j][0]] + dp[i]) % mod;
      }
      if (i + a[j][1] + 1 < n) {
        dp[i + a[j][1] + 1] = (dp[i + a[j][1] + 1] - dp[i]) % mod;
        if (dp[i + a[j][1] + 1] < 0) {
          dp[i + a[j][1] + 1] += mod;
        }
      }
    }
  }
  cout << dp[n - 1];
  return 0;
}


Error: WA

Fixed submission:

/*
                ******

        Author: Nguyen Phuc Chuong

                ******
*/

#include <bits/stdc++.h>

using namespace std;

long long a[10][2];
long long dp[200000];
const long long mod = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  dp[0] = 1;
  dp[1] = -1;
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    }
    for (int j = 0; j < k; j++) {
      dp[i + a[j][0]] = (dp[i + a[j][0]] + dp[i]) % mod;
      dp[i + a[j][1] + 1] = (dp[i + a[j][1] + 1] - dp[i]) % mod;
      if (dp[i + a[j][1] + 1] < 0) {
        dp[i + a[j][1] + 1] += mod;
      }
    }
  }
  cout << dp[n - 1];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define debug_value(x)                                                         \\
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x     \\
       << endl;
#define debug(x)                                                               \\
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

int N, K;
int L[10], R[10];

ll cnt[500005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  cnt[0] = 1;
  cnt[1] = -1;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cnt[i] += cnt[i - 1];
    cnt[i] %= MOD;
    for (int j = 0; j < K; j++) {
      cnt[i + L[j]] += cnt[i];
      cnt[i + L[j]] %= MOD;
      cnt[i + R[j] + 1] -= cnt[i];
      cnt[i + R[j] + 1] %= MOD;
    }
  }
  cout << (cnt[N - 1] + MOD) % MOD << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define debug_value(x)                                                         \\
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x     \\
       << endl;
#define debug(x)                                                               \\
  cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

int N, K;
int L[10], R[10];

ll cnt[200005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  cnt[0] = 1;
  cnt[1] = -1;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cnt[i] += cnt[i - 1];
    cnt[i] %= MOD;
    for (int j = 0; j < K; j++) {
      cnt[i + L[j]] += cnt[i];
      cnt[i + L[j]] %= MOD;
      cnt[i + R[j] + 1] -= cnt[i];
      cnt[i + R[j] + 1] %= MOD;
    }
  }
  cout << (cnt[N - 1] + MOD) % MOD << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()

#define MOD 998244353
using namespace std;

// }}}

int main() {
  int N, K;

  cin >> N >> K;
  vector<pair<int, int>> seg;

  FOR(i, 0, K) {
    int L, R;
    cin >> L >> R;
    seg.push_back(pair<int, int>(L, R));
  }

  long long cur = 0;

  long long a[500000];

  FOR(i, 0, N + 1) a[i] = 0;
  a[0] = 1;
  a[1] = -1;

  FOR(i, 0, N) {
    cur = (cur + a[i]) % MOD;
    FOR(j, 0, seg.size()) {
      a[i + seg[j].first] = (a[i + seg[j].first] + cur) % MOD;
      a[i + seg[j].second + 1] = (a[i + seg[j].second + 1] - cur) % MOD;
    }
  }

  cout << (cur + MOD) % MOD << endl;
}


Error: 0

Fixed submission:

// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()

#define MOD 998244353
using namespace std;

// }}}

int main() {
  int N, K;

  cin >> N >> K;
  vector<pair<int, int>> seg;

  FOR(i, 0, K) {
    int L, R;
    cin >> L >> R;
    seg.push_back(pair<int, int>(L, R));
  }

  long long cur = 0;

  long long a[300000];

  FOR(i, 0, N + 1) a[i] = 0;
  a[0] = 1;
  a[1] = -1;

  FOR(i, 0, N) {
    cur = (cur + a[i]) % MOD;
    FOR(j, 0, seg.size()) {
      a[i + seg[j].first] = (a[i + seg[j].first] + cur) % MOD;
      a[i + seg[j].second + 1] = (a[i + seg[j].second + 1] - cur) % MOD;
    }
  }

  cout << (cur + MOD) % MOD << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
struct qu {
  ll l;
  ll r;
} q[11];
struct tree {
  ll l;
  ll r;
  ll lazy;
  ll ans;
} t[4 * 200005];
void build(ll l, ll r, int pos) {
  t[pos].l = l;
  t[pos].r = r;
  if (l == r) {
    if (l == 1) {
      t[pos].ans = 1;
    }
    return;
  }
  ll mid = (l + r) / 2;
  build(l, mid, pos * 2);
  build(mid + 1, r, pos * 2 + 1);
}
void pushdown(int pos) {
  if (t[pos].lazy) {
    ll k = t[pos].lazy;
    t[pos * 2 + 1].lazy += k;
    t[pos * 2].lazy += k;
    t[pos * 2].ans += (t[pos * 2].r - t[pos * 2].l + 1) * k;
    t[pos * 2 + 1].ans += (t[pos * 2 + 1].r - t[pos * 2 + 1].l + 1) * k;
    t[pos].lazy = 0;
  }
}
void update(ll l, ll r, int pos, ll k) {
  if (l <= t[pos].l && t[pos].r <= r) {
    t[pos].ans += k * (t[pos].r - t[pos].l + 1);
    t[pos].ans %= mod;
    t[pos].lazy += k;
    return;
  }
  pushdown(pos);
  ll mid = (t[pos].l + t[pos].r) / 2;
  if (l <= mid) {
    update(l, r, pos * 2, k);
  }
  if (r > mid) {
    update(l, r, pos * 2 + 1, k);
  }
}
ll ask(ll l, ll r, int pos) {
  if (l <= t[pos].l && t[pos].r <= r) {
    return t[pos].ans;
  }
  pushdown(pos);
  ll res = 0;
  ll mid = (t[pos].l + t[pos].r) / 2;
  if (l <= mid) {
    res += ask(l, r, pos * 2);
  }
  if (r > mid) {
    res += ask(l, r, pos * 2 + 1);
  }
  return res % mod;
}
int main() {
  ll n;
  int m;
  scanf("%lld%d", &n, &m);
  set<int> s;
  for (int i = 1; i <= m; i++) {
    scanf("%lld%lld", &q[i].l, &q[i].r);
  }
  build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    ll tp = ask(i, i, 1);
    if (tp) {
      for (int j = 1; j <= m; j++) {
        if (i + q[j].l > n) {
          continue;
        }
        update(i + q[j].l, min(n, i + q[j].r), 1, tp);
      }
    }
  }
  printf("%lld\\n", ask(n, n, 1));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[200005];
const ll mod = 998244353;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  set<int> s;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    for (int j = x; j <= y; j++) {
      s.insert(j);
    }
  }
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (dp[i]) {
      for (set<int>::iterator its = s.begin(); its != s.end(); its++) {
        int k = *its;
        if (i + k > n) {
          break;
        }
        dp[i + k] += dp[i];
        dp[i + k] %= mod;
      }
    }
  }
  printf("%lld\\n", dp[n]);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2E5 + 5;
const int mod = 998244353;

int dp[N];

void solve() {

  int n, k;
  scanf("%lld %lld", &n, &k);
  pair<int, int> p[15];

  for (int i = 0; i < k; i++) {
    int x, y;
    scanf("%lld %lld", &x, &y);
    p[i] = make_pair(x, y);
  }

  dp[1] = 1;

  for (int i = 2; i <= n; i++) {

    dp[i] = 0;

    for (int j = 0; j < k; j++) {

      int l = max(1LL, i - p[j].second);
      int r = i - p[j].first;

      if (l <= r) {
        dp[i] += dp[r] - dp[l - 1];
        dp[i] %= mod;
        dp[i] += mod;
        dp[i] %= mod;
      }
    }

    dp[i] += dp[i - 1];
    dp[i] %= mod;
  }

  printf("%lld\\n", ((dp[n] - dp[n - 1]) + mod) % mod);
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1E5 + 5;
const int mod = 998244353;

int dp[N];

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> idx;

  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      idx.push_back(j);
  }
  sort(idx.begin(), idx.end());
  idx.erase(unique(idx.begin(), idx.end()), idx.end());
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j : idx) {
      if (i - j >= 1) {
        dp[i] += dp[i - j];
        dp[i] %= mod;
      } else
        break;
    }
  }

  cout << dp[n];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define sf1(n) scanf("%d", &n)
#define sf2(n, q) scanf("%d %d", &n, &q)
#define f first
#define ss second
#define pb push_back
#define lli long long
#define mem(ar, false) memset(ar, false, sizeof(ar))
#define mpr make_pair
#define endl "\\n"
#define mod 1000000007
#define inf 9000000000000000000LL
#define M 200005

using namespace std;

lli dp[200005];
lli ar[200005];
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v;
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    v.pb(mpr(a, b));
  }
  sort(v.begin(), v.end());
  dp[1] = 1;

  ar[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int l = i - v[j].ss;
      int r = i - v[j].f;
      if (r <= 0)
        continue;
      l = max(1, l);
      dp[i] += ar[r] - ar[l - 1];
      dp[i] %= 998244353;
    }

    ar[i] = ar[i - 1] + dp[i];
  }
  cout << dp[n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define sf1(n) scanf("%d", &n)
#define sf2(n, q) scanf("%d %d", &n, &q)
#define f first
#define ss second
#define pb push_back
#define lli long long
#define mem(ar, false) memset(ar, false, sizeof(ar))
#define mpr make_pair
#define endl "\\n"
#define mod 1000000007
#define inf 9000000000000000000LL
#define M 200005

using namespace std;

int dp[200005];
int br[200005];
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;

  vector<int> v;
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    br[a]++;
    br[b + 1]--;
  }
  for (int i = 1; i <= n; i++) {
    br[i] += br[i - 1];
    if (br[i] == 0)
      continue;
    else
      v.pb(i);
  }
  sort(v.begin(), v.end());
  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 - v[j]];
        dp[i] %= 998244353;
      } else
        break;
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

#define ll long long int
#define ld long double
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define pb push_back

using namespace std;
typedef pair<int, int> pii;

#define MOD 998244353

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

  ll a[n + 1];
  ll pref[n + 1];
  memset(pref, 0, sizeof(pref));
  memset(a, 0, sizeof(a));
  a[1] = pref[1] = 1;
  vector<pii> segments(k);
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    segments[i] = {l, r};
  }

  for (int i = 2; i <= n; i++) {
    for (pii seg : segments) {
      int l = max(0, i - seg.second - 1);
      int r = max(0, i - seg.first);
      a[i] += ((pref[r] - pref[l]) + MOD) % MOD;
      a[i] %= MOD;
    }
    pref[i] = ((pref[i - 1] % MOD) + (a[i] % MOD)) % MOD;
  }

  cout << a[n];
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#define ll long long int
#define ld long double
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define pb push_back

using namespace std;
typedef pair<int, int> pii;

#define MOD 998244353

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

  ll a[n + 1];
  bool has[n + 1];
  memset(has, false, sizeof(has));
  memset(a, 0, sizeof(a));
  a[1] = 1;
  set<int> h;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      has[j] = true;
      h.insert(j);
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j : h) {
      if (i + j > n)
        break;
      a[i + j] += a[i];
      a[i + j] %= MOD;
    }
  }

  cout << a[n];
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//  main.cpp
//  AtCoderD
//
//  Created by Shivam on 19/09/20.
//  Copyright © 2020 Shivam. All rights reserved.
//

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define x first
#define y second
#define mp make_pair
#define endl "\\n"
typedef long long ll;
int mod = 998244353;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> intervals(k);
  for (int i = 0; i < k; ++i)
    cin >> intervals[i].x >> intervals[i].y;
  sort(intervals.begin(), intervals.end());
  vector<ll> dp(n + 1, 0), suffix(n + 1, 0);
  dp[n - 1] = 1;
  suffix[n - 1] = 1;
  for (int i = n - 2; i >= 0; --i) {
    for (auto range : intervals) {
      if (i + range.x > n - 1)
        break;
      int m = min(range.y, n - 1 - i);
      dp[i] += suffix[i + range.x] - suffix[i + m + 1] + mod;
      if (dp[i] >= mod)
        dp[i] %= mod;
    }
    suffix[i] = dp[i] + suffix[i + 1];
    if (suffix[i] >= mod)
      suffix[i] %= mod;
    // cout<<i<<" "<<dp[i]<<" "<<suffix[i]<<endl;
  }
  cout << dp[0] << endl;
  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  AtCoderD
//
//  Created by Shivam on 19/09/20.
//  Copyright © 2020 Shivam. All rights reserved.
//

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define x first
#define y second
#define mp make_pair
#define endl "\\n"
typedef long long ll;
int mod = 998244353;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> intervals(k);
  for (int i = 0; i < k; ++i)
    cin >> intervals[i].x >> intervals[i].y;
  sort(intervals.begin(), intervals.end());
  vector<ll> dp(n + 1, 0);
  dp[n] = 1;
  for (int i = n - 1; i >= 1; --i) {
    for (auto Int : intervals) {
      if (Int.x > n - i)
        break;
      int m = min(n - i, Int.y);
      for (int j = Int.x; j <= m; ++j) {
        dp[i] += dp[i + j];
        if (dp[i] >= mod)
          dp[i] -= mod;
      }
    }
    // cout<<i<<" "<<dp[i]<<endl;
  }
  cout << dp[1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

/*@author Vipen Loka*/
#include <bits/stdc++.h>
#define endl '\\n'
#define ff first
#define ss second
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 998244353
#define inf 1000000000000000001;
#define deb(x) cout << #x << ':' << x << '\\n';

using namespace std;

struct fentree {
  int n;
  vector<ll> tree;

  void init(int n) {
    this->n = n;
    tree.assign(n + 2, 0);
  }

  void set(int i, ll val) {
    while (i <= n) {
      tree[i] += val;
      tree[i] %= mod;
      i += i & -i;
    }
  }

  ll get(int a) {
    ll s = 0;
    while (a > 0) {
      s += tree[a];
      s %= mod;
      a -= a & -a;
    }

    return s;
  }

  ll get(int a, int b) {
    // a and b are inclusive
    ll t = get(b) - get(a - 1);
    if (t < 0)
      t += mod;
    return t;
  }
};

void solve() {
  int i, j;
  int n, k;
  cin >> n >> k;

  vector<pair<int, int>> intervals(k);
  for (int i = 0; i < k; ++i) {
    cin >> intervals[i].ff >> intervals[i].ss;
  }

  fentree t;
  t.init(n);
  t.set(1, 1);

  for (int i = 2; i <= n; ++i) {
    ll s = 0;
    for (int j = 0; j < k; ++j) {
      int a = max(0, i - intervals[j].ss);
      int b = min(i - 1, i - intervals[j].ff);

      s += t.get(a, b);
      s %= mod;
    }

    t.set(i, s);
  }

  printf("%lld\\n", t.get(n, n) % mod);
}

int main() {
  // ios_base::sync_with_stdio(false);
  //    cin.tie(0);

  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
}


Error: TLE

Fixed submission:

/*@author Vipen Loka*/
#include <bits/stdc++.h>
#define endl '\\n'
#define ff first
#define ss second
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define deb(x) cout << #x << ':' << x << '\\n';

using namespace std;

void solve() {
  int i, j;
  int n, k;
  cin >> n >> k;
  vector<int> pos(n + 1, 0), dp(n + 1, 0);
  vector<pair<int, int>> intervals(k);

  for (int i = 0; i < k; ++i) {
    int a, b;
    // cin >> a >> b;
    cin >> intervals[i].ff >> intervals[i].ss;
    // printf("%d %d\\n",intervals[i].ff,intervals[i].ss);
    // for (int j = a; j <= b; ++j)
    // {
    // 	pos[j]=1;
    // }
  }
  dp[1] = 1;
  sort(intervals.begin(), intervals.end());

  for (int i = 2; i <= n; ++i) {
    for (int t = 0; t < k; t++) {
      // printf("%d %d\\n",intervals[t].ff,intervals[t].ss);
      for (int j = intervals[t].ff; j <= min(intervals[t].ss, i - 1); ++j) {
        dp[i] += dp[i - j];
        dp[i] %= 998244353;
      }
    }

    // printf("%d\\n",dp[i]);
  }

  printf("%d\\n", dp[n]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = LONG_LONG_MAX / 2;
const ll mod = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> p;
  for (int i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    p.push_back(make_pair(l, r));
  }

  vector<ll> dp(2 * N + 1, 0);
  dp[1] = 1;
  for (int i = 1; i < N; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    for (auto j : p) {
      dp[i + j.first] += dp[i];
      dp[i + j.second + 1] -= dp[i];
      dp[i + j.first] %= mod;
      if (dp[i + j.second + 1] < 0)
        dp[i + j.second + 1] += mod;
    }
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = LONG_LONG_MAX / 2;
const ll mod = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int N, K;
  cin >> N >> K;
  set<int> s;
  for (int i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      s.insert(j);
    }
  }
  vector<int> v(s.size());
  copy(s.begin(), s.end(), v.begin());
  // for (int i : v) {
  //  cout << i << " ";
  //}
  // cout << endl;

  vector<ll> dp(N + 1, 0);
  dp[1] = 1;
  for (int i = 1; i < N; i++) {
    for (int j : v) {
      if (i + j > N)
        continue;
      dp[i + j] += dp[i];
      dp[i + j] %= mod;
    }
  }
  cout << dp[N] << endl;
}