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

/* winners never quit and quitters never win.
      #swap                                 */

#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;
#pragma GCC optimize("-O3")
using ll = long long;
using ld = long double;
const ll mod = 998244353;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

template <typename T> void make_unique(vector<T> &vec) {
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
}

#define endl '\\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define pls pair<ll, string>
#define psl pair<string, ll>
#define plc pair<ll, char>
#define pcl pair<char, ll>
#define pss pair<string, string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";

#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 << endl;
  err(++it, args...);
}

#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T                                                                      \\
  ll t = 0;                                                                    \\
  cin >> t;                                                                    \\
  for (ll test = 0; test < t; test++)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll nxt() {
  ll TemporaryVariable;
  cin >> TemporaryVariable;
  return TemporaryVariable;
}

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

ll fast_exp(ll base, ll exp1) {
  base %= mod;
  ll res = 1;
  while (exp1 > 0) {
    if (exp1 & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp1 /= 2;
  }
  return res % mod;
}

ll add(ll x, ll y) { return (x + y + mod) % mod; }

ll mul(ll x, ll y) { return (x * y) % mod; }

ll seg[800005], lazy[800005];
vc<ll> v;
ll val, n, q, value;
ll pp[200005];

void update(ll qlow, ll qhigh, ll low, ll high, ll pos) {
  if (low > high)
    return;
  ll mid = (low + high) / 2;
  if (lazy[pos] != 0) {
    ll temp1 = pp[high - low + 1];
    temp1 = add(temp1, -1);
    temp1 = mul(temp1, value);
    temp1 = mul(temp1, pp[n - 1 - high]);
    temp1 = mul(temp1, lazy[pos]);

    seg[pos] = temp1;
    if (low != high) {
      lazy[2 * pos + 1] = lazy[pos];
      lazy[2 * pos + 2] = lazy[pos];
    }
    lazy[pos] = 0;
  }
  if (qlow > high or qhigh < low)
    return;

  if (qlow <= low and qhigh >= high) {

    ll temp1 = pp[high - low + 1];
    temp1 = add(temp1, -1);
    temp1 = mul(temp1, value);
    temp1 = mul(temp1, pp[n - 1 - high]);
    temp1 = mul(temp1, val);

    seg[pos] = temp1;
    if (low != high) {
      lazy[2 * pos + 1] = val;
      lazy[2 * pos + 2] = val;
    }
    return;
  }
  update(qlow, qhigh, low, mid, 2 * pos + 1);
  update(qlow, qhigh, mid + 1, high, 2 * pos + 2);
  seg[pos] = add(seg[2 * pos + 1], seg[2 * pos + 2]);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // isprime();
  // coeff();
  /*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
  /*rlimit rlim;
  if (getrlimit(RLIMIT_STACK, &rlim)) return 1;
  rlim.rlim_cur = rlim.rlim_max;
  if (setrlimit(RLIMIT_STACK, &rlim)) return 2;*/
  pp[0] = 1;
  rep(a, 1, 200005) pp[a] = mul(pp[a - 1], 10);
  value = fast_exp(9, mod - 2);
  n = nxt(), q = nxt();
  val = 1;
  rep(a, 0, n) { update(a, a, 0, n - 1, 0); }
  while (q--) {
    ll l = nxt() - 1, r = nxt() - 1;
    val = nxt();
    update(l, r, 0, n - 1, 0);
    cout << seg[0] << endl;
  }
  printclock;
  return 0;
}

Error: TLE

Fixed submission:

/* winners never quit and quitters never win.
      #swap                                 */

#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;
#pragma GCC optimize("-O3")
using ll = long long;
using ld = long double;
const ll mod = 998244353;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

template <typename T> void make_unique(vector<T> &vec) {
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
}

#define endl '\\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define pls pair<ll, string>
#define psl pair<string, ll>
#define plc pair<ll, char>
#define pcl pair<char, ll>
#define pss pair<string, string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";

#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 << endl;
  err(++it, args...);
}

#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T                                                                      \\
  ll t = 0;                                                                    \\
  cin >> t;                                                                    \\
  for (ll test = 0; test < t; test++)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll nxt() {
  ll TemporaryVariable;
  cin >> TemporaryVariable;
  return TemporaryVariable;
}

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

ll fast_exp(ll base, ll exp1) {
  base %= mod;
  ll res = 1;
  while (exp1 > 0) {
    if (exp1 & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp1 /= 2;
  }
  return res % mod;
}

ll add(ll x, ll y) { return (x + y + mod) % mod; }

ll mul(ll x, ll y) { return (x * y) % mod; }

ll seg[800005], lazy[800005];
vc<ll> v;
ll val, n, q;

void update(ll qlow, ll qhigh, ll low, ll high, ll pos) {
  if (low > high)
    return;
  ll mid = (low + high) / 2;
  if (lazy[pos] != 0) {
    ll temp1 = fast_exp(10, mid - low + 1);
    temp1 = add(temp1, -1);
    temp1 = mul(temp1, fast_exp(9, mod - 2));
    temp1 = mul(temp1, fast_exp(10, n - 1 - mid));
    temp1 = mul(temp1, lazy[pos]);

    ll temp2 = fast_exp(10, high - mid);
    temp2 = add(temp2, -1);
    temp2 = mul(temp2, fast_exp(9, mod - 2));
    temp2 = mul(temp2, fast_exp(10, n - 1 - high));
    temp2 = mul(temp2, lazy[pos]);

    seg[pos] = add(temp1, temp2);
    if (low != high) {
      lazy[2 * pos + 1] = lazy[pos];
      lazy[2 * pos + 2] = lazy[pos];
    }
    lazy[pos] = 0;
  }
  if (qlow > high or qhigh < low)
    return;

  if (qlow <= low and qhigh >= high) {

    ll temp1 = fast_exp(10, mid - low + 1);
    temp1 = add(temp1, -1);
    temp1 = mul(temp1, fast_exp(9, mod - 2));
    temp1 = mul(temp1, fast_exp(10, n - 1 - mid));
    temp1 = mul(temp1, val);

    ll temp2 = fast_exp(10, high - mid);
    temp2 = add(temp2, -1);
    temp2 = mul(temp2, fast_exp(9, mod - 2));
    temp2 = mul(temp2, fast_exp(10, n - 1 - high));
    temp2 = mul(temp2, val);
    seg[pos] = add(temp1, temp2);
    if (low != high) {
      lazy[2 * pos + 1] = val;
      lazy[2 * pos + 2] = val;
    }
    return;
  }
  update(qlow, qhigh, low, mid, 2 * pos + 1);
  update(qlow, qhigh, mid + 1, high, 2 * pos + 2);
  seg[pos] = add(seg[2 * pos + 1], seg[2 * pos + 2]);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // isprime();
  // coeff();
  /*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
  /*rlimit rlim;
  if (getrlimit(RLIMIT_STACK, &rlim)) return 1;
  rlim.rlim_cur = rlim.rlim_max;
  if (setrlimit(RLIMIT_STACK, &rlim)) return 2;*/
  n = nxt(), q = nxt();
  val = 1;
  rep(a, 0, n) { update(a, a, 0, n - 1, 0); }
  while (q--) {
    ll l = nxt() - 1, r = nxt() - 1;
    val = nxt();
    update(l, r, 0, n - 1, 0);
    cout << seg[0] << endl;
  }
  printclock;
  return 0;
}