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

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;
int n, q, p10[N], i9;

int add(int x, int y) {
  if (x + y >= mod) {
    return x + y - mod;
  }
  return x + y;
}

int sub(int x, int y) {
  if (x - y < 0) {
    return x - y + mod;
  }
  return x - y;
}

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

int exp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1) {
      ans = mul(ans, x);
    }
    x = mul(x, x);
    y >>= 1;
  }
  return ans;
}

int inv(int x) { return exp(x, mod - 2); }

struct ST {
  int st[4 * N];
  int lazy[4 * N];

  int same(int d, int len) {
    int fact = mul(sub(p10[len], 1), i9);
    return mul(d, fact);
  }

  int merge(int a, int b, int len) { return add(mul(a, p10[len]), b); }

  void push(int v, int tl, int tr) {
    if (!lazy[v])
      return;

    int tm = (tl + tr) / 2;

    st[2 * v] = same(lazy[v], tm - tl + 1);
    st[2 * v + 1] = same(lazy[v], tr - tm);

    lazy[2 * v] = lazy[v];
    lazy[2 * v + 1] = lazy[v];

    lazy[v] = 0;
  }

  void build(int v, int tl, int tr) {
    if (tl == tr) {
      st[v] = 1;
      return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    st[v] = merge(st[v * 2], st[v * 2 + 1], tr - tm);
  }

  void update(int v, int tl, int tr, int l, int r, int val) {
    if (l > r) {
      return;
    }
    if (tl == l && tr == r) {
      st[v] = same(val, r - l + 1);
      lazy[v] = val;
      return;
    }
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(tm, r), val);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
    st[v] = merge(st[v * 2], st[v * 2 + 1], tr - tm);
  }

  int query() { return st[1]; }
};

ST st;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  p10[0] = 1;
  for (int i = 1; i < N; i++) {
    p10[i] = mul(p10[i - 1], 10);
  }
  i9 = inv(9);

  cin >> n >> q;

  st.build(1, 1, n);

  int l, r, d;
  for (int qi = 0; qi < q; qi++) {
    cin >> l >> r >> d;
    st.update(1, 1, n, l, r, d);
    cout << st.query() << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;
int n, q;

int add(int x, int y) {
  if (x + y >= mod) {
    return x + y - mod;
  }
  return x + y;
}

int sub(int x, int y) {
  if (x - y < 0) {
    return x - y + mod;
  }
  return x - y;
}

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

int exp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1) {
      ans = mul(ans, x);
    }
    x = mul(x, x);
    y >>= 1;
  }
  return ans;
}

int inv(int x) { return exp(x, mod - 2); }

struct ST {
  int st[4 * N];
  int lazy[4 * N];

  int same(int d, int len) {
    int fact = mul(sub(exp(10, len), 1), inv(9));
    return mul(d, fact);
  }

  int merge(int a, int b, int len) { return add(mul(a, exp(10, len)), b); }

  void push(int v, int tl, int tr) {
    if (!lazy[v])
      return;

    int tm = (tl + tr) / 2;

    st[2 * v] = same(lazy[v], tm - tl + 1);
    st[2 * v + 1] = same(lazy[v], tr - tm);

    lazy[2 * v] = lazy[v];
    lazy[2 * v + 1] = lazy[v];

    lazy[v] = 0;
  }

  void build(int v, int tl, int tr) {
    if (tl == tr) {
      st[v] = 1;
      return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    st[v] = merge(st[v * 2], st[v * 2 + 1], tr - tm);
  }

  void update(int v, int tl, int tr, int l, int r, int val) {
    if (l > r) {
      return;
    }
    if (tl == l && tr == r) {
      st[v] = same(val, r - l + 1);
      lazy[v] = val;
      return;
    }
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(tm, r), val);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
    st[v] = merge(st[v * 2], st[v * 2 + 1], tr - tm);
  }

  int query() { return st[1]; }
};

ST st;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> q;

  st.build(1, 1, n);

  int l, r, d;
  for (int qi = 0; qi < q; qi++) {
    cin >> l >> r >> d;
    st.update(1, 1, n, l, r, d);
    cout << st.query() << '\\n';
  }

  return 0;
}