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

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const ll mod = 998244353;
const ll maxn = 2e5 + 10;

struct tnode {
  ll w, l, r, siz, alazy;
  ll sq, mlazy = 1;
} tn[maxn << 2];

ll n, m;

ll wt[maxn];

void pushup(ll u) {
  tn[u].w = (tn[u << 1].w + tn[u << 1 | 1].w) % mod;
  tn[u].sq = (tn[u << 1].sq + tn[u << 1 | 1].sq) % mod;
}

void build(ll u, ll l, ll r) {
  tn[u].l = l;
  tn[u].r = r;
  tn[u].siz = r - l + 1;
  if (l == r) {
    tn[u].w = wt[l];
    tn[u].sq = wt[l] * wt[l];
    return;
  }
  ll mid = (l + r) >> 1;
  build(u << 1, l, mid);
  build(u << 1 | 1, mid + 1, r);
  pushup(u);
}

void pushdown(ll u) {
  if (tn[u].alazy != 0 or tn[u].mlazy != 1) {
    tn[u << 1].sq =
        ((tn[u << 1].sq + 2LL * tn[u << 1].w * tn[u].alazy % mod) % mod +
         tn[u].alazy * tn[u].alazy % mod * tn[u << 1].siz % mod) %
        mod;
    tn[u << 1 | 1].sq =
        ((tn[u << 1 | 1].sq + 2LL * tn[u << 1 | 1].w * tn[u].alazy % mod) %
             mod +
         tn[u].alazy * tn[u].alazy % mod * tn[u << 1 | 1].siz % mod) %
        mod;

    tn[u << 1].w = (tn[u << 1].w * tn[u].mlazy % mod +
                    tn[u << 1].siz * tn[u].alazy % mod) %
                   mod;
    tn[u << 1 | 1].w = (tn[u << 1 | 1].w * tn[u].mlazy % mod +
                        tn[u << 1 | 1].siz * tn[u].alazy % mod) %
                       mod;

    tn[u << 1].alazy =
        (tn[u << 1].alazy * tn[u].mlazy % mod + tn[u].alazy) % mod;
    tn[u << 1 | 1].alazy =
        (tn[u << 1 | 1].alazy * tn[u].mlazy % mod + tn[u].alazy) % mod;

    tn[u << 1].mlazy = (tn[u << 1].mlazy * tn[u].mlazy) % mod;
    tn[u << 1 | 1].mlazy = (tn[u << 1 | 1].mlazy * tn[u].mlazy) % mod;

    tn[u].alazy = 0;
    tn[u].mlazy = 1;
  }
}

ll query(ll u, ll l, ll r, ll t) {
  if (l <= tn[u].l and r >= tn[u].r) {
    if (t == 1)
      return tn[u].w;
    if (t == 2)
      return tn[u].sq;
  }

  ll ans = 0;
  pushdown(u);
  ll mid = (tn[u].l + tn[u].r) >> 1;
  if (l <= mid)
    ans = (ans + query(u << 1, l, r, t)) % mod;
  if (r > mid)
    ans = (ans + query(u << 1 | 1, l, r, t)) % mod;
  return ans;
}

void update(ll u, ll l, ll r, ll wa, ll wm) {
  if (l <= tn[u].l and r >= tn[u].r) {
    tn[u].sq = ((tn[u].sq + 2LL * tn[u].w * wa % mod) % mod +
                wa * wa % mod * tn[u].siz % mod) %
               mod;
    tn[u].w = (tn[u].w * wm % mod + tn[u].siz * wa % mod) % mod;

    tn[u].alazy = (tn[u].alazy * wm % mod + wa) % mod;
    tn[u].mlazy = (tn[u].mlazy * wm) % mod;

    return;
  }
  pushdown(u);
  ll mid = (tn[u].l + tn[u].r) >> 1;
  if (l <= mid)
    update(u << 1, l, r, wa, wm);
  if (r > mid)
    update(u << 1 | 1, l, r, wa, wm);
  pushup(u);
}

/*
 * cin>>n>>m;
    for(ll i=1;i<=n;i++){
        cin>>wt[i];
    }

    build(1,1,n);
    while(m--){
        ll t;
        cin>>t;
        switch(t){
            case 1:{
                ll x,y;
                cin>>x>>y;
                cout<<query(1,x,y,1)<<endl;
                break;
            }
            case 2:{
                ll x,y;
                cin>>x>>y;
                cout<<query(1,x,y,2)<<endl;
                break;
            }
            case 3:{
                ll x,y,z;
                cin>>x>>y>>z;
                update(1,x,y,0,z%mod);
                break;
            }
            case 4:{
                ll x,y,z;
                cin>>x>>y>>z;
                update(1,x,y,z%mod,1);
                break;
            }

        }


    }
 */

int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif

  ll k;
  cin >> n >> k;
  wt[1] = 1;
  build(1, 1, n);

  vector<pair<ll, ll>> vt;
  for (int i = 0; i < k; ++i) {
    ll u, v;
    cin >> u >> v;
    vt.emplace_back(u, v);
  }
  sort(vt.begin(), vt.end());
  for (ll i = 1; i < n; ++i) {
    for (auto j : vt) {
      if (i + j.second > n) {
        if (i + j.first <= n) {
          update(1, i + j.first, n, query(1, i, i, 1) % mod, 1);
        }
        break;
      } else {
        update(1, i + j.first, i + j.second, query(1, i, i, 1) % mod, 1);
      }
    }
  }
  cout << query(1, n, n, 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
ll mod = 998244353;
ll dp[200005];
vector<ll> vt;
int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif

  ll n;
  cin >> n;
  dp[1] = 1;
  ll num;
  cin >> num;
  for (ll i = 0; i < num; ++i) {
    ll l, r;
    scanf("%lld %lld", &l, &r);
    for (ll j = l; j <= r; ++j) {
      vt.push_back(j);
    }
  }
  sort(vt.begin(), vt.end());
  for (ll i = 1; i < n; ++i) {
    for (ll j : vt) {
      if (i + j > n) {
        break;
      }
      dp[i + j] += dp[i];
      dp[i + j] %= mod;
    }
  }
  cout << dp[n] % mod << endl;
  return 0;
}