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


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
const ll MOD = 998244353;

ll dp[200001];

int main() {
  int N, K;
  cin >> N >> K;

  vector<pair<int, int>> step;
  for (int i = 0; i < K; i++) {
    int L, R;
    cin >> L >> R;
    step.push_back(make_pair(L, R));
  }

  dp[1] = 1;
  dp[2] = -1;
  ll S = 0;
  for (int i = 1; i <= N; i++) {
    S = (S + dp[i]) % MOD;
    for (auto p : step) {
      int L = p.first;
      int R = p.second;
      if (i + L <= N) {
        dp[i + L] = (dp[i + L] + S) % MOD;
      }
      if (i + R + 1 <= N) {
        dp[i + R + 1] = (dp[i + R + 1] - S + MOD) % MOD;
      }
    }
  }

  ll ans = S;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
const ll MOD = 998244353;

ll dp[200001];

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> step;
  for (int i = 0; i < K; i++) {
    int L, R;
    cin >> L >> R;
    for (int j = L; j <= R; j++) {
      step.push_back(j);
    }
  }

  dp[1] = 1;

  for (int i = 1; i < N; i++) {
    for (int s : step) {
      if (i + s > N)
        continue;

      dp[i + s] = (dp[i + s] + dp[i]) % MOD;
    }
  }

  ll ans = dp[N];

  cout << ans << endl;

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

#include <bits/stdc++.h>
const int m = 998244353;
using namespace std;
long long st[(int)1e6];
long long get(int n) {
  long long x = 0;
  while (n > 0) {
    x += st[n];
    n -= n & (-n);
  }
  return x;
}
void update(int i, int x) {
  while (i < 1e6) {
    st[i] += x;
    i += i & (-i);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  memset(st, 0, sizeof(st));
  long long n, k, a, b;
  cin >> n >> k;
  set<pair<int, int>> s;
  while (k--) {
    cin >> a >> b;
    s.insert({a, b});
  }
  update(1, 1);
  long long r = 0;
  for (int i = 2; i <= n; i++) {
    r = 0;
    for (auto x : s) {
      r += get(i - x.first) - get(i - x.second - 1);
      r %= m;
    }
    update(i, r);
  }

  cout << r;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int m = 998244353;
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  long long n, k, a, b;
  scanf("%lld%lld", &n, &k);
  set<long long> s;
  while (k--) {
    scanf("%lld%lld", &a, &b);
    for (int i = a; i <= b; i++)
      s.insert(i);
  }
  vector<long long> r(n + 1);
  r[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int x : s) {
      if (i - x < 0)
        break;
      r[i] += r[i - x];
      r[i] %= m;
    }
  }
  printf("%lld", r[n]);
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mx 200005
#define ll long long
#define mod 998244353

void IO() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int ar[mx];
char ch[mx];
int m, n, k, ii;
int mark[mx];
ll dp[mx], csum[mx];
vector<int> v;
vector<pair<int, int>> ace;

ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a %= mod;
  return a;
}

ll func(int val) {
  if (val == n)
    return 1;
  if (dp[val] != -1)
    return dp[val];
  ll re = 0;
  for (int u : v) {
    if (val + u > n)
      break;
    re = add(re, func(val + u));
  }
  return dp[val] = re;
}

void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    for (int j = x; j <= y; j++)
      mark[j] = 1;
  }
  int shuru = 1;
  int sesh = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (mark[i]) {
      sesh++;
    } else {
      if (shuru <= sesh) {
        ace.push_back({shuru, sesh});
      }
      shuru = i + 1;
      sesh = i;
    }
  }
  dp[n] = 1;
  csum[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    int need = n - i;
    for (auto it : ace) {
      if (it.first > need)
        break;
      int st = it.first;
      int en = min(need, it.second);
      ll va = (csum[i + st] - csum[i + en + 1]) % mod;
      if (va < 0)
        va = (va + mod) % mod;
      dp[i] = (dp[i] + va) % mod;
      if (it.second == need)
        break;
    }
    csum[i] += csum[i + 1];
    csum[i] = add(csum[i], dp[i]);
  }
  printf("%lld\\n", dp[1]);
}

int main() {
  IO();
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define mx 200005
#define ll long long
#define mod 998244353

void IO() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int ar[mx];
char ch[mx];
int m, n, k, ii;
int mark[mx];
ll dp[mx];
vector<int> v;

ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a %= mod;
  return a;
}

ll func(int val) {
  if (val == n)
    return 1;
  if (dp[val] != -1)
    return dp[val];
  ll re = 0;
  for (int u : v) {
    if (val + u > n)
      break;
    re = add(re, func(val + u));
  }
  return dp[val] = re;
}

