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

#include <bits/stdc++.h>
#define for_int(i, a, b) for (int i = (a); i <= (b); ++i)
#define rep_int(i, a, b) for (int i = (a); i >= (b); --i)
#define for_ll(i, a, b) for (ll i = (a); i <= (b); ++i)
#define rep_ll(i, a, b) for (ll i = (a); i >= (b); --i)
#define IOS ios::sync_with_stdio(false);
#define ull unsigned long long
#define ll long long
#define db double
#define HPMAX 1005
#define KMP_MAX 1005
#define prime_MAX 1000005
#define quick_pow_mod 1000000007
#define PII pair<int, int>
const int factor_N = 10000005;
const int tree_arr_N = 1000005;
using namespace std;
/*
        1.注意边界情况
        2.优先考虑时间复杂度
!!! 3.求最大值时，答案应至多初始化为INT_MIN；求最小值时，答案应至少初始化为INT_MAX
        4.遇事不决先暴力
        5.代码要写简洁
*/
// 快读
int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  return x * f;
}
const int mod = 998244353;
int n, k;
int main() {
  n = read();
  k = read();
  vector<ll> dp(n, 0), l(k + 1, 0), r(n + 1, 0), sum(n + 1, 0);
  for_int(i, 1, k) l[i] = read(), r[i] = read();
  dp[0] = 1;
  for_ll(i, 0, n - 1) {
    if (i == 0)
      continue;
    sum[i] = sum[i - 1] + dp[i - 1];
    for_ll(j, 1, k) {
      if (l[j] > i)
        continue;
      int t = min(r[j], i) + 1;
      dp[i] += sum[i - l[j] + 1] - sum[i - t + 1];
    }
    dp[i] %= mod;
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#define for_int(i, a, b) for (int i = (a); i <= (b); ++i)
#define rep_int(i, a, b) for (int i = (a); i >= (b); --i)
#define for_ll(i, a, b) for (ll i = (a); i <= (b); ++i)
#define rep_ll(i, a, b) for (ll i = (a); i >= (b); --i)
#define IOS ios::sync_with_stdio(false);
#define ull unsigned long long
#define ll long long
#define db double
#define HPMAX 1005
#define KMP_MAX 1005
#define prime_MAX 1000005
#define quick_pow_mod 1000000007
#define PII pair<int, int>
const int factor_N = 10000005;
const int tree_arr_N = 1000005;
using namespace std;
/*
        1.注意边界情况
        2.优先考虑时间复杂度
!!! 3.求最大值时，答案应至多初始化为INT_MIN；求最小值时，答案应至少初始化为INT_MAX
        4.遇事不决先暴力
        5.代码要写简洁
*/
// 快读
int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  return x * f;
}
const int mod = 998244353;
int n, k, l, r;
unordered_map<int, bool> mp;
int main() {
  n = read();
  k = read();
  vector<int> dp(n + 1, 0), a;
  while (k--) {
    l = read();
    r = read();
    while (l <= r) {
      if (mp[l] != true)
        a.push_back(l), mp[l] = true;
      l++;
    }
  }
  sort(a.begin(), a.end());
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (auto step : a) {
      if (step >= i)
        break;
      dp[i] = (dp[i] + dp[i - step]) % mod;
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define int long long
#define mod 998244353
int L[11], R[11];
int dp[200005];
int sum[200005];

signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  for (int i = 1; i <= K; i++) {
    cin >> L[i] >> R[i];
  }
  sum[1] = dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 1; j <= K; j++) {
      if (i - L[j] < 1)
        continue;
      else if (i - R[j] < 1) {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
      } else {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
        dp[i] = (dp[i] - sum[i - R[j] - 1] + mod) % mod;
      }
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define int long long
#define mod 998244353
int L[11], R[11];
int dp[100005];
int sum[100005];

signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  for (int i = 1; i <= K; i++) {
    cin >> L[i] >> R[i];
  }
  sum[1] = dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 1; j <= K; j++) {
      if (i - L[j] < 1)
        continue;
      else if (i - R[j] < 1) {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
      } else {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
        dp[i] = (dp[i] - sum[i - R[j] - 1] + mod) % mod;
      }
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define INF ~(1 << 31)
#define INFL ~(1LL << 63)
#define mod 998244353LL

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> d(k);
  rep(i, k) { cin >> d[i].first >> d[i].second; }

  vector<ll> dp(n + 1);
  dp[1] = 1;
  reps(i, 2, n + 1) {
    rep(j, k) {
      int f = i - d[j].first, s = max(i - d[j].second, 1);
      if (f < 1)
        continue;
      else if (s == 1)
        dp[i] = (dp[i] + dp[f]) % mod;
      else
        dp[i] = (dp[i] - dp[s - 1] + dp[f]) % mod;
    }
    dp[i] = (dp[i] + dp[i - 1]) % mod;
  }
  if (dp[n] < dp[n - 1])
    dp[n] += mod;
  cout << dp[n] - dp[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define INF ~(1 << 31)
#define INFL ~(1LL << 63)
#define mod 998244353LL

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> d(k);
  rep(i, k) { cin >> d[i].first >> d[i].second; }

  vector<ll> dp(n + 1);
  dp[1] = 1;
  reps(i, 2, n + 1) {
    rep(j, k) {
      reps(l, d[j].first, d[j].second + 1) {
        if (i - l < 1)
          continue;
        dp[i] = (dp[i] + dp[i - l]) % mod;
      }
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<pair<int, int>> D(K);
  rep(i, K) cin >> D.at(i).first >> D.at(i).second;

  sort(D.begin(), D.end());

  ll mod = 998244353;

  vector<ll> dp(N + 1, 0);
  vector<ll> dp2(K, 0);
  dp.at(1) = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < K; j++) {
      pair<int, int> d = D.at(j);

      if (i - d.second >= 0) {
        dp2.at(j) -= dp.at(i - d.second);
      }

      if (i + 1 - d.first >= 0) {
        dp2.at(j) += dp.at(i + 1 - d.first);
      }

      while (dp2.at(j) < 0) {
        dp2.at(j) += mod;
      }
      dp2.at(j) %= mod;
      dp.at(i + 1) += dp2.at(j);
      dp.at(i + 1) %= mod;
    }
  }

  cout << dp.at(N) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  vector<pair<int, int>> D(K);
  rep(i, K) cin >> D.at(i).first >> D.at(i).second;

  sort(D.begin(), D.end());

  ll mod = 998244353;

  vector<ll> dp(N + 1, 0);
  dp.at(1) = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < K; j++) {
      pair<int, int> d = D.at(j);

      ll k_min = max(1, d.first);
      ll k_max = min(i, d.second);
      for (int k = k_min; k <= k_max; k++) {
        dp.at(i + 1) += dp.at(i + 1 - k);
        dp.at(i + 1) %= mod;
      }
    }
  }

  cout << dp.at(N) << endl;
  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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<mint> wa(N + 1, 0);
  wa.at(1) = 1;
  vector<P> p(K);
  int x, y;

  rep(i, K) {
    cin >> x >> y;
    p.at(i) = make_pair(x, y);
  }

  for (int i = 1; i < N; i++) {
    mint sum = 0;
    rep(j, K) {
      int r = max(i - (p.at(j).first) + 1, 0);
      int l = max(i - (p.at(j).second), 0);

      sum += wa.at(r) - wa.at(l);
    }
    wa.at(i + 1) = wa.at(i) + sum;
  }

  cout << wa.at(N) - wa.at(N - 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> dp(N, 0);
  dp.at(0) = 1;
  vector<int> S;
  int x, y;
  const int mod = 998244353;

  rep(i, K) {
    cin >> x >> y;
    for (int i = x; i <= y; i++)
      S.push_back(i);
  }
  sort(S.begin(), S.end());
  rep(i, N) {
    for (auto e : S) {
      if (i + e < N) {
        dp.at(i + e) += dp.at(i);
        dp.at(i + e) %= mod;
      } else
        break;
    }
  }
  cout << dp.at(N - 1) << endl;
  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++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;
int dp[200010];
int main() {

  int n, k;
  cin >> n >> k;
  vector<P> act;
  vector<int> sum(n + 1, 0);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    act.emplace_back(l, r);
  }

  dp[0] = 0;
  dp[1] = 1;
  sum[0] = 0;
  sum[1] = 1;
  for (int i = 2; i < n + 1; ++i) {
    for (auto a : act) {
      int l = a.first;
      int r = a.second;

      l = max(0, i - l);
      r = max(0, i - r - 1);
      int p = (sum[l] - sum[r]) % mod;
      if (p < 0)
        p += mod;
      dp[i] = (dp[i] + p) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int mod = 998244353;
int dp[200010];

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

  vector<P> act;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    act.emplace_back(l, r);
  }

  dp[0] = 1;
  rep(i, n) {
    for (auto a : act) {
      int l = a.first;
      int r = a.second;
      for (int j = l; j < r + 1; ++j) {
        int nx = i + j;
        if (nx >= n)
          continue;
        dp[nx] = (dp[nx] + dp[i]) % mod;
      }
    }
  }

  int ans = dp[n - 1];
  cout << ans << endl;

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

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

int main() {
  long long l, r, n, mod = 998244353, k, maxi1, maxi2;
  cin >> n >> k;
  vector<long long> a;
  vector<pair<long long, long long>> pr(k);
  for (int i = 0; i < k; i++) {
    cin >> pr[i].first >> pr[i].second;
    // for(int j=l;j<=r;j++) a.push_back(j);
  }
  // sort(a.begin(), a.end());
  vector<long long> dp(n + 1, 0);
  vector<long long> cumdp(n + 1, 0);
  dp[1] = 1;
  cumdp[1] = 1;
  for (int i = 2; i < n + 1; i++) {
    for (int j = 0; j < k; j++) {
      maxi1 = i - pr[j].second - 1;
      if (maxi1 < 0)
        maxi1 = 0;
      maxi2 = i - pr[j].first;
      if (maxi2 < 0)
        maxi2 = 0;
      dp[i] = ((dp[i] + cumdp[maxi2]) % mod - cumdp[maxi1] + mod) % mod;
    }
    //    cout<<dp[i]<<endl;
    cumdp[i] = (cumdp[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long l, r, n, mod = 998244353, k;
  cin >> n >> k;
  vector<long long> a;
  for (int i = 0; i < k; i++) {
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      a.push_back(j);
  }
  sort(a.begin(), a.end());
  vector<long long> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i < n + 1; i++) {
    for (int j = 0; j < a.size(); j++) {
      if (i - a[j] > 0)
        dp[i] = (dp[i] + dp[i - a[j]]) % mod;
    }
  }
  cout << dp[n] << endl;

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

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

using namespace std;
typedef long long ll;

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> ranges(K);
  vector<ll> diffs(N + 2, 0);
  vector<ll> paths(N + 2, 0);
  paths[1] = 1;
  ll cur = 0;
  for (ll i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    ranges[i] = make_pair(l, r);
  }
  for (ll i = 1; i <= N; i++) {
    cur += diffs[i];
    paths[i] += cur;
    paths[i] = paths[i] % 998244353;
    if (paths[i] > 0) {
      for (ll j = 0; j < K; j++) {
        if (i + ranges[j].first <= N) {
          diffs[i + ranges[j].first] += paths[i];
          diffs[min(N + 1, i + ranges[j].second + 1)] -= paths[i];
        }
      }
    }
  }
  cout << paths[N] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;
typedef long long ll;

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> ranges(K);
  vector<ll> diffs(N + 1, 0);
  vector<ll> paths(N + 1, 0);
  paths[1] = 1;
  ll cur = 0;
  for (ll i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    ranges[i] = make_pair(l, r);
  }
  for (ll i = 1; i <= N; i++) {
    cur += diffs[i];
    paths[i] += cur;
    paths[i] = paths[i] % 998244353;
    if (paths[i] > 0) {
      for (ll j = 0; j < K; j++) {
        if (i + ranges[j].first <= N) {
          diffs[i + ranges[j].first] += paths[i];
          diffs[min(N + 1, i + ranges[j].second + 1)] -= paths[i];
        }
      }
    }
  }
  cout << paths[N] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<int, int>> pos;
vector<int> dp;
const int M = 998244353;

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  cin >> n >> k;
  pos.resize(k);
  for (auto &i : pos)
    cin >> i.first >> i.second;
  sort(pos.begin(), pos.end());
  dp.resize(n + 2);
  dp[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    for (auto &j : pos) {
      if (i + j.first > n)
        break;
      int l = i + j.first, h = min(n, i + j.second);
      dp[i] = (dp[i] + (dp[l] - dp[h + 1])) % M;
      while (dp[i] < 0)
        dp[i] += M;
    }
    if (i == 1)
      break;
    dp[i] = (dp[i] + dp[i + 1]) % M;
  }
  cout << dp[1] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<int, int>> pos;
vector<int> dp;
const int M = 998244353;
int solve(int cpos) {
  if (cpos == n)
    return 1;
  if (cpos > n)
    return 0;
  if (dp[cpos] != -1)
    return dp[cpos];
  int ans = 0;
  for (auto &i : pos) {
    for (int j = i.first; j <= i.second; j++) {
      if (cpos + j > n)
        break;
      else
        ans = (ans + solve(cpos + j)) % M;
    }
  }
  return dp[cpos] = ans;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  cin >> n >> k;
  pos.resize(k);
  for (auto &i : pos)
    cin >> i.first >> i.second;
  sort(pos.begin(), pos.end());
  dp.resize(n + 1, -1);
  cout << solve(1) << "\\n";
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Mod = 998244353, N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
LL f[N], a[20], b[20], c[20];
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
  }
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j < m; ++j) {
      c[j] += (i - a[j] >= 1 ? f[i - a[j]] : 0) -
              (i - b[j] - 1 >= 1 ? f[i - b[j] - 1] : 0);
      f[i] = (f[i] + c[j]) % Mod;
    }
  }
  cout << f[n] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Mod = 998244353, N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
LL f[N], a[N], b[N], c[20];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
  }
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j < m; ++j) {
      c[j] += (i - a[j] >= 1 ? f[i - a[j]] : 0) -
              (i - b[j] - 1 >= 1 ? f[i - b[j] - 1] : 0);
      f[i] = (f[i] + c[j]) % Mod;
    }
  }
  cout << (f[n] + Mod) % Mod << endl;
  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 < (int)(n); ++i)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair

void solve(int test) {

  long long n;
  cin >> n;
  long long hell = 998244353;

  long long i, j;

  vector<long long> dp(n + 1, 0LL);

  long long k;
  cin >> k;

  vector<pair<long long, long long>> v(k);

  for (i = 0; i < k; i++) {
    cin >> v[i].first;
    cin >> v[i].second;
  }
  sort(v.begin(), v.end());

  dp[1] = 1;
  vector<long long> prefix(n + 2, 0LL);
  prefix[1] = 1LL;

  for (i = 2; i <= n; i++) {
    for (j = 0; j < v.size(); j++) {
      if (v[j].first >= i)
        break;
      long long l = max(1LL, i - v[j].first);
      long long r = max(1LL, i - v[j].second);

      // cout<<l<<" "<<r<<endl;

      dp[i] = (dp[i] + prefix[l] - prefix[r - 1]) % hell;
    }

    dp[i] = dp[i] % hell;
    if (dp[i] < 0)
      dp[i] = dp[i] + hell;
    dp[i] = dp[i] % hell;

    prefix[i] = (prefix[i - 1] + dp[i]) % hell;
  }

  cout << dp[n] << endl;

  return;
}

int main() {

  int t = 1;
  // cin>>t;

  for (int x = 1; x <= t; x++) {
    solve(x);
  }
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(int test) {

  long long n;
  cin >> n;
  long long hell = 998244353;

  long long i, j;

  vector<long long> dp(n + 1, 0LL);

  long long k;
  cin >> k;

  vector<pair<long long, long long>> v(k);

  vector<long long> moves;

  for (i = 0; i < k; i++) {
    cin >> v[i].first;
    cin >> v[i].second;

    for (j = v[i].first; j <= v[i].second; j++)
      moves.pb(j);
  }

  sort(moves.begin(), moves.end());

  dp[1] = 1;

  for (i = 2; i <= n; i++) {
    int lb = lower_bound(moves.begin(), moves.end(), i) - moves.begin();

    if (lb == 0)
      continue;
    for (j = 0; j < lb; j++) {
      dp[i] = (dp[i] + dp[i - moves[j]]) % hell;
    }
  }

  cout << dp[n] << endl;

  return;
}

int main() {

  int t = 1;
  // cin>>t;

  for (int x = 1; x <= t; x++) {
    solve(x);
  }
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;

const int MAXN = 2e5 + 5;
const int mod = 998244353;
LL tr[MAXN], dp[MAXN];
int n, k, L[15], R[15];
void update(int x, LL y) {
  for (; x <= n; x += (x & (-x)))
    tr[x] = (tr[x] + y) % mod;
}
LL find(int x) {
  if (!x)
    return 0;
  LL res = 0;
  for (; x; x -= (x & (-x)))
    res = (res + tr[x]) % mod;
  return res;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; ++i) {
    scanf("%d %d", &L[i], &R[i]);
  }
  dp[1] = 1;
  update(1, 1);
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i <= L[j])
        continue;
      dp[i] = (dp[i] + find(i - L[j]) - find(max(i - R[j] - 1, 0))) % mod;
      dp[i] = (dp[i] + mod) % mod;
    }
    update(i, dp[i]);
  }
  printf("%lld", dp[n]);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;

