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

#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x, i##end = y; i <= i##end; ++i)
#define _rep(i, x, y) for (int i = x, i##end = y; i >= i##end; --i)
#define ll long long
typedef std::pair<int, int> pii;
inline bool up(int &x, const int y) { return x < y ? x = y, 1 : 0; }
inline bool down(int &x, const int y) { return x > y ? x = y, 1 : 0; }
const int mod = 998244353;
#define N 200005
int n, q;
ll pw[N], sum[N];
inline void reduce(ll &x) { x -= mod, x += x >> 31 & mod; }
struct node {
  int l, r;
  mutable int v;
  inline node(int L, int R = -1, int V = 0) : l(L), r(R), v(V) {}
  inline bool operator<(const node &x) const { return l < x.l; }
};
#define It std::set<node>::iterator
std::set<node> s;
inline It split(int p) {
  It it = s.lower_bound(node(p));
  if (it != s.end() && it->l == p)
    return it;
  --it;
  int l = it->l, r = it->r, v = it->v;
  s.erase(it);
  s.insert(node(l, p - 1, v));
  return s.insert(node(p, r, v)).first;
}
ll ret = 0;
inline void assign(int L, int R, int val) {
  It r = split(R + 1), l = split(L);
  for (It i = l; i != r; ++i) {
    int v = i->v, _l = i->l, _r = i->r;
    reduce(ret += mod - (sum[_r] - sum[_l - 1] + mod) * v % mod);
    //		std::cerr<<"C: "<<(i->l)<<' '<<(i->r)<<' '<<v<<'\\n';
  }
  s.erase(l, r);
  reduce(ret += (sum[R] - sum[L - 1] + mod) * val % mod);
  s.insert(node(L, R, val));
}
int main() {
  std::ios::sync_with_stdio(0);
  std::cin >> n >> q;
  pw[n] = 1;
  _rep(i, n - 1, 1) pw[i] = pw[i + 1] * 10 % mod;
  rep(i, 1, n) reduce(sum[i] = sum[i - 1] + pw[i]);
  ret = sum[n];
  s.insert(node(1, n, 1));
  while (q--) {
    int l, r, d;
    std::cin >> l >> r >> d;
    assign(l, r, d);
    std::cout << ret << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x, i##end = y; i <= i##end; ++i)
#define _rep(i, x, y) for (int i = x, i##end = y; i >= i##end; --i)
#define ll long long
typedef std::pair<int, int> pii;
inline bool up(int &x, const int y) { return x < y ? x = y, 1 : 0; }
inline bool down(int &x, const int y) { return x > y ? x = y, 1 : 0; }
const int mod = 998244353;
#define N 200005
int n, q;
ll pw[N], sum[N];
inline void reduce(ll &x) { x -= mod, x += x >> 31 & mod; }
struct node {
  int l, r;
  mutable int v;
  inline node(int L, int R = -1, int V = 0) : l(L), r(R), v(V) {}
  inline bool operator<(const node &x) const { return l < x.l; }
};
#define It std::set<node>::iterator
std::set<node> s;
inline It split(int p) {
  It it = s.lower_bound(node(p));
  if (it != s.end() && it->l == p)
    return it;
  --it;
  int l = it->l, r = it->r, v = it->v;
  s.erase(it);
  s.insert(node(l, p - 1, v));
  return s.insert(node(p, r, v)).first;
}
inline void assign(int L, int R, int val) {
  It r = split(R + 1), l = split(L);
  s.erase(l, r);
  s.insert(node(L, R, val));
}
inline ll qry(ll ret = 0) {
  for (auto i : s) {
    int l = i.l, r = i.r, v = i.v;
    reduce(ret += (sum[r] - sum[l - 1] + mod) * v % mod);
  }
  return ret;
}
int main() {
  std::ios::sync_with_stdio(0);
  std::cin >> n >> q;
  pw[n] = 1;
  _rep(i, n - 1, 1) pw[i] = pw[i + 1] * 10 % mod;
  rep(i, 1, n) reduce(sum[i] = sum[i - 1] + pw[i]);
  s.insert(node(1, n, 1));
  while (q--) {
    int l, r, d;
    std::cin >> l >> r >> d;
    assign(l, r, d);
    std::cout << qry() << '\\n';
  }
  return 0;
}