void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    for (int j = x; j <= y; j++)
      mark[j] = 1;
  }
  for (int i = 1; i <= n; i++) {
    if (mark[i])
      v.push_back(i);
    dp[i] = -1;
  }
  printf("%lld\\n", func(1));
}

int main() {
  IO();
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int siz = 2e5 + 10;
const int SIZ = 1e6 + 10;
const int mod = 998244353;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "3";

int seg[4 * siz];

void update(int id, int st, int ed, int pos, int val) {
  if (st == ed) {
    seg[id] += val;
    return;
  }

  int mid = (st + ed) / 2;
  if (pos <= mid) {
    update(2 * id, st, mid, pos, val);
  } else {
    update(2 * id + 1, mid + 1, ed, pos, val);
  }

  seg[id] = (seg[2 * id] + seg[2 * id + 1]) % mod;
}

int query(int id, int st, int ed, int lt, int rt) {
  if (rt < st || ed < lt) {
    return 0;
  }

  if (lt <= st && ed <= rt) {
    return seg[id];
  }

  int mid = (st + ed) / 2;
  int s1 = query(2 * id, st, mid, lt, rt);
  int s2 = query(2 * id + 1, mid + 1, ed, lt, rt);

  return ((s1 + s2) % mod);
}

ii a[11];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen ((file + ".inp").c_str(), "r", stdin);
  //    freopen ((file + ".out").c_str(), "w", stdout);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= k; i++) {
    cin >> a[i].fi >> a[i].se;
  }

  update(1, 1, n, 1, 1);
  for (int i = 2; i <= n; i++) {
    int f = 0;
    for (int j = 1; j <= k; j++) {
      (f += query(1, 1, n, max(i - a[j].se, 0LL), max(i - a[j].fi, 0LL))) %=
          mod;
    }

    update(1, 1, n, i, f);
  }

  cout << query(1, 1, n, n, n) << "\\n";

  //    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";

  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int siz = 2e5 + 10;
const int SIZ = 1e6 + 10;
const int mod = 998244353;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "3";

int n;
vector<int> mov;
int f[siz];

int dp(int pos) {
  if (pos == n) {
    return 1;
  }

  int &res = f[pos];
  if (res != -1) {
    return res;
  }

  res = 0;

  int p = upper_bound(all(mov), n - pos) - mov.begin() - 1;
  for (int i = p; i >= 0; i--) {
    int nxt = pos + mov[i];
    if (nxt <= n) {
      (res += dp(nxt)) %= mod;
    }
  }

  return res;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen ((file + ".inp").c_str(), "r", stdin);
  //    freopen ((file + ".out").c_str(), "w", stdout);

  int k;
  cin >> n >> k;

  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;

    for (int j = l; j <= r; j++) {
      mov.push_back(j);
    }
  }

  sort(all(mov));

  memset(f, -1, sizeof(f));

  cout << dp(1) << "\\n";

  //    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  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 { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  vector<mint> dp(n + 5), sum(n + 5);
  rep(i, k) cin >> l[i] >> r[i];
  dp[1] = 1;
  sum[1] = 1;
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int a = i - r[j];
      int b = i - l[j];
      a = max(a, 1);
      if (b < 0)
        continue;
      dp[i] += sum[b] - sum[a - 1];
    }
    sum[i] = dp[i] + sum[i - 1];
  }
  cout << dp[n] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> s;
  vector<int> dat(n + 10);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      s.push_back(j);
    }
  }
  sort(s.begin(), s.end());
  dat[1] = 1;
  rep(i, n) {
    if (dat[i] == 0) {
      continue;
    }
    for (int p = 0; p < (int)s.size(); p++) {
      if (i + p <= n) {
        dat[i + s[p]] += dat[i];
        dat[i + s[p]] %= MOD;
      } else {
        s.erase(s.begin() + p);
      }
    }
  }
  cout << dat[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const int MOD = 998244353;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k), dp(n + 5), sum(n + 5);
  rep(i, k) { cin >> l[i] >> r[i]; }

  dp[1]++;
  sum[1]++;
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int a = i - r[j];
      int b = i - l[j];
      a = max(a, 1);
      if (b < 0)
        continue;
      dp[i] += sum[b] - sum[a - 1];
      dp[i] = (dp[i] % MOD + MOD) % MOD;
    }
    sum[i] = dp[i] + sum[i - 1];
    sum[i] = (sum[i] % MOD + MOD) % MOD;
  }
  cout << (dp[n] + MOD) % MOD << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using p = pair<int, int>;
