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

#include <bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 2e5 + 1;
ll t[4 * N], powr[N];
ll s[4 * N];
ll ans[4 * N];
ll mod = 998244353;
void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    s[v] = powr[tl];
    return;
  }
  ll tm = (tl + tr) / 2;
  build(2 * v, tl, tm);
  build(2 * v + 1, tm + 1, tr);
  s[v] = (s[2 * v] + s[2 * v + 1]);
  if (s[v] > mod) {
    s[v] %= mod;
  }
}

void update(ll v, ll tl, ll tr, ll l, ll r, ll val) {
  if (l > r) {
    return;
  }
  if (tl == l && tr == r) {
    t[v] = val;
    ans[v] = val * s[v];
    if (ans[v] > mod)
      ans[v] %= mod;
    return;
  }
  if (t[v] != -1) {
    t[2 * v + 1] = t[v];
    t[2 * v] = t[v];
    ans[2 * v + 1] = s[2 * v + 1] * t[v];
    ans[2 * v] = s[2 * v] * t[v];
    if (ans[2 * v] > mod)
      ans[2 * v] %= mod;
    if (ans[2 * v + 1] > mod)
      ans[2 * v + 1] %= mod;
    t[v] = -1;
  }
  ll tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, min(r, tm), val);
  update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
  ans[v] = ans[2 * v] + ans[2 * v + 1];
  if (ans[v] > mod)
    ans[v] %= mod;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast;
  ll T = 1, i, j;

  // cin >> T;
  while (T--) {
    ll n, k;
    cin >> n >> k;
    for (i = 0; i < 4 * N; i++) {
      t[i] = -1;
    }

    ll p = 1;
    for (i = n; i >= 1; i--) {
      powr[i] = p;
      p *= 10;
      if (p > mod) {
        p %= mod;
      }
    }
    build(1, 1, n);
    update(1, 1, n, 1, n, 1);

    //..cout << ans[1] << endl;
    while (k--) {
      ll l, r, d;
      cin >> l >> r >> d;
      update(1, 1, n, l, r, d);
      cout << ans[1] << "\\n";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 2e5 + 5;
ll t[4 * N], powr[N];
ll s[4 * N];
ll ans[4 * N];
ll mod = 998244353;
void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    s[v] = powr[tl];
    return;
  }
  ll tm = (tl + tr) / 2;
  build(2 * v, tl, tm);
  build(2 * v + 1, tm + 1, tr);
  s[v] = (s[2 * v] + s[2 * v + 1]) % mod;
}

void update(ll v, ll tl, ll tr, ll l, ll r, ll val) {
  if (l > r) {
    return;
  }
  if (tl == l && tr == r) {
    t[v] = val;
    ans[v] = val * s[v];
    ans[v] %= mod;
    return;
  }
  if (t[v] != -1) {
    t[2 * v + 1] = t[v];
    t[2 * v] = t[v];
    ans[2 * v + 1] = s[2 * v + 1] * t[v];
    ans[2 * v] = s[2 * v] * t[v];
    t[v] = -1;
  }
  ll tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, min(r, tm), val);
  update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
  ans[v] = ans[2 * v] + ans[2 * v + 1];
  ans[v] %= mod;
}
ll query(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r) {
    return 0;
  }
  if (tl == tr) {
    return ans[v];
  }
  ll tm = (tl + tr) / 2;
  if (t[v] != -1) {
    return ans[v];
  }
  ll lef = query(2 * v, tl, tm, l, min(r, tm));

  ll rig = query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r);

  return (lef + rig) % mod;
}
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast;
  ll T = 1, i, j;

  // cin >> T;
  while (T--) {
    ll n, k;
    cin >> n >> k;
    memset(t, -1, sizeof(t));

    ll p = 1;
    for (i = n; i >= 1; i--) {
      powr[i] = p;
      p *= 10;
      p %= mod;
    }
    build(1, 1, n);
    update(1, 1, n, 1, n, 1);

    //..cout << ans[1] << endl;
    while (k--) {
      ll l, r, d;
      cin >> l >> r >> d;
      update(1, 1, n, l, r, d);
      cout << query(1, 1, n, 1, n) << endl;
    }
  }
  return 0;
}