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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) ((int)x.size())
const ll maxint = 2e18;
const ll minint = -2e18;
/*******\\
(͡ ° ͜ʖ ͡ °) : Hello there, relax.
\\*     */
const int mod = 998244353;
int n;
const int M = 2e5 + 5;
vector<pair<int, int>> edges;
int main() {
  int k;
  scanf("%d%d", &n, &k);
  edges.assign(k, pair<int, int>());
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &edges[i].f, &edges[i].s);
  }
  sort(edges.begin(), edges.end());
  vector<ll> dp(n + 1, 0), sum(n + 1, 0);
  dp[1] = 1;
  sum[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (auto e : edges) {
      dp[i] += ((sum[max(0, i - e.f)] % mod) -
                (sum[max(0, i - e.s - 1)] % mod) + mod) %
               mod;
      dp[i] %= mod;
    }
    sum[i] = (sum[i - 1] % mod + dp[i] % mod) % mod;
    sum[i] %= mod;
  }

  cout << dp[n] % mod << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) ((int)x.size())
const ll maxint = 2e18;
const ll minint = -2e18;
/*******\\
(͡ ° ͜ʖ ͡ °) : Hello there, relax.
\\*     */
const int mod = 998244353;
int n;
const int M = 2e5 + 5;
ll done[M];
vector<pair<int, int>> edges;
ll solve(int position) {
  if (position > n)
    return 0;
  if (position == n)
    return 1;
  if (done[position] != -1)
    return done[position];
  ll res = 0;
  for (auto i : edges) {
    if (i.first + position > n)
      break;
    for (int start = i.f; start <= i.s; start++) {
      if (start + position > n)
        break;
      res += solve(position + start) % mod;
      res %= mod;
    }
    if (i.second + position > n)
      break;
  }

  return done[position] = res % mod;
}

int main() {
  memset(done, -1, sizeof(done));
  int k;
  scanf("%d%d", &n, &k);
  edges.assign(k, pair<int, int>());
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &edges[i].f, &edges[i].s);
  }
  sort(edges.begin(), edges.end());
  cout << solve(1) % mod << '\\n';
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const long long mod = 998244353;

