Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\\n'
// #define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                                                         \\
  do {                                                                         \\
    ostringstream os;                                                          \\
    os << setw(n) << setfill('0') << s;                                        \\
    s = os.str();                                                              \\
  } while (0);
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
#define INIT                                                                   \\
  std::cin.tie(nullptr);                                                       \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cout << std::fixed << std::setprecision(10);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T> using Pq = priority_queue<T>;
template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T> vector<T> make_vec(size_t a, T n) {
  return vector<T>(a, n);
}
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

constexpr ll mod = 998244353;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

class Fp {
private:
  long long val;

public:
  Fp() { val = 0; }
  Fp(long long v) : val(v % mod) {
    if (val < 0)
      val += mod;
  }
  Fp getVal() { return val; }
  Fp operator+=(const Fp &in) {
    val += in.val;
    if (val >= mod)
      val -= mod;
    return *this;
  }
  Fp operator+(const Fp &in) const { return Fp(*this) += in; }
  Fp &operator-=(const Fp &in) {
    val -= in.val;
    if (val < 0)
      val += mod;
    return *this;
  }
  Fp operator-(const Fp &in) const { return Fp(*this) -= in; }
  Fp &operator*=(const Fp &in) {
    val = val * in.val % mod;
    return *this;
  }
  Fp operator*(const Fp &in) const { return Fp(*this) *= in; }
  friend Fp operator*(const long long n, const Fp &in) { return in * n; }
  Fp &operator/=(const Fp &in) {
    *this *= in.modpow(mod - 2);
    return *this;
  }
  Fp operator/(const Fp &in) const { return Fp(*this) /= in; }
  bool operator==(const Fp &in) const { return val == in.val; }
  bool operator!=(const Fp &in) const { return val != in.val; }
  Fp modpow(long long n) const {
    if (n == 0)
      return 1;
    if (n & 1) {
      return (*this) * ((*this) * (*this)).modpow(n / 2);
    } else {
      return ((*this) * (*this)).modpow(n / 2);
    }
  }
  friend istream &operator>>(istream &is, Fp &fp) {
    is >> fp.val;
    return is;
  }
  friend ostream &operator<<(ostream &os, Fp &fp) {
    os << fp.val;
    return os;
  }
  friend ostream &operator<<(ostream &os, Fp &&fp) {
    os << fp.val;
    return os;
  }
};

Fp modpow(const Fp &in, const long long n) { return in.modpow(n); }
Fp modpow(const ll a, const long long n) { return Fp(a).modpow(n); }

using mint = Fp;

class Factorial {
public:
  vector<mint> fact, finv;
  Factorial(long long n) : fact(n + 1, mint(1)), finv(n + 1, mint(1)) {
    for (int i = 2; i <= n; i++) {
      mint tmp(i);
      fact[i] = fact[i - 1] * tmp;
      finv[i] = finv[i - 1] / tmp;
    }
  }
  mint perm(const long long n, const long long k) {
    return fact[n] * finv[n - k];
  }
  mint com(const long long n, const long long k) {
    if (n < k || n < 0 || k < 0)
      return mint(0);
    return fact[n] * finv[k] * finv[n - k];
  }
};

