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

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

#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define pre(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;
#define br "\\n"
#define ff first
#define ss second
#define MAXI 10000001
ll MAX = 1e10;
ll mod = 998244353;

vll tree(MAXI), lazy(MAXI), arr, bin10;
vb vis;
ll n, q, mod9;

ll bin_exp(ll a, ll b) {
  a = a % mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1 == 1)
      ans = (a * ans) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return ans;
}

ll mod_inv(ll n) {
  ll a = bin_exp(n, mod - 2);
  return a;
}

ll gp(ll l, ll r) {
  swap(l, r);
  r = n - 1 - r;
  l = n - 1 - l;
  ll ans1 = ((bin10[r + 1] - 1LL) * mod9) % mod;
  ll ans2 = ((bin10[l] - 1LL) * mod9) % mod;
  if (ans1 > ans2)
    return (ans1 - ans2);
  else
    return (ans1 - ans2 + mod) % mod;
}

void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    tree[v] = gp(tl, tr);
    return;
  }
  ll tm = (tl + tr) / 2;
  build(2 * v, tl, tm);
  build(2 * v + 1, tm + 1, tr);
  tree[v] = (tree[2 * v] + tree[2 * v + 1]) % mod;
}

ll sum(ll v, ll tl, ll tr) {
  if (lazy[v] > 0) {
    tree[v] = (gp(tl, tr) * lazy[v]) % mod;
    lazy[2 * v] = lazy[v];
    lazy[2 * v + 1] = lazy[v];
    lazy[v] = 0;
  }
  return tree[v];
}

void update(ll v, ll tl, ll tr, ll l, ll r, ll val) {
  if (lazy[v] > 0) {
    tree[v] = (gp(tl, tr) * lazy[v]) % mod;
    lazy[2 * v] = lazy[v];
    lazy[2 * v + 1] = lazy[v];
    lazy[v] = 0;
  }
  if (l > r)
    return;

  if (l == tl && r == tr) {
    tree[v] = (gp(tl, tr) * val) % mod;
    lazy[2 * v] = val;
    lazy[2 * v + 1] = val;
    return;
  }
  ll tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, min(tm, r), val);
  update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
  tree[v] = (tree[2 * v] + tree[2 * v + 1]) % mod;
}

void solve() {
  cin >> n >> q;
  mod9 = mod_inv(9);
  bin10.resize(n + 2);
  ll p = 1;
  rep(i, 0, n + 2) {
    bin10[i] = p;
    p = p * 10;
    if (p > mod)
      p %= mod;
  }
  build(1, 0, n - 1);
  ll temp = sum(1, 0, n - 1);
  rep(i, 0, q) {
    ll l, r, val;
    cin >> l >> r >> val;
    update(1, 0, n - 1, l - 1, r - 1, val);
    cout << sum(1, 0, n - 1) << br;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll t = 1;
  // cin >> t;
  rep(i, 0, t) { solve(); }
}

Error: TLE

Fixed submission:

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

#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define pre(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;
#define br "\\n"
#define ff first
#define ss second
#define MAXI 10000001
ll MAX = 1e10;
ll mod = 998244353;

vll tree(MAXI), lazy(MAXI), arr;
vb vis;
ll n, q;

ll mod9;

ll bin_exp(ll a, ll b) {
  a = a % mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1 == 1)
      ans = (a * ans) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return ans;
}

ll mod_inv(ll n) {
  ll a = bin_exp(n, mod - 2);
  return a;
}

ll gp(ll l, ll r) {
  swap(l, r);
  r = n - 1 - r;
  l = n - 1 - l;
  ll ans1 = ((bin_exp(10, r + 1) - 1LL) * mod_inv(9)) % mod;
  ll ans2 = ((bin_exp(10, l) - 1LL) * mod_inv(9)) % mod;
  return (ans1 - ans2 + mod) % mod;
}

void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    tree[v] = gp(tl, tr);
    return;
  }
  ll tm = (tl + tr) / 2;
  build(2 * v, tl, tm);
  build(2 * v + 1, tm + 1, tr);
  tree[v] = (tree[2 * v] + tree[2 * v + 1]) % mod;
}

ll sum(ll v, ll tl, ll tr) {
  if (lazy[v] > 0) {
    tree[v] = (gp(tl, tr) * lazy[v]) % mod;
    lazy[2 * v] = lazy[v];
    lazy[2 * v + 1] = lazy[v];
    lazy[v] = 0;
  }
  return tree[v];
}

void update(ll v, ll tl, ll tr, ll l, ll r, ll val) {
  if (lazy[v] > 0) {
    tree[v] = (gp(tl, tr) * lazy[v]) % mod;
    lazy[2 * v] = lazy[v];
    lazy[2 * v + 1] = lazy[v];
    lazy[v] = 0;
  }
  if (l > r)
    return;

  if (l == tl && r == tr) {
    tree[v] = (gp(tl, tr) * val) % mod;
    lazy[2 * v] = val;
    lazy[2 * v + 1] = val;
    return;
  }
  ll tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, min(tm, r), val);
  update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
  tree[v] = (tree[2 * v] + tree[2 * v + 1]) % mod;
}

void solve() {
  cin >> n >> q;
  build(1, 0, n - 1);
  ll temp = sum(1, 0, n - 1);
  rep(i, 0, q) {
    ll l, r, val;
    cin >> l >> r >> val;
    update(1, 0, n - 1, l - 1, r - 1, val);
    cout << sum(1, 0, n - 1) << br;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll t = 1;
  // cin >> t;
  rep(i, 0, t) { solve(); }
}