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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> s;
  long long MOD = 998244353;

  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    s.push_back(make_pair(l, r));
  }

  // sを用いてiにたどり着くまでの方法
  vector<long long> dp(n + 1, 0);
  // 累積和
  vector<long long> cuml(n + 1, 0);
  dp[1] = 1;
  cuml[1] = 1;

  for (int i = 2; i < n + 1; i++) {
    for (int j = 0; j < k; j++) {
      int l, r;
      if (i - s[j].first < 1)
        continue;
      r = i - s[j].first;
      l = max(1, i - s[j].second);

      dp[i] = (dp[i] + cuml[r] - cuml[l - 1] + MOD) % MOD;
    }
    // 累積和を更新する
    cuml[i] = (cuml[i - 1] + dp[i]) % MOD;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> s;
  long long MOD = 998244353;

  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j < r + 1; j++) {
      s.push_back(j);
    }
  }

  // sを用いてiにたどり着くまでの方法
  vector<long long> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i < n + 1; i++) {
    for (int mid : s) {
      if (i > mid)
        dp[i] = (dp[i] + dp[i - mid]) % MOD;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#define maxn 200005
#define mod 998244353
using namespace std;
int n, k, cnt;
long long A;
int l[maxn], r[maxn];
long long tree[maxn];

int lowbit(int x) { return x & (-x); }
void add(int x, long long v) {
  for (; x <= n; x += lowbit(x))
    (tree[x] += v) %= mod;
}
long long getsum(int x) {
  A = 0;
  for (; x >= 1; x -= lowbit(x))
    (A += tree[x]) %= mod;
  return A % mod;
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++)
    cin >> l[i] >> r[i];

  add(1, 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= k; j++)
      if (i > l[j]) {
        add(i, (getsum(i - l[j]) - getsum(max(0, i - r[j] - 1)) + mod) % mod);
        //		cout<<i-l[i]<<" "<<i-r[i]-1<<endl;
        //		cout<<i<<" "<<getsum(i-l[i])<<"
        //"<<getsum(max(0,i-r[i]-1))<<endl;
      }
  //	for(int i=1;i<=n;i++) cout<<getsum(i)-getsum(i-1)<<" ";
  //	cout<<endl;
  cout << (getsum(n) % mod - getsum(n - 1) % mod + mod) % mod << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define maxn 200005
#define mod 998244353
using namespace std;
int n, k, cnt;
int step[maxn];
long long ans;
long long dp[maxn];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      step[++cnt] = j;
  }
  sort(step + 1, step + 1 + cnt);
  dp[1] = 1ll;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= cnt; j++)
      if (i - step[j] >= 0) {
        if (dp[i - step[j]])
          (dp[i] += dp[i - step[j]]) %= mod;
      } else
        break;
  }

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

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

const int mod = 998244353;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> s(k);
  rep(i, k) { cin >> s[i].first >> s[i].second; }
  vector<mint> dp(n);
  vector<mint> sum(n);
  sum[0] = 1;
  dp[0] = 1;
  rep(i, n) {
    rep(j, k) {
      if (i - s[j].first >= 0)
        dp[i] += sum[i - s[j].first];
      if (i - s[j].second - 1 >= 0)
        dp[i] -= sum[i - s[j].second - 1];
    }
    if (i)
      sum[i] += dp[i] + sum[i - 1];
  }
  cout << dp[n - 1].x << endl;
}

Error: TLE

Fixed submission:

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

const int mod = 998244353;
int n, k;
vector<pair<int, int>> s;
vector<ll> dp;
ll path(int x, vector<ll> &dp) {
  ll res = 0;
  rep(i, k) {
    for (int j = s[i].first; j <= s[i].second; j++) {
      if (x - j >= 0) {
        if (dp[x - j] != -1)
          res = (res + dp[x - j]) % mod;
        else
          res = (res + path(x - j, dp)) % mod;
      }
    }
  }
  dp[x] = res;
  return res;
}