template <typename T> class segment_tree {
public:
  segment_tree(int n_)
      : n(__lowest_power_of_2(n_)), dat(n * 2, 0), lazy(n * 2, 0) {}

  template <typename Iterator>
  segment_tree(Iterator left, Iterator right)
      : n(__lowest_power_of_2(std::distance(left, right))), dat(n * 2, 0),
        lazy(n * 2, 0) {
    std::copy(left + n, right + n, dat.begin());
    for (int i = n - 1; i >= 1; --i) {
      dat[i] = std::max(dat[i * 2], dat[i * 2 + 1]);
    }
  }

private:
  int n;
  std::vector<T> dat, lazy;

public:
  void add(int ql, int qr, const T &x) { __add(1, 0, n, ql, qr, x); }

  T sum(int ql, int qr) { return __sum(1, 0, n, ql, qr); }

private:
  void __flush_lazy(int v, int l, int r) {
    dat[v] += lazy[v] * (r - l);
    if (v < n) {
      lazy[v * 2] += lazy[v];
      lazy[v * 2 + 1] += lazy[v];
    }
    lazy[v] = 0;
  }

  void __update_dat(int v) { dat[v] = dat[v * 2] + dat[v * 2 + 1]; }

  void __add(int n, int l, int r, const int ql, const int qr, const T &x) {
    __flush_lazy(n, l, r);
    if (r <= ql || qr <= l) {
      return;
    } else if (ql <= l && r <= qr) {
      lazy[n] += x;
      __flush_lazy(n, l, r);
    } else {
      int m = (l + r) / 2;
      __add(n * 2, l, m, ql, qr, x);
      __add(n * 2 + 1, m, r, ql, qr, x);
      __update_dat(n);
    }
  }

  T __sum(int n, int l, int r, const int ql, const int qr) {
    __flush_lazy(n, l, r);
    if (r <= ql || qr <= l) {
      return 0;
    } else if (ql <= l && r <= qr) {
      return dat[n];
    } else {
      int m = (l + r) / 2;
      T res_left = __sum(n * 2, l, m, ql, qr);
      T res_right = __sum(n * 2 + 1, m, r, ql, qr);
      __update_dat(n);
      return res_left + res_right;
    }
  }

  int __lowest_power_of_2(int x) const {
    int res = 1;
    while (res < x)
      res <<= 1;
    return res;
  }
};