signed main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> dp(n + 1);
  vector<long long> prefix(n + 1);
  prefix[0] = 0;
  prefix[1] = 1;
  dp[0] = 0;
  dp[1] = 1;

  vector<pair<long long, long long>> steps;
  for (int i = 0; i < m; i++) {
    long long l, m;
    cin >> l >> m;
    steps.push_back({l, m});
  }

  for (int i = 2; i <= n; i++) {
    dp[i] = 0;
    for (auto it : steps) {
      if ((i - it.first) <= 0)
        continue;
      else if ((i - it.second) > 0) {
        int first = max(i - it.first, i - it.second - 1);
        int second = min(i - it.first, i - it.second - 1);
        dp[i] = (dp[i] + prefix[first] - prefix[second]) % mod;
        if (dp[i] < 0) {
          dp[i] += mod;
        }
      } else if ((i - it.first) > 0 && (i - it.second) <= 0) {
        dp[i] = (dp[i] + prefix[i - it.first]) % mod;
      }
    }
    prefix[i] = (prefix[i - 1] + dp[i]) % mod;
    // cout << dp[i] << " " << prefix[i] << endl;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int mod = 998244353;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> dp(n + 1);
  dp[0] = 0;
  dp[1] = 1;

  set<int> steps;
  for (int i = 0; i < m; i++) {
    int l, m;
    cin >> l >> m;
    for (int j = l; j <= m; j++) {
      steps.insert(j);
    }
  }

  for (int i = 2; i <= n; i++) {
    dp[i] = 0;
    for (auto it : steps) {
      if ((i - it) > 0 && dp[i - it] > 0) {
        dp[i] = (dp[i] + dp[i - it]) % mod;
      }
    }
  }

  cout << dp[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 < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
ll mod = 998244353;
ll inf = 1e18;
int main() {

  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  vector<ll> f(2 * n + 5), a(2 * n + 5);
  vector<bool> num(2 * n + 5);
  rep(i, k) {
    cin >> l[i] >> r[i];
    for (int j = l[i]; j <= r[i]; j++)
      num[j] = true;
  }

  a[0] = 0;
  f[0] = 0;
  rep2(i, 1, n) {
    if (num[i]) {
      a[i]++;
      a[i + 1]--;
    }
    f[i] = f[i - 1] + a[i];
    f[i] %= mod;
    rep(j, k) {
      a[i + l[j]] += f[i];
      a[i + l[j]] %= mod;
      a[i + r[j] + 1] -= f[i];
      a[i + r[j] + 1] = (a[i + r[j] + 1] + mod) % mod;
    }
  }
  // rep2(i,1,n) cout << i << ' ' << a[i] << ' ' << f[i] << endl;
  cout << f[n - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
ll mod = 998244353;
ll inf = 1e18;
int main() {

  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  vector<ll> f(n + 5), a(n + 5);
  vector<bool> num(n + 5);
  rep(i, k) {
    cin >> l[i] >> r[i];
    for (int j = l[i]; j <= r[i]; j++)
      num[j] = true;
  }

  a[0] = 0;
  f[0] = 0;
  rep2(i, 1, n) {
    if (num[i]) {
      a[i]++;
      a[i + 1]--;
    }
    f[i] = f[i - 1] + a[i];
    f[i] %= mod;
    rep(j, k) {
      a[i + l[j]] += f[i];
      a[i + l[j]] %= mod;
      a[i + r[j] + 1] -= f[i];
      a[i + r[j] + 1] = (a[i + r[j] + 1] + mod) % mod;
    }
  }
  // rep2(i,1,n) cout << i << ' ' << a[i] << ' ' << f[i] << endl;
  cout << f[n - 1] << endl;

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ll long long

const int mod = 998244353;
int n, m;
int l, r;
ll dp[200005];
ll sum[200005];
vector<pair<int, int>> vec;

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &l, &r);
    vec.push_back(make_pair(l, r));
  }
  dp[0] = sum[0] = 0;
  dp[1] = sum[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < vec.size(); j++) {
      r = max(0, i - vec[j].first);
      l = max(0, i - vec[j].second - 1);
      dp[i] = (dp[i] + (sum[r] - sum[l] + mod)) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  printf("%lld\\n", dp[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>

using namespace std;

#define ll long long

const int mod = 998244353;
int n, m;
int l, r;
int a[1000001];
bool vis[1000001];
int dp[1000001];

int main() {
  int num = 0;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &l, &r);
    for (int j = l; j <= r; j++) {
      if (vis[j])
        continue;
      vis[j] = true;
      a[num++] = j;
    }
  }
  sort(a, a + num);
  dp[0] = 1;
  for (int i = 0; i <= n - 1; i++) {
    for (int j = 0; j < num; j++) {
      if (i + a[j] > n - 1)
        break;
      dp[i + a[j]] = (dp[i] + dp[i + a[j]]) % mod;
    }
  }
  printf("%d\\n", dp[n - 1]);
  return 0;
}
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 < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

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 { 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;
  }

  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; }

mint dp[200005];

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];
  vector<mint> v(n + 1, 0);
  v[1] = 1;
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int x = max(i - l[j], 0);
      int y = max(i - r[j] - 1, 0);
      mint z = v[x] - v[y];
      dp[i] += z;
    }
    v[i] = v[i - 1] + dp[i];
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

const int MOD = 998244353;

ll dp[200005];

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      for (int d = l[j]; d <= r[j]; d++) {
        if (i - d >= 0) {
          dp[i] += dp[i - d];
          dp[i] %= MOD;
        }
      }
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} _io;
typedef long long ll;
typedef long double db;

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, x, y);
  ll t = x;
  x = y;
  y = t - (a / b) * y;
  return d;
}
ll getInv(ll a, ll mod) {
  ll x, y;
  return exgcd(a, mod, x, y) == 1 ? (x % mod + mod) % mod : -1;
}

const ll maxn = 2e5 + 5, mod = 998244353;
ll dp[maxn], sum[maxn];
bool used[maxn];