const int MAXN = 1e5 + 5;
const int mod = 998244353;
LL tr[MAXN], dp[MAXN];
int n, k, L[15], R[15];
void update(int x, LL y) {
  for (; x <= n; x += (x & (-x)))
    tr[x] = (tr[x] + y) % mod;
}
LL find(int x) {
  if (!x)
    return 0;
  LL res = 0;
  for (; x; x -= (x & (-x)))
    res = (res + tr[x]) % mod;
  return res;
}

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n + 1, 0LL);
  vector<ll> dpsum(n + 1, 0LL);
  vector<ll> L(k), R(k);
  rep(i, k) { cin >> L[i] >> R[i]; }
  dp[1] = 1;
  dpsum[1] = 1;
  rep2(now, 2, n + 1) {
    rep(i, k) {
      ll L1 = now - L[i];
      ll R1 = now - R[i] - 1;
      if (L1 < 0)
        continue;
      R1 = max(R1, 0LL);
      dp[now] += dpsum[L1] - dpsum[R1];
      dp[now] %= mod;
    }
    dpsum[now] = dpsum[now - 1] + dp[now];
  }
  cout << dp[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n + 1, 0);
  vector<ll> L(k), R(k);
  rep(i, k) { cin >> L[i] >> R[i]; }
  dp[1] = 1;
  rep2(now, 1, n + 1) {
    if (dp[now] == 0)
      continue;
    rep(i, k) {
      rep2(j, L[i], R[i] + 1) {
        if (now + j > n)
          break;
        dp[now + j] += dp[now];
        dp[now + j] %= 998244353;
      }
    }
  }
  cout << dp[n] << endl;

  return 0;
}