typedef long long ll;
const ll mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  int q = 0;
  rep(m, k) {
    int l, r;
    cin >> l >> r;
    for (int o = l; o < r; o++) {
      a[q] = o;
      q++;
    }
  }
  int g = a.size();
  int coun = 0;
  for (int s = 0; s < (1 << g); s++) {
    int sum = 0;
    for (int i = 0; i < g; i++) {
      if ((s >> i) & 1) {
        sum += a[i];
      }
    }
    if (sum == n - 1) {
      coun++;
      coun = coun % mod;
    }
  }
  cout << coun << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, k;
int l[200005], r[200005];

int dp[2000005];

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &l[i], &r[i]);
  }
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 1; j <= k; j++) {
      dp[i + l[j]] = (dp[i + l[j]] + dp[i]) % mod;
      dp[i + r[j] + 1] = (dp[i + r[j] + 1] - dp[i] + mod) % mod;
    }
  }
  printf("%d", dp[n]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, k;
int l[200005], r[200005];

int dp[200005];

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &l[i], &r[i]);
  }
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 1; j <= k; j++) {
      dp[i + l[j]] = (dp[i + l[j]] + dp[i]) % mod;
      dp[i + r[j] + 1] = (dp[i + r[j] + 1] - dp[i] + mod) % mod;
    }
  }
  printf("%d", dp[n]);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VPI = vector<PII>;
using VPL = vector<PLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define RREP(i, m, n) for (int i = (m); i >= (n); i--)