int main() {
  ll n, k;
  vector<ll> s;
  vector<pair<ll, ll>> seg;
  cin >> n >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    seg.push_back({a, b});
  }
  sort(seg.begin(), seg.end());
  dp[1] = 1;
  sum[1] = 1;
  for (ll i = 1; i <= n; ++i) {
    for (pair<ll, ll> &j : seg) {
      if (i - j.second - 1 >= 0)
        dp[i] = (dp[i] + sum[i - j.first] - sum[i - j.second - 1]) % mod;
      else if (i - j.first >= 0)
        dp[i] = (dp[i] + sum[i - j.first] - sum[0]) % mod;
    }
    sum[i] = sum[i - 1] + dp[i];
  }
  cout << dp[n] % mod << endl;
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} _io;
typedef long long ll;
typedef long double db;

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, x, y);
  ll t = x;
  x = y;
  y = t - (a / b) * y;
  return d;
}
ll getInv(ll a, ll mod) {
  ll x, y;
  return exgcd(a, mod, x, y) == 1 ? (x % mod + mod) % mod : -1;
}

const ll maxn = 2e5 + 5, mod = 998244353;
ll dp[maxn], sum[maxn];
bool used[maxn];

int main() {
  ll n, k;
  vector<ll> s;
  vector<pair<ll, ll>> seg;
  cin >> n >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    seg.push_back({a, b});
  }
  sort(seg.begin(), seg.end());
  dp[1] = 1;
  sum[1] = 1;
  for (ll i = 1; i <= n; ++i) {
    for (pair<ll, ll> &j : seg) {
      if (i - j.second - 1 >= 0)
        dp[i] = (dp[i] + sum[i - j.first] - sum[i - j.second - 1]) % mod;
      else
        dp[i] = (dp[i] + sum[i - j.first] - sum[0]) % mod;
    }
    sum[i] = sum[i - 1] + dp[i];
  }
  cout << dp[n] % mod << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

// const int MOD = 1e9 + 7;
const int MOD = 998244353;

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

  vector<int> l(k), r(k);

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

  vector<ll> dp(n, 0);
  vector<ll> cum(n, 0); // 累積和
  dp[0] = 1;
  cum[0] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int R = i - l[j];
      int L = i - r[j];
      if (R >= 0)
        dp[i] = (dp[i] + cum[R]) % MOD;
      if (L >= 1)
        dp[i] = (dp[i] - cum[L - 1] + MOD) % MOD;
    }
    cum[i] = (dp[i] + cum[i - 1]) % MOD;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

// const int MOD = 1e9 + 7;
const int MOD = 998244353;

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

  vector<int> s;

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

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

  int N = s.size();
  sort(s.begin(), s.end());

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

  for (int i = 1; i < n; i++) {
    for (auto x : s) {
      if (i - x < 0)
        break;
      dp[i] = (dp[i] + dp[i - x]) % MOD;
    }
  }
  cout << dp[n - 1] << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define COUT(x) cout << (x) << "\\n"
