Problem p02549 (C++)

Buggy submission (status: Time Limit Exceeded):

// Problem : D - Leaping Tak
// Contest : AtCoder - AtCoder Beginner Contest 179
// URL : https://atcoder.jp/contests/abc179/tasks/abc179_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 998244353
#define EPS 1e-9
#define inf 1e10
#define PI acos(-1.0)
#define int long long
#define N 100005

// Solution

int n, k;
vii p;

class SegTree {
  int tl, tr, sum;
  SegTree *left, *right;

public:
  SegTree(int l, int r) {
    sum = 0;
    tl = l;
    tr = r;
    left = nullptr;
    right = nullptr;
  }

  void makeChildren() {
    int tm = (tl + tr) / 2;
    left = new SegTree(tl, tm);
    right = new SegTree(tm + 1, tr);
  }

  void update(int pos, int val) {
    sum += val;
    if (tl == tr)
      return;
    if (left == nullptr)
      makeChildren();
    if (pos <= left->tr)
      left->update(pos, val);
    else
      right->update(pos, val);
  }

  int get(int l, int r) {
    if (l > tr || r < tl)
      return 0;
    if (tl >= l && tr <= r)
      return sum;
    if (left == nullptr)
      return 0;
    return left->get(l, r) + right->get(l, r);
  }
};

void solve() {
  cin >> n >> k;
  p.resize(n);
  for (auto &[a, b] : p) {
    cin >> a >> b;
  }
  SegTree *st = new SegTree(1, n);
  vector<int> dp(n + 1);
  dp[1] = 1;
  st->update(1, 1);
  for (int i = 2; i <= n; i++) {
    for (auto [a, b] : p) {
      int l = max(1ll, i - b);
      int r = max(0ll, i - a);
      dp[i] += st->get(l, r);
      dp[i] %= mod;
    }
    st->update(i, dp[i]);
  }
  cout << dp[n];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

  int tests;
  tests = 1;
  // cin>>tests;

  for (int test = 1; test <= tests; test++) {
    // cout<<"Case #"<<test<<": ";
    solve();
  }

  return 0;
}

Fixed submission:

// Problem : D - Leaping Tak
// Contest : AtCoder - AtCoder Beginner Contest 179
// URL : https://atcoder.jp/contests/abc179/tasks/abc179_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 998244353
#define EPS 1e-9
#define inf 1e10
#define PI acos(-1.0)
#define int long long
#define N 100005

// Solution

int n, k;
vii p;

class SegTree {
  int tl, tr, sum;
  SegTree *left, *right;

public:
  SegTree(int l, int r) {
    sum = 0;
    tl = l;
    tr = r;
    left = nullptr;
    right = nullptr;
  }

  void makeChildren() {
    int tm = (tl + tr) / 2;
    left = new SegTree(tl, tm);
    right = new SegTree(tm + 1, tr);
  }

  void update(int pos, int val) {
    sum += val;
    if (tl == tr)
      return;
    if (left == nullptr)
      makeChildren();
    if (pos <= left->tr)
      left->update(pos, val);
    else
      right->update(pos, val);
  }

  int get(int l, int r) {
    if (l > tr || r < tl)
      return 0;
    if (tl >= l && tr <= r)
      return sum;
    if (left == nullptr)
      return 0;
    return left->get(l, r) + right->get(l, r);
  }
};

void solve() {
  cin >> n >> k;
  p.resize(k);
  for (auto &[a, b] : p) {
    cin >> a >> b;
  }
  SegTree *st = new SegTree(1, n);
  vector<int> dp(n + 1);
  dp[1] = 1;
  st->update(1, 1);
  for (int i = 2; i <= n; i++) {
    for (auto [a, b] : p) {
      int l = max(1ll, i - b);
      int r = max(0ll, i - a);
      dp[i] += st->get(l, r);
      dp[i] %= mod;
    }
    st->update(i, dp[i]);
  }
  cout << dp[n];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

  int tests;
  tests = 1;
  // cin>>tests;

  for (int test = 1; test <= tests; test++) {
    // cout<<"Case #"<<test<<": ";
    solve();
  }

  return 0;
}