int main() {
  int n, k;
  cin >> n >> k;
  VI l(k), r(k);
  VLL dp(n + 10, 0);
  REP(i, 0, k) { cin >> l[i] >> r[i]; }
  dp[1] = 1;
  dp[2] = -1;

  REP(i, 1, n + 1) {
    dp[i] += dp[i - 1];
    dp[i] %= 998244353;
    if (dp[i] < 0) {
      dp[i] += 998244353;
    }
    REP(j, 0, k) {
      if (i + l[j] <= n) {
        dp[i + l[j]] += dp[i];
        dp[i + l[j]] %= 998244353;
      }
      if (i + r[j] + 1 <= n) {
        dp[i + r[j] + 1] -= dp[i];
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VPI = vector<PII>;
using VPL = vector<PLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
// i=m -> n-1(昇順)
#define RREP(i, m, n) for (int i = (m); i >= (n); i--)
// i=m -> n(降順)

int main() {
  int n, k;
  cin >> n >> k;
  VI l(k), r(k), list;
  VLL dp(n + 10, 0);
  REP(i, 0, k) {
    cin >> l[i] >> r[i];
    REP(j, l[i], r[i] + 1) {
      list.push_back(j);
      dp[j + 1]++;
    }
  }
  sort(list.begin(), list.end());

  REP(i, list[0], n) {
    for (int x : list) {
      if (i + x <= n) {
        dp[i + x] += dp[i];
        dp[i + x] %= 998244353;
      }
    }
  }

  cout << dp[n] << 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;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) ((x).begin()), ((x).end())

#define READ(x) (cin >> (x))
#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))
#define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No"))

#define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed

bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); }

#define DIVIDER 998244353

ll solve(ll N, ll K, vector<ll> &L, vector<ll> &R) {
  // vector<ll> num_list;
  // for (size_t i = 0; i < K; i++) {
  //   for (size_t j = L[i]; j <= R[i]; j++) {
  //     num_list.push_back(j);
  //   }
  // }
  vector<ll> pattern_count(N, 0);
  vector<ll> pattern_count_sum(N, 0);
  pattern_count[0] = 1;
  pattern_count_sum[0] = 1;

  for (ll i = 1; i <= N - 1; i++) {
    for (size_t j = 0; j < K; j++) {
      ll l = L[j];
      ll r = R[j];

      if (l > i) {
        continue;
      }
      if (r > i) {
        r = i;
      }

      pattern_count[i] += (pattern_count_sum[i - l] -
                           (r != i ? pattern_count_sum[i - r - 1] : 0)) +
                          DIVIDER;
    }

    pattern_count[i] %= DIVIDER;
    pattern_count_sum[i] =
        (pattern_count[i] + pattern_count_sum[i - 1]) % DIVIDER;
  }

  return pattern_count[N - 1];
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, K;
  cin >> N >> K;

  std::vector<ll> L(K, 0), R(K, 0);
  for (size_t i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  // main procedure
  ll ans = solve(N, K, L, R);

  // output
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) ((x).begin()), ((x).end())

#define READ(x) (cin >> (x))
#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))
#define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No"))

#define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed

bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); }

#define DIVIDER 998244353

ll solve(ll N, ll K, vector<ll> &L, vector<ll> &R) {
  //
  //
  vector<ll> num_list;
  for (size_t i = 0; i < K; i++) {
    for (size_t j = L[i]; j <= R[i]; j++) {
      num_list.push_back(j);
    }
  }
  sort(ALL(num_list));

  vector<ll> pattern_count(N, 0);
  pattern_count[0] = 1;
  for (ll i = 1; i <= N - 1; i++) {
    for (auto &&x : num_list) {
      if (x > i) {
        break;
      }

      pattern_count[i] += pattern_count[i - x];
    }

    pattern_count[i] %= DIVIDER;
  }

  return pattern_count[N - 1];
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, K;
  cin >> N >> K;

  std::vector<ll> L(K, 0), R(K, 0);
  for (size_t i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  // main procedure
  ll ans = solve(N, K, L, R);

  // output
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const ll LINF = LLONG_MAX;
const int INF = INT_MAX;
const double PI = acos(-1);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A;
  vector<pair<ll, ll>> LR;
  rep(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    LR.push_back(make_pair(a, b));
    rep(j, a, b + 1) { A.push_back(j); }
  }

  vector<ll> dp(200010, 0);
  dp[1]++;

  rep(i, 1, n + 10) {
    if (i != 2)
      dp[i] = (dp[i - 1] % MOD + dp[i] % MOD) % MOD;
    if (dp[i] >= 1) {
      for (auto p : LR) {
        ll l = p.first;
        ll r = p.second;
        if (l + i <= n + 10) {
          dp[l + i] = ((dp[l + i] + (dp[i] % MOD)) % MOD + MOD) % MOD;
        }
        if (r + i <= n + 10) {
          dp[r + i + 1] = ((dp[r + i + 1] - (dp[i] % MOD)) % MOD + MOD) % MOD;
        }
      }
    }
  }

  /* rep(i,1,n + 1){
          debug(dp[i]);
  }
  */

  ll ans = (dp[n] % MOD + MOD) % MOD;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const ll LINF = LLONG_MAX;
const int INF = INT_MAX;
const double PI = acos(-1);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A;

  rep(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    rep(j, a, b + 1) { A.push_back(j); }
  }

  vector<ll> dp(200010, 0);
  dp[1] = 1;

  rep(i, 1, n + 1) {
    for (auto j : A) {
      if (i - j >= 0) {
        dp[i] = (dp[i] + (dp[i - j] % MOD)) % MOD;
      }
    }
  }

  /* rep(i,0,n){
          debug(dp[i]);

  }
  */
  ll ans = dp[n] % MOD;
  cout << ans << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
// print(fixed << setprecision(10));
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repk(i, n, k) for (ll i = k; i < (n); i++)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl
double PI = acos(-1.0);
int INF = 1e9;
ll LINF = 1e18;
ll MOD = 1e9 + 7;
ll mods = 998244353;

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

  ll n;
  int k;
  cin >> n >> k;
  vector<ll> s(k * 2);
  ll l, r;
  rep(i, k) {
    cin >> l >> r;
    s[i * 2] = l;
    s[i * 2 + 1] = r;
  }
  ll sum = 0;
  vector<ll> tb(n + 1, 0);
  tb[1] = 1;
  tb[2] = -1;
  for (ll i = 1; i <= n; i++) {
    sum = (tb[i] + sum + mods) % mods;
    rep(j, k) {
      l = s[j * 2];
      r = s[j * 2 + 1];
      if (i + l <= n)
        tb[i + l] = (tb[i + l] + sum) % mods;
      if (i + r < n)
        tb[i + r + 1] = (tb[i + r + 1] - sum) % mods;
    }
  }
  print(sum);
  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;
// print(fixed << setprecision(10));
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repk(i, n, k) for (ll i = k; i < (n); i++)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl
double PI = acos(-1.0);
int INF = 1e9;
ll LINF = 1e18;
ll MOD = 1e9 + 7;
ll mods = 998244353;

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

  ll n;
  int k;
  cin >> n >> k;
  vector<ll> s(k * 2);
  ll l, r;
  rep(i, k) {
    cin >> l >> r;
    s[i * 2] = l;
    s[i * 2 + 1] = r;
  }
  vector<ll> tb(n + 1, 0);
  tb[1] = 1;
  for (ll i = 1; i < n; i++) {
    if (tb[i] == 0)
      continue;
    rep(j, k) {
      l = s[j * 2];
      r = s[j * 2 + 1];
      rep(m, r - l + 1) {
        if (i + l + m > n)
          break;
        tb[i + l + m] += tb[i];
        if (tb[i + l + m] > mods)
          tb[i + l + m] %= mods;
      }
    }
  }
  print(tb[n]);
  return 0;
}