int main() {
  cin >> n >> k;
  s.resize(k);
  rep(i, k) { cin >> s[i].first >> s[i].second; }
  dp.assign(n, -1);
  dp[0] = 1;
  cout << path(n - 1, dp) << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define deb(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef double dd;
const int MOD = 998244353;
#define ff first
#define ss second

template <typename T> string to_string(vector<T> v) {
  string res;
  for (auto &i : v)
    res += char('0' + i), res += ' ';
  return res;
}

template <class A> void vbug(A x) { cout << to_string(x) << endl; }

void solve() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].ff >> v[i].ss;
  }
  vector<int> dp(n + 1, 0), pre(n + 1, 0);
  dp[0] = 0;
  dp[1] = pre[1] = 1;
  for (int i = 2; i <= n; i++) {
    int tmp = 0;
    for (auto j : v) {
      int l = i - j.ff;
      int r = i - j.ss - 1;
      l = max(0, l);
      r = max(0, r);
      tmp = ((tmp + pre[l]) % MOD - pre[r]) % MOD;
    }
    tmp = (tmp + MOD) % MOD;
    dp[i] = tmp;
    pre[i] = (pre[i - 1] + dp[i]) % MOD;
  }
  // vbug(dp);
  cout << dp[n] << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1, tt = 0;
  // cin >> t;
  while (t--) {
    // tt++;
    // cout << "Case #" << t << ": ";
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define deb(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef double dd;
const int MOD = 998244353;

vector<int> dp, v;
int n;

int recc(int i) {
  if (i > n)
    return 0;
  if (i == n)
    return 1;
  if (dp[i - 1] != -1)
    return dp[i - 1];
  int ans = 0;
  for (int j = 0; j < (int)v.size(); j++) {
    if (i + v[j] > n)
      break;
    int temp = recc(i + v[j]) % MOD;
    ans = (ans % MOD + temp) % MOD;
  }
  return dp[i - 1] = ans;
}

void solve() {
  int k;
  cin >> n >> k;
  while (k--) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      v.push_back(i);
  }
  sort(v.begin(), v.end());
  dp.resize(n, -1);
  int ans = recc(1) % MOD;
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1, tt = 0;
  // cin >> t;
  while (t--) {
    // tt++;
    // cout << "Case #" << t << ": ";
    solve();
  }
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(j, k) cin >> l[j] >> r[j];

  vector<ll> dp(n + 1, 0);
  vector<ll> dpsum(n + 1, 0);
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(1, li);
      dp[i] += dpsum[ri] - dpsum[li - 1];
      // dp[i] = (dp[i]+MOD) % MOD;
      dp[i] %= MOD;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= MOD;
    // dpsum[i] = (dpsum[i]+MOD) % MOD;
  }
  cout << (dp[n] + MOD) % MOD << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 998244353;
const int N = 2 * 1e5 + 100;
int l, r;
vector<int> s;
int dp[N];
int main() {
  int n, k;
  cin >> n >> k;
  rep(i, k) {
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      s.push_back(i);
  }
  int m = s.size();

  dp[0] = 1;
  rep(i, n) {
    rep(j, m) {
      if (i + s[j] >= n)
        continue;
      dp[i + s[j]] += dp[i];
      dp[i + s[j]] %= MOD;
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
const ll M = 998244353;

int main() {
  ll N, K;
  cin >> N >> K;
  ll L[K], R[K];
  // vector<ll> p;
  for (ll i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  ll a[N + 1];
  ll f[N + 1];
  for (ll i = 0; i < N + 1; i++) {
    f[i] = 0;
    a[0] = 0;
  }
  f[1] = 1;
  a[1] = 1;
  for (ll i = 0; i < N + 1; i++) {
    for (ll j = 0; j < K; j++) {
      ll li = i - R[j];
      ll ri = i - L[j];
      if (ri < 0)
        continue;
      if (li < 1)
        li = 1;
      f[i] += (a[ri] - a[li - 1]) % M;
    }
    a[i] = a[i - 1] + f[i];
  }

  cout << f[N] % M << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
const ll M = 998244353;

int main() {
  ll N, K;
  cin >> N >> K;
  ll L[K], R[K];
  vector<ll> p;
  for (ll i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
    for (ll j = L[i]; j <= R[i]; j++) {
      p.push_back(j);
    }
  }
  sort(p.begin(), p.end());
  ll m[N + 1];
  ll ans = 0;
  m[1] = 1;
  for (ll num = 2; num < N + 1; num++) {
    m[num] = 0;
    bool ok = false;
    for (ll i = 0; i < p.size(); i++) {
      if (p[i] <= num - 1) {
        m[num] += m[num - p[i]] % M;
        m[num] = m[num] % M;
      } else {
        break;
      }
    }
  }
  cout << m[N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define forab(i, a, b) for (int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define trav(i, x) for (auto &i : x)
#define endl cout << "\\n";
// const int mod = 1e9+7;
const int maxn = 2e5 + 7;
int factors[maxn];
ll mod = 998244353;
int main() {
  FAST_IO;
  ll n, x, y, k;
  cin >> n >> k;
  int arr[k][2];
  for (int i = 0; i < k; i++) {
    cin >> arr[i][0] >> arr[i][1];
  }
  vector<ll> dp(maxn, 0);
  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 (auto it = 0; it < k; it++) {

      int a = arr[it][0];
      int b = arr[it][1];

      if (a + i < n)
        dp[i + a] = (dp[i + a] + dp[i]) % mod;
      if (b + 1 + i < n)
        dp[i + b + 1] = (dp[i + b + 1] - dp[i] + mod) % mod;
    }
  }

  cout << dp[n - 1] % mod;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define forab(i, a, b) for (int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define trav(i, x) for (auto &i : x)
#define endl cout << "\\n";
// const int mod = 1e9+7;
const int maxn = 2e5 + 7;
int factors[maxn];
ll mod = 998244353;
int main() {
  FAST_IO;
  ll n, x, y, k;
  cin >> n >> k;
  set<int> s;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    for (int j = x; j <= y; j++)
      s.insert(j);
  }
  vector<ll> dp(maxn, 0);
  dp[0] = 0;
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (auto it = s.begin(); it != s.end(); it++) {
      // cout<<*it;
      if (i - *it >= 1)
        dp[i] = (dp[i] + dp[i - *it]) % mod;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

template <typename T> struct BIT {
  int n;            // 要素数
  vector<T> bit[2]; // データの格納先
  BIT(int n_) { init(n_); }
  void init(int n_) {
    n = n_ + 1;
    for (int p = 0; p < 2; p++)
      bit[p].assign(n, 0);
  }
  void add_sub(int p, int i, T x) {
    for (int idx = i; idx < n; idx += (idx & -idx)) {
      (bit[p][idx] += x) %= MOD;
    }
  }
  void add(int l, int r, T x) { // [l,r) に加算
    add_sub(0, l, -x * (l - 1));
    add_sub(0, r, x * (r - 1));
    add_sub(1, l, x);
    add_sub(1, r, -x);
  }
  T sum_sub(int p, int i) {
    T s(0);
    for (int idx = i; idx > 0; idx -= (idx & -idx)) {
      (s += bit[p][idx]) %= MOD;
    }
    return s;
  }
  T sum(int i) {
    ll res = (sum_sub(1, i) * i) % MOD;
    return (res + sum_sub(0, i)) % MOD;
  }
};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  BIT<ll> bt(2 * 100000 + 10);
  bt.add(1, 2, 1);
  ll N, K;
  cin >> N >> K;

  vector<ll> L(N), R(N);
  REP(i, K) cin >> L[i] >> R[i], R[i]++;
  REP(j, N) {
    REP(i, K) {
      bt.add(min(j + L[i], N + 1), min(j + R[i], N + 1),
             bt.sum(j) - bt.sum(j - 1));
    }
  }

  std::cout << (bt.sum(N) - bt.sum(N - 1) + MOD) % MOD << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

ll dp[100000 * 2 + 10]{};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll N, K;
  cin >> N >> K;
  dp[1] = 1;
  vector<ll> L(N), R(N);
  REP(i, K) cin >> L[i] >> R[i];
  vector<ll> S;
  REP(i, K) {
    for (ll d = L[i]; d <= R[i]; ++d)
      S.push_back(d);
  }

  for (ll i = 1; i < N; ++i) {
    for (auto s : S)
      if (i + s <= N)
        (dp[i + s] += dp[i]) %= MOD;
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

ll N, K;
const ll mm = 998244353;
int main() {

  cin >> N >> K;

  vector<pair<ll, ll>> tile(K);
  for (ll i = 0; i < K; i++)
    cin >> tile[i].first >> tile[i].second;
  vector<ll> Msum(N + 1, 0);
  Msum[1] = 1;
  for (ll i = 2; i <= N; i++) {

    for (ll j = 0; j < K; j++) {

      ll s1 = i - tile[j].first;
      ll s2 = i - tile[j].second;
      if (s1 <= 0 && s2 <= 0)
        continue;
      if (s2 <= 0 && s1 > 0)
        s2 = 1;
      Msum[i] += Msum[s1] - Msum[s2 - 1];
      if (Msum[i] < 0)
        Msum[i] += mm;
      Msum[i] %= mm;
    }
    Msum[i] += Msum[i - 1];
    Msum[i] %= mm;
  }

  cout << (Msum[N] - Msum[N - 1] + mm) % mm << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

ll N, K, count1 = 0;
stack<ll> rote;
vector<ll> T;
void ST(ll sum) {

  if (sum == N) {
    count1++;
    return;
  }
  if (sum > N)
    return;
  for (ll i = 0; i < T.size(); i++) {

    ST(sum + T[i]);
  }
  // ST(sum);

  return;
}

int main() {

  cin >> N >> K;
  set<ll> tile;

  for (ll i = 0; i < K; i++) {

    ll s1, s2;
    cin >> s1 >> s2;
    for (ll j = s1; j <= s2; j++)
      tile.insert(j);
  }

  for (ll i : tile)
    T.push_back(i);
  ST(1);

  cout << count1 << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

//
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;
#define int long long

const int INF = 1000000007;
const int MOD = 998244353;

int bit[200005];
int n, k;
void upd(int x, int val) {
  for (; x <= n; x += x & -x) {
    bit[x] = (bit[x] + val) % MOD;
  }
}

int get(int x) {
  int res = 0;
  for (; x > 0; x -= x & -x) {
    res = (res + bit[x]) % MOD;
  }
  return res;
}
void solve_test() {
  cin >> n >> k;
  upd(1, 1);
  vector<pair<int, int>> v;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    v.PB({l, r});
  }
  sort(v.begin(), v.end());
  for (int i = 2; i <= n; i++) {
    int lo = 0, hi = 0;
    for (int j = 0; j < k; j++) {
      lo = v[j].f;
      hi = v[j].s;
      if (lo >= i)
        break;
      //            what_is(lo);
      //            what_is(hi);
      hi = min(i - 1, hi);
      int res = (MOD + get(i - lo) - get(i - hi - 1)) % MOD;
      upd(i, res);
    }
    // cout << i << " " << get(i) << "\\n";
  }
  cout << (MOD + get(n) - get(n - 1)) % MOD << "\\n";
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tests;
  tests = 1;
  // cin >> tests;
  while (tests--) {
    solve_test();
  }
  return 0;
}


Error: 0

Fixed submission:

//
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int INF = 1000000007;
const int MOD = 998244353;

int bit[200005];
int n, k;
int upd(int x, int val) {
  for (; x <= n; x += x & -x) {
    bit[x] = (bit[x] + val) % MOD;
  }
}

int get(int x) {
  int res = 0;
  for (; x > 0; x -= x & -x) {
    res = (res + bit[x]) % MOD;
  }
  return res;
}
void solve_test() {
  cin >> n >> k;
  upd(1, 1);
  vector<pii> v;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    v.PB({l, r});
  }
  sort(v.begin(), v.end());
  for (int i = 2; i <= n; i++) {
    int lo = 0, hi = 0;
    for (int j = 0; j < k; j++) {
      lo = max(lo, v[j].f);
      hi = v[j].s;
      if (lo > hi)
        continue;
      if (lo >= i)
        break;
      //            what_is(lo);
      //            what_is(hi);
      hi = min(i - 1, hi);
      int res = (MOD + get(i - lo) - get(i - hi - 1)) % MOD;
      upd(i, res);
      lo = hi + 1;
    }
    // cout << i << " " << get(i) << "\\n";
  }
  cout << get(n) - get(n - 1) << "\\n";
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tests;
  tests = 1;
  // cin >> tests;
  while (tests--) {
    solve_test();
  }
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];

  const int mod = 998244353;
  vector<int> dp(n + 1, 0), dps(n + 1, 0);
  dp[1] = dps[1] = 1;

  for (int i = 2; i < n + 1; ++i) {
    rep(j, k) {
      dp[i] = ((dp[i] + (i - l[j] >= 0 ? dps[i - l[j]] : 0) % mod) % mod -
               (i - (r[j] + 1) >= 0 ? dps[i - (r[j] + 1)] : 0) % mod + mod) %
              mod;
    }
    dps[i] = dps[i - 1] + dp[i];
    dps[i] %= mod;
  }

  // rep(i, n+1) cout << dp[i] << " ";
  // cout << endl;

  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

const int mod = 998244353;
vector<ll> m;

ll dp[220000];
ll rec(ll n) {
  if (dp[n] > 0)
    return dp[n];
  if (n == 0)
    return 1;

  ll ret = 0;
  for (auto step : m) {
    if (n - step < 0)
      continue;

    ret = (ret + rec(n - step)) % mod;
  }

  return dp[n] = ret;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;

  rep(i, k) {
    ll l, r;
    cin >> l >> r;

    for (ll j = l; j <= r; ++j)
      m.push_back(j);
  }

  cout << rec(n - 1) << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
int n;
long long mod = 998244353;
vector<pi> seg;
long long sum(vector<long long> &tree, int i) {
  long long ans = 0;
  while (i > 0) {
    ans += tree[i];
    i -= (i & -i);
  }
  return ans;
}
void update(vector<long long> &tree, int i, long long diff) {
  while (i < tree.size()) {
    tree[i] += diff;
    i += (i & -i);
  }
}
int main() {
  vector<long long> tree(600000, 0);
  int k;
  cin >> n >> k;
  while (k--) {
    int a, b;
    cin >> a >> b;
    seg.push_back(pi(a, b));
  }
  update(tree, n, 1);
  for (int i = n - 1; i >= 1; i--) {
    long long ret = 0;
    for (int j = 0; j < seg.size(); j++) {
      int start = i + seg[j].first;
      int fin = i + seg[j].second;
      ret += sum(tree, fin) - sum(tree, start - 1);
      ret %= mod;
    }
    update(tree, i, ret);
  }
  cout << sum(tree, 1);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
long long dp[300005];
bool check[200005];
long long mod = 998244353;
vector<int> arr;
long long f(int curr) {
  long long &ret = dp[curr];
  if (~ret)
    return ret;
  ret = 0;
  for (int i = 0; i < arr.size(); i++) {
    if (curr + arr[i] > n)
      break;
    ret += f(curr + arr[i]);
    ret %= mod;
  }
  return ret;
}
int main() {
  int k;
  cin >> n >> k;
  while (k--) {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++)
      check[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    if (check[i])
      arr.push_back(i);
  }
  memset(dp, -1, sizeof(dp));
  dp[n] = 1;
  cout << f(1);
}