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

#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;
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int)x.size())
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n)                                                        \\
  vector<type> v(n);                                                           \\
  rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w)                                                     \\
  vector<vector<type>> v(h, vector<type>(w));                                  \\
  rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)                                                     \\
  std::vector<type> __VA_ARGS__;                                               \\
  MACRO_rdv2_Init(n, __VA_ARGS__);                                             \\
  for (int w_ = 0; w_ < n; ++w_) {                                             \\
    MACRO_rdv2_Scan(w_, __VA_ARGS__);                                          \\
  }
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x)                                                                \\
  {                                                                            \\
    wr(x);                                                                     \\
    return;                                                                    \\
  }
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; order_of_key (k) : Number of
// items strictly smaller than k . find_by_order(k) : K-th element in a set
// (counting from zero).
void rset();
void init_test();
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  init_test();
  return 0;
}
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> void MACRO_rdv2_Init(int n, T &t) { t.resize(n); }
template <typename First, typename... Rest>
void MACRO_rdv2_Init(int n, First &first, Rest &...rest) {
  first.resize(n);
  MACRO_rdv2_Init(n, rest...);
}
template <typename T> void MACRO_rdv2_Scan(int p, T &t) { std::cin >> t[p]; }
template <typename First, typename... Rest>
void MACRO_rdv2_Scan(int p, First &first, Rest &...rest) {
  std::cin >> first[p];
  MACRO_rdv2_Scan(p, rest...);
}
template <typename T> void wrv(const vector<T> &v) {
  rep(__ii, 0, sz(v)) {
    if (__ii)
      cout << ' ';
    cout << v[__ii];
  }
  cout << '\\n';
}
template <typename T> void wrm(const vector<vector<T>> &v) {
  rep(__ii, 0, sz(v)) {
    rep(__jj, 0, v[__ii].size()) {
      if (__jj)
        cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\\n';
  }
}
template <typename T> void sc(T &x) { cin >> x; }
template <typename Head, typename... Tail> void sc(Head &head, Tail &...tail) {
  cin >> head;
  sc(tail...);
}
template <typename T> void wr(const T &x) { cout << x << '\\n'; }
template <typename Head, typename... Tail>
void wr(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wr(tail...);
}
template <typename T> void wrf(const T &x) { cout << x << endl; }
template <typename Head, typename... Tail>
void wrf(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wrf(tail...);
}
template <typename T> void debug_out(const T &x) { cerr << x << '\\n'; }
template <typename Head, typename... Tail>
void debug_out(const Head &head, const Tail &...tail) {
  cerr << head << ' ';
  debug_out(tail...);
}
template <typename... T> void err(const T &...cod) {
  wr(cod...);
  exit(0);
}

const int mod = 998244353;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int mult(int x, int y) { return (1LL * x * y) % mod; }

int inv(int x) { return powmod(x, mod - 2); }

int divide(int x, int y) { return mult(x, inv(y)); }

struct fentree {
  vector<int> bit;
  int n;

  fentree() {
    this->n = 200 * 1000 + 10;
    bit.assign(n, 0);
  }

  fentree(int n) {
    this->n = n;
    bit.assign(n, 0);
  }

  int sum(int r) {
    if (r < 0)
      return 0;
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      add_self(ret, bit[r]);
    return ret;
  }

  void add(int idx, int delta) {
    if (idx < 0)
      return;
    for (; idx < n; idx = idx | (idx + 1))
      add_self(bit[idx], delta);
  }
};

void solve() {
  rd(int, n, k);
  vpii v;
  rep(i, 0, k) {
    rd(int, l, r);
    v.pb(mp(l, r));
  }

  fentree f(n + 1);
  f.add(1, 1);
  rep(i, 2, n + 1) {
    for (auto p : v) {
      f.add(i, f.sum(i - p.ff));
      f.add(i, sub(0, f.sum(i - p.ss - 1)));
    }
  }

  // rep(i, 1, n + 1) dbg(i, f.sum(i) - f.sum(i - 1));

  wr(sub(f.sum(n), f.sum(n - 1)));
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  // cin >> qq;
  while (qq--)
    solve();
}

Error: TLE

Fixed submission:

#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;
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int)x.size())
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n)                                                        \\
  vector<type> v(n);                                                           \\
  rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w)                                                     \\
  vector<vector<type>> v(h, vector<type>(w));                                  \\
  rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)                                                     \\
  std::vector<type> __VA_ARGS__;                                               \\
  MACRO_rdv2_Init(n, __VA_ARGS__);                                             \\
  for (int w_ = 0; w_ < n; ++w_) {                                             \\
    MACRO_rdv2_Scan(w_, __VA_ARGS__);                                          \\
  }
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x)                                                                \\
  {                                                                            \\
    wr(x);                                                                     \\
    return;                                                                    \\
  }
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; order_of_key (k) : Number of
// items strictly smaller than k . find_by_order(k) : K-th element in a set
// (counting from zero).
void rset();
void init_test();
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  init_test();
  return 0;
}
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> void MACRO_rdv2_Init(int n, T &t) { t.resize(n); }
template <typename First, typename... Rest>
void MACRO_rdv2_Init(int n, First &first, Rest &...rest) {
  first.resize(n);
  MACRO_rdv2_Init(n, rest...);
}
template <typename T> void MACRO_rdv2_Scan(int p, T &t) { std::cin >> t[p]; }
template <typename First, typename... Rest>
void MACRO_rdv2_Scan(int p, First &first, Rest &...rest) {
  std::cin >> first[p];
  MACRO_rdv2_Scan(p, rest...);
}
template <typename T> void wrv(const vector<T> &v) {
  rep(__ii, 0, sz(v)) {
    if (__ii)
      cout << ' ';
    cout << v[__ii];
  }
  cout << '\\n';
}
template <typename T> void wrm(const vector<vector<T>> &v) {
  rep(__ii, 0, sz(v)) {
    rep(__jj, 0, v[__ii].size()) {
      if (__jj)
        cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\\n';
  }
}
template <typename T> void sc(T &x) { cin >> x; }
template <typename Head, typename... Tail> void sc(Head &head, Tail &...tail) {
  cin >> head;
  sc(tail...);
}
template <typename T> void wr(const T &x) { cout << x << '\\n'; }
template <typename Head, typename... Tail>
void wr(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wr(tail...);
}
template <typename T> void wrf(const T &x) { cout << x << endl; }
template <typename Head, typename... Tail>
void wrf(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wrf(tail...);
}
template <typename T> void debug_out(const T &x) { cerr << x << '\\n'; }
template <typename Head, typename... Tail>
void debug_out(const Head &head, const Tail &...tail) {
  cerr << head << ' ';
  debug_out(tail...);
}
template <typename... T> void err(const T &...cod) {
  wr(cod...);
  exit(0);
}

const int mod = 998244353;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int mult(int x, int y) { return (1LL * x * y) % mod; }

int inv(int x) { return powmod(x, mod - 2); }

int divide(int x, int y) { return mult(x, inv(y)); }

const int N = 5e5 + 10;

void solve() {
  rd(int, n, k);
  vi s;
  rep(i, 0, k) {
    rd(int, l, r);
    per(j, r, l) s.pb(j);
  }

  vi f(n + 1);
  f[1] = 1;
  rep(i, 2, n + 1) {
    for (int x : s) {
      if (i - x >= 1) {
        add_self(f[i], f[i - x]);
      }
    }
  }

  wr(f[n]);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  // cin >> qq;
  while (qq--)
    solve();
}