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

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

#define int long long
#define ff first
#define ss second
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a, int l, int md) {
  a %= md;
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a % md;
    l /= 2;
    a = a * a % md;
  }
  return res;
}
int binpow(int a, int l) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a;
    l /= 2;
    a = a * a;
  }
  return res;
}
int invmod(int a, int md) { return powmod(a, md - 2, md); }
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) { return b | (1LL << i); } // set ith bit
int __unset(int b, int i) { return b & (~(1UL << i)); }
int __check(int b, int i) { return b & (1LL << i); } // returns 0 if ith bit is
                                                     // 0
int mulmod(int a, int b, int md) {
  return (((a % md) * (b % md)) % md + md) % md;
}
int addmod(int a, int b, int md) { return ((a % md + b % md) % md + md) % md; }
int submod(int a, int b, int md) {
  return (((a % md - b % md) % md) + md) % md;
}
int divmod(int a, int b, int md) {
  return mulmod(a, powmod(b, md - 2, md), md);
} // if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL;                 // very large number
priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int MOD = 998244353;
int po[200010], inv9;
class SegmentTree {
private:
  int n;
  vii st;
  vi lz;
  vi a; // In st ss contains value and ff contains the index of the value
  void merge(int node) {
    st[node].ss = st[2 * node].ss + st[2 * node + 1].ss;
    st[node].ss %= MOD;
  }
  void build(int node, int lo, int hi) {
    if (lo == hi) {
      // st[node]={lo,(a[lo]*po[n-lo-1])%MOD};
      st[node] = {lo, (a[lo] * po[n - lo - 1]) % MOD};
      return;
    }
    build(2 * node, lo, (lo + hi) / 2);
    build(2 * node + 1, (lo + hi) / 2 + 1, hi);
    merge(node);
  }
  void propagate(int node, int l, int r) {
    if (l != r) {
      lz[2 * node] = lz[node];
      lz[2 * node + 1] = lz[node];
    }
    int v = lz[node];
    // int t1 = v*powmod(10,n-r-1,MOD);
    int t1 = v * po[n - r - 1];
    t1 %= MOD;
    int t2 = (po[r - l + 1] - 1 + MOD) % MOD;
    int t3 = inv9;
    // cout<<po[1]<<endl;
    // cout<<l<<" "<<r<<" "<<t1<<" "<<t2<<" "<<t3<<endl;
    t1 = (t1 * t2) % MOD;
    t1 = (t1 * t3) % MOD;
    st[node].ss = t1;
    st[node].ss %= MOD;
    lz[node] = 0;
  }
  void update(int node, int l, int r, int lo, int hi, int val) {
    if (lz[node] != 0)
      propagate(node, l, r);
    if (hi < l || lo > r)
      return;
    if (hi >= r && lo <= l) {
      lz[node] = val;
      propagate(node, l, r);
      return;
    }
    update(2 * node, l, (l + r) / 2, lo, hi, val);
    update(2 * node + 1, (l + r) / 2 + 1, r, lo, hi, val);
    merge(node);
  }
  ii rmq(int node, int l, int r, int lo,
         int hi) { // ss contains value and ff contains the index of the value
    if (lz[node] != 0)
      propagate(node, l, r);
    if (hi < l || lo > r)
      return {-1, 0};
    if (hi >= r && lo <= l)
      return st[node];
    ii d1 = rmq(2 * node, l, (l + r) / 2, lo, hi);
    ii d2 = rmq(2 * node + 1, (l + r) / 2 + 1, r, lo, hi);
    return {-1, (d1.ss + d2.ss) % MOD};
  }

public:
  SegmentTree(vi &__a) {
    n = __a.size();
    a = __a;
    st.assign(4 * n, {0, 0});
    lz.assign(4 * n, 0);
    build(1, 0, n - 1);
  }
  void Update(int l, int r, int val) { return update(1, 0, n - 1, l, r, val); }
  ii RMQ(int l, int r) { return rmq(1, 0, n - 1, l, r); }
};