signed main() {
  INIT;
  cin >> N >> K;
  vector<pi> lr(K);
  REP(i, K) { cin >> lr[i].first >> lr[i].second; }
  segment_tree<mint> st(N);
  dump("hoge");
  st.add(0, 1, 1);
  REP(i, N) {
    auto now = st.sum(i, i + 1);
    REP(j, K) {
      st.add(i + lr[j].first, min(i + lr[j].second + 1, int(N)), now);
    }
  }
  cout << st.sum(N - 1, N) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\\n'
// #define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                                                         \\
  do {                                                                         \\
    ostringstream os;                                                          \\
    os << setw(n) << setfill('0') << s;                                        \\
    s = os.str();                                                              \\
  } while (0);
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
#define INIT                                                                   \\
  std::cin.tie(nullptr);                                                       \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cout << std::fixed << std::setprecision(10);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T> using Pq = priority_queue<T>;
template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T> vector<T> make_vec(size_t a, T n) {
  return vector<T>(a, n);
}
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

constexpr ll MOD = 998244353;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

class Fp {
private:
  long long val;

public:
  static long long mod;

  Fp() { val = 0; }
  Fp(long long v) : val(v % mod) {
    if (val < 0)
      val += mod;
  }
  Fp getVal() { return val; }
  Fp operator+=(const Fp &in) {
    val += in.val;
    if (val >= mod)
      val -= mod;
    return *this;
  }
  Fp operator+(const Fp &in) const { return Fp(*this) += in; }
  Fp &operator-=(const Fp &in) {
    val -= in.val;
    if (val < 0)
      val += mod;
    return *this;
  }
  Fp operator-(const Fp &in) const { return Fp(*this) -= in; }
  Fp &operator*=(const Fp &in) {
    val = val * in.val % mod;
    return *this;
  }
  Fp operator*(const Fp &in) const { return Fp(*this) *= in; }
  friend Fp operator*(const long long n, const Fp &in) { return in * n; }
  Fp &operator/=(const Fp &in) {
    *this *= in.modpow(mod - 2);
    return *this;
  }
  Fp operator/(const Fp &in) const { return Fp(*this) /= in; }
  bool operator==(const Fp &in) const { return val == in.val; }
  bool operator!=(const Fp &in) const { return val != in.val; }
  Fp modpow(long long n) const {
    if (n == 0)
      return 1;
    if (n & 1) {
      return (*this) * ((*this) * (*this)).modpow(n / 2);
    } else {
      return ((*this) * (*this)).modpow(n / 2);
    }
  }
  friend istream &operator>>(istream &is, Fp &fp) {
    is >> fp.val;
    return is;
  }
  friend ostream &operator<<(ostream &os, Fp &fp) {
    os << fp.val;
    return os;
  }
  friend ostream &operator<<(ostream &os, Fp &&fp) {
    os << fp.val;
    return os;
  }
};

Fp modpow(const Fp &in, const long long n) { return in.modpow(n); }
Fp modpow(const ll a, const long long n) { return Fp(a).modpow(n); }

using mint = Fp;
long long mint::mod = MOD;

class Factorial {
public:
  vector<mint> fact, finv;
  Factorial(long long n) : fact(n + 1, mint(1)), finv(n + 1, mint(1)) {
    for (int i = 2; i <= n; i++) {
      mint tmp(i);
      fact[i] = fact[i - 1] * tmp;
      finv[i] = finv[i - 1] / tmp;
    }
  }
  mint perm(const long long n, const long long k) {
    return fact[n] * finv[n - k];
  }
  mint com(const long long n, const long long k) {
    if (n < k || n < 0 || k < 0)
      return mint(0);
    return fact[n] * finv[k] * finv[n - k];
  }
};

template <typename T> class segment_tree {
public:
  segment_tree(int n_)
      : n(__lowest_power_of_2(n_)), dat(n * 2, 0), lazy(n * 2, 0) {}

  template <typename Iterator>
  segment_tree(Iterator left, Iterator right)
      : n(__lowest_power_of_2(std::distance(left, right))), dat(n * 2, 0),
        lazy(n * 2, 0) {
    std::copy(left + n, right + n, dat.begin());
    for (int i = n - 1; i >= 1; --i) {
      dat[i] = std::max(dat[i * 2], dat[i * 2 + 1]);
    }
  }

private:
  int n;
  std::vector<T> dat, lazy;

public:
  void add(int ql, int qr, const T &x) { __add(1, 0, n, ql, qr, x); }

  T sum(int ql, int qr) { return __sum(1, 0, n, ql, qr); }

private:
  void __flush_lazy(int v, int l, int r) {
    dat[v] += lazy[v] * (r - l);
    if (v < n) {
      lazy[v * 2] += lazy[v];
      lazy[v * 2 + 1] += lazy[v];
    }
    lazy[v] = 0;
  }

  void __update_dat(int v) { dat[v] = dat[v * 2] + dat[v * 2 + 1]; }

  void __add(int n, int l, int r, const int ql, const int qr, const T &x) {
    __flush_lazy(n, l, r);
    if (r <= ql || qr <= l) {
      return;
    } else if (ql <= l && r <= qr) {
      lazy[n] += x;
      __flush_lazy(n, l, r);
    } else {
      int m = (l + r) / 2;
      __add(n * 2, l, m, ql, qr, x);
      __add(n * 2 + 1, m, r, ql, qr, x);
      __update_dat(n);
    }
  }

  T __sum(int n, int l, int r, const int ql, const int qr) {
    __flush_lazy(n, l, r);
    if (r <= ql || qr <= l) {
      return 0;
    } else if (ql <= l && r <= qr) {
      return dat[n];
    } else {
      int m = (l + r) / 2;
      T res_left = __sum(n * 2, l, m, ql, qr);
      T res_right = __sum(n * 2 + 1, m, r, ql, qr);
      __update_dat(n);
      return res_left + res_right;
    }
  }

  int __lowest_power_of_2(int x) const {
    int res = 1;
    while (res < x)
      res <<= 1;
    return res;
  }
};

signed main() {
  INIT;
  cin >> N >> K;
  vector<pi> lr(K);
  REP(i, K) { cin >> lr[i].first >> lr[i].second; }
  segment_tree<mint> st(N);
  dump("hoge");
  st.add(0, 1, 1);
  REP(i, N) {
    auto now = st.sum(i, i + 1);
    REP(j, K) {
      st.add(i + lr[j].first, min(i + lr[j].second + 1, int(N)), now);
    }
  }
  cout << st.sum(N - 1, N) << endl;
}