Problem p02549 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define repi(n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(p) p.begin(), p.end()
#define pop_count(p) __builtin_popcountll(p)
#define len(s, e) ((e) - (s) + 1)
#define mid(s, e) ((s) + ((e) - (s)) / 2)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int mod = 998244353;
const int oo = 1e9;
const ll OO = 1e18;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
template <typename A, typename B>
std::istream &operator>>(std::istream &in, pair<A, B> &a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T> std::istream &operator>>(std::istream &in, vector<T> &a) {
  for (auto &v : a)
    in >> v;
  return in;
}
template <typename T, size_t N>
std::istream &operator>>(std::istream &in, array<T, N> &a) {
  for (size_t i = 0; i < N; i++)
    in >> a[i];
  return in;
}
template <typename A, typename B>
std::ostream &operator<<(std::ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
std::ostream &operator<<(std::ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
std::ostream &operator<<(std::ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const list<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
#ifdef ANEEE_LOCAL
template <typename T>
std::ostream &operator<<(std::ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
#else
template <typename T>
std::ostream &operator<<(std::ostream &out, const vector<T> &a) {
  for (const auto &v : a)
    out << v << ' ';
  return out;
}
#endif
#ifdef ANEEE_LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 7
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << "[" << name << ": " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << ": " << arg1 << "]  ";
  __f(comma + 2, args...);
}
template <typename Arg1> void print(Arg1 &&arg1) { cout << arg1 << endl; }
template <typename Arg1, typename... Args>
void print(Arg1 &&arg1, Args &&...args) {
  cout << arg1 << ' ';
  print(args...);
}
template <typename Arg1> void read(Arg1 &&arg1) { cin >> arg1; }
template <typename Arg1, typename... Args>
void read(Arg1 &&arg1, Args &&...args) {
  cin >> arg1;
  read(args...);
}
int nextInt() {
  int x;
  read(x);
  return x;
}
ll nextLong() {
  ll x;
  read(x);
  return x;
}

class Timer {
  string _name;
  clock_t _start;

public:
  Timer(string name = "Runtime") {
    _name = "[" + name + ": ", _start = clock();
  }
  ~Timer() {
    cerr << _name << double(clock() - _start) / CLOCKS_PER_SEC << "]\n";
  }
};

#ifdef ONLINE_JUDGE
#undef CASE_INFO
#endif

template <class val_t> struct segment_tree {
  int n;
  vector<val_t> T;

  template <typename X> void init(int sz, const vector<X> a) {
    n = sz;
    T.resize(2 * n);
    for (int i = 0; i < n; ++i) {
      T[n + i] = a[i];
    }
    for (int i = n - 1; i > 0; --i) {
      T[i] = T[i << 1] + T[i << 1 | 1];
      if (T[i] >= mod)
        T[i] -= mod;
    }
  }

  val_t query(int i) {
    val_t ret = 0;
    for (i += n; i > 0; i >>= 1) {
      ret += T[i];
      if (ret >= mod)
        ret -= mod;
    }
    return ret;
  }

  void update(int l, int r, val_t val) {
    for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        T[l] += val, T[l] -= (T[l] >= mod ? mod : 0), ++l;
      if (r & 1)
        --r, T[r] += val, T[r] -= (T[r] >= mod ? mod : 0);
    }
  }
};

void solve() {
  int n, k;
  read(n, k);
  vector<pair<int, int>> a(n);
  read(a);
  segment_tree<long long> st;
  st.init(n, vector<int>(n));
  st.update(0, 0, 1);
  for (int i = 0; i + 1 < n; ++i) {
    long long ways = st.query(i);
    trace(i, ways);
    for (const auto &ra : a) {
      st.update(i + ra.F, min(i + ra.S, n - 1), ways);
    }
  }
  print(st.query(n - 1));
}

signed main() {
  Timer ti;
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
#ifdef TEST_CASES
  cin >> T;
#endif
  for (int tt = 1; tt <= T; tt++) {
#ifdef CASE_INFO
    cout << "Case #" << tt << ": ";
#endif
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define repi(n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(p) p.begin(), p.end()
#define pop_count(p) __builtin_popcountll(p)
#define len(s, e) ((e) - (s) + 1)
#define mid(s, e) ((s) + ((e) - (s)) / 2)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int mod = 998244353;
const int oo = 1e9;
const ll OO = 1e18;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
template <typename A, typename B>
std::istream &operator>>(std::istream &in, pair<A, B> &a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T> std::istream &operator>>(std::istream &in, vector<T> &a) {
  for (auto &v : a)
    in >> v;
  return in;
}
template <typename T, size_t N>
std::istream &operator>>(std::istream &in, array<T, N> &a) {
  for (size_t i = 0; i < N; i++)
    in >> a[i];
  return in;
}
template <typename A, typename B>
std::ostream &operator<<(std::ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
std::ostream &operator<<(std::ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
std::ostream &operator<<(std::ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const list<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
#ifdef ANEEE_LOCAL
template <typename T>
std::ostream &operator<<(std::ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
#else
template <typename T>
std::ostream &operator<<(std::ostream &out, const vector<T> &a) {
  for (const auto &v : a)
    out << v << ' ';
  return out;
}
#endif
#ifdef ANEEE_LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 7
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << "[" << name << ": " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << ": " << arg1 << "]  ";
  __f(comma + 2, args...);
}
template <typename Arg1> void print(Arg1 &&arg1) { cout << arg1 << endl; }
template <typename Arg1, typename... Args>
void print(Arg1 &&arg1, Args &&...args) {
  cout << arg1 << ' ';
  print(args...);
}
template <typename Arg1> void read(Arg1 &&arg1) { cin >> arg1; }
template <typename Arg1, typename... Args>
void read(Arg1 &&arg1, Args &&...args) {
  cin >> arg1;
  read(args...);
}
int nextInt() {
  int x;
  read(x);
  return x;
}
ll nextLong() {
  ll x;
  read(x);
  return x;
}

class Timer {
  string _name;
  clock_t _start;

public:
  Timer(string name = "Runtime") {
    _name = "[" + name + ": ", _start = clock();
  }
  ~Timer() {
    cerr << _name << double(clock() - _start) / CLOCKS_PER_SEC << "]\n";
  }
};

#ifdef ONLINE_JUDGE
#undef CASE_INFO
#endif

template <class val_t> struct segment_tree {
  int n;
  vector<val_t> T;

  template <typename X> void init(int sz, const vector<X> a) {
    n = sz;
    T.resize(2 * n);
    for (int i = 0; i < n; ++i) {
      T[n + i] = a[i];
    }
    for (int i = n - 1; i > 0; --i) {
      T[i] = T[i << 1] + T[i << 1 | 1];
      if (T[i] >= mod)
        T[i] -= mod;
    }
  }

  val_t query(int i) {
    val_t ret = 0;
    for (i += n; i > 0; i >>= 1) {
      ret += T[i];
      if (ret >= mod)
        ret -= mod;
    }
    return ret;
  }

  void update(int l, int r, val_t val) {
    for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        T[l] += val, T[l] -= (T[l] >= mod ? mod : 0), ++l;
      if (r & 1)
        --r, T[r] += val, T[r] -= (T[r] >= mod ? mod : 0);
    }
  }
};

void solve() {
  int n, k;
  read(n, k);
  vector<pair<int, int>> a(k);
  read(a);
  segment_tree<long long> st;
  st.init(n, vector<int>(n));
  st.update(0, 0, 1);
  for (int i = 0; i + 1 < n; ++i) {
    long long ways = st.query(i);
    trace(i, ways);
    for (const auto &ra : a) {
      st.update(i + ra.F, min(i + ra.S, n - 1), ways);
    }
  }
  print(st.query(n - 1));
}

signed main() {
  Timer ti;
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
#ifdef TEST_CASES
  cin >> T;
#endif
  for (int tt = 1; tt <= T; tt++) {
#ifdef CASE_INFO
    cout << "Case #" << tt << ": ";
#endif
    solve();
  }
  return 0;
}