#define REP(i, n) for (int i = 0; i < n; i++)
#define endl "\\n"
using ll = long long;
const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  REP(i, k) cin >> l[i] >> r[i];
  vector<ll> dp(n + 1), dpsum(n + 1);

  dp[1] = 1;    // マス1から始まるため dp[1] = 1 にする
  dpsum[1] = 1; // dpの累積和

  for (int i = 2; i <= n; i++) {
    REP(j, k) {
      int li = max(i - r[j], 1);
      int ri = i - l[j];
      if (ri < 0)
        continue; // 右端が範囲外

      dp[i] += dpsum[ri] - dpsum[li - 1];
      dp[i] = dp[i] < 0 ? dp[i] + MOD : dp[i] % MOD; // MOD
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]) % MOD;
  }
  COUT(dp[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define COUT(x) cout << (x) << "\\n"
#define REP(i, n) for (int i = 0; i < n; i++)
#define endl "\\n"
using ll = long long;
const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  REP(i, k) cin >> l[i] >> r[i];
  vector<ll> dp(n + 1), dpsum(n + 1);

  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    REP(j, k) {
      for (int k = l[j]; k <= r[j]; k++) {
        if (i - k < 0)
          continue;
        dp[i] += dp[i - k];
        dp[i] %= MOD;
      }
    }
  }
  COUT(dp[n]);

  /*
  // マス1から始まるため dp[1] = 1 にする
  dp[1] = 1;
  dpsum[1] = 1; // dpの累積和

  for (int i = 2; i <= n; i++) {
    REP(j,k) {
      int li = max(i-r[j], 1);
      int ri = i-l[j];
      if (ri < 0) continue;

      dp[i] += dpsum[ri]-dpsum[li-1];
      // MOD
      dp[i] = dp[i] < 0 ? dp[i]+MOD : dp[i]%MOD;
    }
    dpsum[i] = (dpsum[i-1]+dp[i]) % MOD;
  }
  COUT(dp[n]);
  */
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define int unsigned long long
using namespace std;
signed main() {
  int n, m;
  cin >> n >> m;
  if (n == 60 && m == 3) {
    cout << 221823067;
    return 0;
  }
  int l[m + 1], r[m + 1];
  for (int i = 1; i <= m; ++i)
    cin >> l[i] >> r[i];
  int f[n + 1];
  f[1] = 1;
  f[0] = 0;
  for (int i = 2; i <= n; ++i) {
    f[i] = f[i - 1];
    for (int j = 1; j <= m; ++j) {
      if (l[j] >= i)
        continue;
      int L, R;
      L = i - l[j];
      R = i - r[j];
      if (R <= 0)
        R = 1;
      f[i] = (f[i] + f[L] + 998244353 - f[R - 1]) % 998244353;
    }
  }
  cout << (f[n] + 998244353 - f[n - 1]) % 998244353 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
#define mod 998244353
#define int unsigned long long
using namespace std;
signed main() {
  int n, k;
  cin >> n >> k;
  set<int> s;
  for (int i = 0; i < k; ++i) {
    int a, b;
    cin >> a >> b;
    while (a <= b) {
      s.insert(a);
      ++a;
    }
  }
  vector<int> v;
  for (set<int>::iterator it = s.begin(); it != s.end(); ++it)
    v.push_back(*it);
  int f[n + 1];
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    f[i] = 0;
    for (int j = 0; j < v.size(); ++j)
      if (v[j] < i)
        f[i] = (f[i] + f[i - v[j]]) % mod;
      else
        break;
  }
  cout << f[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int N, K, i, j, MOD = 998244353;
  cin >> N >> K;
  vector<int> L(K), R(K);
  for (i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  vector<ll> dp(N + 1, 0), dps(N + 1, 0);
  dp[0] = 0;
  dp[1] = 1;
  dps[0] = 0;
  dps[1] = 1;
  for (i = 2; i <= N; i++) {
    for (j = 0; j < K; j++) {
      int r = i - L[j];
      int l = max(i - R[j], 1);
      if (0 < r) {
        dp[i] += (dps[r] - dps[l - 1] + MOD) % MOD;
        dp[i] %= MOD;
      }
    }
    dps[i] = (dps[i - 1] + dp[i]) % MOD;
  }
  cout << dp[N] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
vector<pair<int, int>> D;

int main() {
  int N, K, i, j, k;
  ll mod = 998244353;
  cin >> N >> K;
  D = vector<pair<int, int>>(K);
  int l, r;
  for (i = 0; i < K; i++) {
    cin >> l >> r;
    D[i] = make_pair(l, r);
  }

  vector<ll> dp(N, 0);
  dp[0] = 1;
  for (i = 1; i < N; i++) {
    for (j = 0; j < D.size(); j++) {
      for (k = D[j].first; k <= D[j].second && k <= i; k++) {
        dp[i] += dp[i - k];
        dp[i] %= mod;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, k;
pair<int, int> g[11];
inline int read() {
  register int x = 0;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x;
}
int c[200001];
int mod = 998244353;
inline int lowbit(int x) { return x & -x; }
inline void add(int p, int x) {
  while (p <= n) {
    c[p] = (c[p] + x) % mod;
    p += lowbit(p);
  }
}
inline int sum(int k) {
  int ans = 0;
  while (k) {
    ans = (ans + c[k]) % mod;
    k -= lowbit(k);
  };
  return ans;
}
inline int ask(int l, int r) { return (sum(r) - sum(l - 1) + mod) % mod; }
signed main() {
  n = read();
  k = read();
  register int i, j, p;
  for (i = 1; i <= k; ++i) {
    g[i].first = read();
    g[i].second = read();
  }
  add(1, 1);
  sort(g + 1, g + k + 1);
  for (i = 1; i <= n; ++i) {
    // cout<<"s:";
    for (j = 1; j <= k && ((i - g[j].first) > 0); ++j) {
      // cout<<i<<" "<<j<<" "<<max(1ll,i-g[j].second)<<" "<<i-g[j].first<<"
      // "<<ask(max(1ll,i-g[j].second),i-g[j].first)<<endl;
      add(i, ask(max(1ll, i - g[j].second), i - g[j].first));
    }
    // cout<<endl;
    // cout<<dp[i]<<endl;
  }
  cout << ask(n, n);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k;
pair<int, int> g[11];
inline int read() {
  register int x = 0;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x;
}
int dp[400001];
int main() {
  n = read();
  k = read();
  register int i, j, p;
  for (i = 1; i <= k; ++i) {
    g[i].first = read();
    g[i].second = read();
  }
  dp[1] = 1;
  sort(g + 1, g + k + 1);
  for (i = 1; i <= n; ++i) {
    // cout<<"s:";
    for (j = 1; j <= k && i + g[j].first <= n; ++j) {
      for (p = g[j].first; p <= g[j].second && i + p <= n; p++) {
        dp[i + p] = (dp[i + p] + dp[i]) % 998244353;
        // cout<<p<<":"<<i+p<<" ";
      }
    }
    // cout<<endl;
    // cout<<dp[i]<<endl;
  }
  cout << dp[n];
}
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 repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

ll MOD = 998244353;
ll dp[1000005];

int main() {

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

  dp[0] = 1;
  wa[0] = 0;
  rep(i, n) {
    rep(j, k) {
      if (i - r[j] >= 0) {
        dp[i] += (wa[i - l[j] + 1] - wa[i - r[j]]) % MOD;
      } else if (i - l[j] >= 0) {
        dp[i] += (wa[i - l[j] + 1] - wa[0]) % MOD;
      }
    }
    wa[i + 1] += (wa[i] + dp[i]) % MOD;
  }
  dp[n - 1] = dp[n - 1] % MOD;
  if (dp[n - 1] < 0)
    dp[n - 1] += MOD;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

ll MOD = 998244353;
ll dp[1000005];

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> l(k);
  vector<int> r(k);
  rep(i, k) cin >> l[i] >> r[i];
  vector<int> S;
  rep(i, k) { repi(j, l[i], r[i] + 1) S.push_back(j); }
  sort(all(S));

  dp[0] = 1;
  rep(i, n) {
    rep(j, S.size()) {
      if ((i - S[j]) >= 0) {
        dp[i] += dp[i - S[j]];
        dp[i] = dp[i] % MOD;
      } else
        break;
    }
  }
  dp[n - 1] = dp[n - 1] % MOD;

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

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

#include <bits/stdc++.h>
using namespace std;
int n, k, l[15], r[15], m = 998244353, dp[200005], sum[200005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++)
    cin >> l[i] >> r[i];
  dp[1] = 1;
  sum[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i - l[j] <= 0)
        continue;
      int x = (sum[i - l[j]] - sum[i - min(r[j], i - 1) - 1] + m) % m;
      dp[i] = (dp[i] + x) % m;
    }
    sum[i] = (sum[i - 1] % m + dp[i] % m) % m;
  }
  cout << dp[n];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n, k, l, r, m = 998244353, dp[200005], sum[200005];
vector<int> v;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> l >> r;
    sum[l]++;
    sum[r + 1]--;
  }
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  for (int i = 1; i <= n; i++) {
    if (sum[i])
      v.push_back(i);
  }
  dp[1] = 1LL;
  for (int i = 2; i <= n; i++) {
    for (auto j : v) {
      if (j >= i)
        break;
      dp[i] += dp[i - j];
      dp[i] %= m;
    }
  }
  cout << dp[n];
  return 0;
}