Problem p02549 (C++) — buggy submission, verdict: Runtime Error



/*
        ID: sixathofpain
        TASK: friday
        LANG: C++

*/

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p * p) % m;
  if (n % 2)
    return (p * a) % m;
  else
    return p;
}
const ll MOD = 998244353;
const int N = int(2e5) + 999;
#define int long long int

int ans[N];

int32_t main() {
  IOS;

  // freopen("friday.in","r",stdin);
  // freopen("friday.out","w",stdout);

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

  vector<pii> v(k);

  for (int i = 0; i < k; i++)
    cin >> v[i].ff >> v[i].ss;

  ans[1] = 1;
  ans[2] = -1;
  int tmp = 0;

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

    tmp = tmp + ans[i];
    tmp %= MOD;
    if (tmp < 0)
      tmp += MOD;

    ans[i] = tmp;

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

      int x = i + v[j].ff;
      int y = i + v[j].ss + 1;
      if (x <= n) {
        ans[x] += tmp;
        ans[x] %= MOD;
      }
      if (y <= n) {
        ans[y] -= tmp;
        ans[y] %= MOD;
      }

      if (x <= n) {
        if (ans[x] < 0)
          ans[x] += MOD;
      }

      if (y <= n) {
        if (ans[y] < 0)
          ans[y] += MOD;
      }
      // cerr<<"for i "<<x<<" "<<ans[x]<<" "<<y<<" "<<ans[y]<<endl;
    }
  }
  cout << ans[n] << endl;

  return 0;
}

Error: 0

Fixed submission:



/*
        ID: sixathofpain
        TASK: friday
        LANG: C++

*/

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p * p) % m;
  if (n % 2)
    return (p * a) % m;
  else
    return p;
}
const ll MOD = 998244353;
const int N = int(2e5) + 999;
#define int long long int

int ans[N];

int32_t main() {
  IOS;

  // freopen("friday.in","r",stdin);
  // freopen("friday.out","w",stdout);

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

  vector<pii> v(k);
  for (int i = 0; i < k; i++)
    cin >> v[i].ff >> v[i].ss;
  ans[1] = 1;
  ans[2] = -1;
  int tmp = 0;

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

    tmp = tmp + ans[i];
    tmp %= MOD;
    if (tmp < 0)
      tmp += MOD;

    // cout<<i<<" "<<ans[i]<<" "<<tmp<<endl;

    ans[i] = tmp;

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

      int x = i + v[j].ff;
      int y = i + v[j].ss + 1;
      if (x <= n) {
        ans[x] += tmp;
        ans[x] %= MOD;
      }
      if (y <= n) {
        ans[y] -= tmp;
        ans[y] %= MOD;
      }

      if (x <= n and ans[x] < 0)
        ans[x] += MOD;

      if (ans[y] < 0 and y <= n)
        ans[y] += MOD;
      // cerr<<"for i "<<x<<" "<<ans[x]<<" "<<y<<" "<<ans[y]<<endl;
    }
  }
  cout << ans[n] << endl;

  return 0;
}