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

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pop_b pop_back
#define pf push_front
#define pop_f pop_front
#define mt make_tuple
#define eb emplace_back
#define sf(x) sizeof(x)
#define len(x) x.length()
#define sz(x) x.size()
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define LB(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define UB(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<set<ll>> vset;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef deque<ll> dql;
typedef deque<int> dqi;
typedef unsigned long long ull;
typedef tuple<int, int, int> State;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
  cout << "\\n";
}

#define vmax(type) std::numeric_limits<type>::max()
#define vmin(type) std::numeric_limits<type>::min()
#define each(a, x) for (auto &a : (x))
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (ll i = (l); i < (r); i++)
#define rep2r(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define range(a) a.begin(), a.end()
#define deb(x) cout << #x << " " << x << endl;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define cinal(a, n)                                                            \\
  vl a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define coutai(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define coutal(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define ent cout << endl;
#define resize_vec(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define ms(dp, x) memset(dp, x, sf(dp))
#define endl '\\n'

ll powm(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const ll mod = 998244353;
const ll N = 1000005;
const ll inf = 1e9;

ll n;
ll st[4 * N], ar[N], lazy[4 * N];

// si -> index of cell that we are at and we are going to fill
// ss -> start of current tree
// se -> end of our tree
//  [ss, se]
void build(int si, int ss, int se) {
  if (ss == se) {
    st[si] = ar[ss];
    return;
  }

  int mid = (ss + se) / 2;

  build(2 * si, ss, mid);
  build(2 * si + 1, mid + 1, se);

  st[si] = st[2 * si] + st[2 * si + 1];
}

int query(int si, int ss, int se, int qs, int qe) {
  if (lazy[si] != 0) {
    int dx = lazy[si] % mod;
    lazy[si] = 0;
    (st[si] += dx * (se - ss + 1)) % mod;

    if (ss != se) {
      (lazy[2 * si] += dx) % mod;
      (lazy[2 * si + 1] += dx) % mod;
    }
  }

  if (ss > qe || se < qs)
    return 0;

  if (ss >= qs && se <= qe)
    return st[si] % mod;

  int mid = (ss + se) / 2;
  return (query(2 * si, ss, mid, qs, qe) +
          query(2 * si + 1, mid + 1, se, qs, qe)) %
         mod;
}

void update(int si, int ss, int se, int qs, int qe, int val) {
  if (lazy[si] != 0) {
    int dx = lazy[si] % mod;
    lazy[si] = 0;
    (st[si] += dx * (se - ss + 1)) % mod;

    if (ss != se) {
      (lazy[2 * si] += dx) % mod;
      (lazy[2 * si + 1] += dx) % mod;
    }
  }

  if (ss > qe || se < qs)
    return;

  if (ss >= qs && se <= qe) {
    int dx = ((se - ss + 1) * val) % mod;
    (st[si] += dx) % mod;

    if (ss != se) {
      (lazy[2 * si] += val) % mod;
      (lazy[2 * si + 1] += val) % mod;
    }
    return;
  }

  int mid = (ss + se) / 2;
  update(2 * si, ss, mid, qs, qe, val);
  update(2 * si + 1, mid + 1, se, qs, qe, val);

  st[si] = (st[2 * si] + st[2 * si + 1]) % mod;
}

void solve() {
  ll k;
  cin >> n >> k;
  rep(i, 4 * n + 1) lazy[i] = 0;
  rep(i, 3 * n) ar[i + 1] = 0;
  ar[1] = 1;
  build(1, 1, 3 * n);
  vector<pair<int, int>> pr;
  rep(i, k) {
    int x, y;
    cin >> x >> y;
    pr.pb({x, y});
  }
  sort(range(pr));
  rep2(i, 1, n) {
    rep(j, k) {
      update(1, 1, n, i + pr[j].fi, i + pr[j].se, query(1, 1, n, i, i) % mod);
    }
  }
  cout << query(1, 1, n, n, n) % mod << endl;
}

// #define _DEBUG
// #define testCases
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  int tc = 1;
#ifdef testCases
  cin >> tc;
#endif
  while (tc--) {
#ifdef _DEBUG
    int tt = clock();
#endif
    solve();

#ifdef _DEBUG
    cerr << "TIME = " << clock() - tt << endl;
    tt = clock();
#endif
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pop_b pop_back
#define pf push_front
#define pop_f pop_front
#define mt make_tuple
#define eb emplace_back
#define sf(x) sizeof(x)
#define len(x) x.length()
#define sz(x) x.size()
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define LB(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define UB(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<set<ll>> vset;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef deque<ll> dql;
typedef deque<int> dqi;
typedef unsigned long long ull;
typedef tuple<int, int, int> State;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
  cout << "\\n";
}

#define vmax(type) std::numeric_limits<type>::max()
#define vmin(type) std::numeric_limits<type>::min()
#define each(a, x) for (auto &a : (x))
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (ll i = (l); i < (r); i++)
#define rep2r(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define range(a) a.begin(), a.end()
#define deb(x) cout << #x << " " << x << endl;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define cinal(a, n)                                                            \\
  vl a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define coutai(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define coutal(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define ent cout << endl;
#define resize_vec(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define ms(dp, x) memset(dp, x, sf(dp))
#define endl '\\n'

ll powm(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const ll mod = 998244353;
const ll N = 1000005;
const ll inf = 1e9;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> seg;
  rep(i, k) {
    int x, y;
    cin >> x >> y;
    rep2(j, x, y + 1) seg.pb(j);
  }
  vl dp(n + 1, 0);
  sort(range(seg));
  dp[1] = 1;
  rep2(i, 1, n) {
    for (int j : seg) {
      if (i + j <= n) {
        dp[i + j] += dp[i];
        dp[i + j] %= mod;
      } else
        break;
    }
  }
  cout << dp[n] << endl;
}

// #define _DEBUG
// #define testCases
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  int tc = 1;
#ifdef testCases
  cin >> tc;
#endif
  while (tc--) {
#ifdef _DEBUG
    int tt = clock();
#endif
    solve();

#ifdef _DEBUG
    cerr << "TIME = " << clock() - tt << endl;
    tt = clock();
#endif
  }
  return 0;
}