signed main(void) {
  IOS;
  int n, q;
  cin >> n >> q;
  po[0] = 1;
  inv9 = invmod(9, MOD);
  for (int i = 1; i < 200010; i++)
    po[i] = (po[i - 1] * 10) % MOD;
  vi a(n, 1);
  SegmentTree st(a);
  // cout<<st.RMQ(0,n-1).ss<<endl;
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--, r--;
    st.Update(l, r, d);
    cout << st.RMQ(0, n - 1).ss << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long
#define ff first
#define ss second
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a, int l, int md) {
  a %= md;
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a % md;
    l /= 2;
    a = a * a % md;
  }
  return res;
}
int binpow(int a, int l) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a;
    l /= 2;
    a = a * a;
  }
  return res;
}
int invmod(int a, int md) { return powmod(a, md - 2, md); }
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) { return b | (1LL << i); } // set ith bit
int __unset(int b, int i) { return b & (~(1UL << i)); }
int __check(int b, int i) { return b & (1LL << i); } // returns 0 if ith bit is
                                                     // 0
int mulmod(int a, int b, int md) {
  return (((a % md) * (b % md)) % md + md) % md;
}
int addmod(int a, int b, int md) { return ((a % md + b % md) % md + md) % md; }
int submod(int a, int b, int md) {
  return (((a % md - b % md) % md) + md) % md;
}
int divmod(int a, int b, int md) {
  return mulmod(a, powmod(b, md - 2, md), md);
} // if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL;                 // very large number
priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int MOD = 998244353;

class SegmentTree {
private:
  int n;
  vii st;
  vi lz;
  vi a; // In st ss contains value and ff contains the index of the value
  void merge(int node) {
    st[node].ss = st[2 * node].ss + st[2 * node + 1].ss;
    st[node].ss %= MOD;
  }
  void build(int node, int lo, int hi) {
    if (lo == hi) {
      st[node] = {lo, (a[lo] * powmod(10, n - lo - 1, MOD)) % MOD};
      return;
    }
    build(2 * node, lo, (lo + hi) / 2);
    build(2 * node + 1, (lo + hi) / 2 + 1, hi);
    merge(node);
  }
  void propagate(int node, int l, int r) {
    if (l != r) {
      lz[2 * node] = lz[node];
      lz[2 * node + 1] = lz[node];
    }
    int v = lz[node];
    int t1 = v * powmod(10, n - r - 1, MOD);
    t1 %= MOD;
    int t2 = (powmod(10, r - l + 1, MOD) - 1 + MOD) % MOD;
    int t3 = invmod(9, MOD);
    t1 = (t1 * t2) % MOD;
    t1 = (t1 * t3) % MOD;
    st[node].ss = t1;
    st[node].ss %= MOD;
    lz[node] = 0;
  }
  void update(int node, int l, int r, int lo, int hi, int val) {
    if (lz[node] != 0)
      propagate(node, l, r);
    if (hi < l || lo > r)
      return;
    if (hi >= r && lo <= l) {
      lz[node] = val;
      propagate(node, l, r);
      return;
    }
    update(2 * node, l, (l + r) / 2, lo, hi, val);
    update(2 * node + 1, (l + r) / 2 + 1, r, lo, hi, val);
    merge(node);
  }
  ii rmq(int node, int l, int r, int lo,
         int hi) { // ss contains value and ff contains the index of the value
    if (lz[node] != 0)
      propagate(node, l, r);
    if (hi < l || lo > r)
      return {-1, 0};
    if (hi >= r && lo <= l)
      return st[node];
    ii d1 = rmq(2 * node, l, (l + r) / 2, lo, hi);
    ii d2 = rmq(2 * node + 1, (l + r) / 2 + 1, r, lo, hi);
    return {-1, (d1.ss + d2.ss) % MOD};
  }

public:
  SegmentTree(vi &__a) {
    n = __a.size();
    a = __a;
    st.assign(4 * n, {0, 0});
    lz.assign(4 * n, 0);
    build(1, 0, n - 1);
  }
  void Update(int l, int r, int val) { return update(1, 0, n - 1, l, r, val); }
  ii RMQ(int l, int r) { return rmq(1, 0, n - 1, l, r); }
};

signed main(void) {
  IOS;
  int n, q;
  cin >> n >> q;
  vi a(n, 1);
  SegmentTree st(a);
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--, r--;
    st.Update(l, r, d);
    cout << st.RMQ(0, n - 1).ss << endl;
  }
  return 0;
}