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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define LL long long
#define maxn 200005

using namespace std;

const LL mod = 998244353;
int n, v[maxn << 2], q, a[maxn];
LL mp[11][maxn], M[maxn << 2];

void Init() {
  for (int i = 1; i <= 9; i++) {
    mp[i][0] = 0;
    for (int j = 1; j <= n; j++)
      mp[i][j] = (mp[i][j - 1] * 10 + i) % mod;
  }
  mp[10][0] = 1;
  for (int i = 1; i <= n; i++)
    mp[10][i] = mp[10][i - 1] * 10 % mod;
}

inline void push_down(int n, int L, int R) {
  v[n << 1] = v[n];
  v[n << 1 | 1] = v[n];
  int mid = (L + R) >> 1;
  M[n << 1] = mp[v[n]][mid - L + 1];
  M[n << 1 | 1] = mp[v[n]][R - mid];
  v[n] = -1;
}
/*
void build(int n,int L,int R,int p)
{
    if (L==R)
    {
        M[p]=1;
        return;
    }
    int mid=(L+R)>>1;
    if (p<=mid) build(n<<1,L,mid,p);
    else build(n<<1|1,mid+1,R,p);
    M[n]=M[n<<1]*M[]
}*/

void Insert(int n, int L, int R, int l, int r, int k) {
  // if (~v[n] && v[n]==k) return ;
  if (~v[n] && L != R)
    push_down(n, L, R);
  if (l <= L && R <= r) {
    v[n] = k;
    M[n] = mp[k][R - L + 1];
    return;
  }
  int mid = (L + R) >> 1;
  if (l <= mid)
    Insert(n << 1, L, mid, l, r, k);
  if (r > mid)
    Insert(n << 1 | 1, mid + 1, R, l, r, k);
  M[n] = M[n << 1] * mp[10][R - mid] % mod + M[n << 1 | 1];
  if (M[n] >= mod)
    M[n] %= mod;
  return;
}

LL solve(int n, int L, int R) {
  if (~v[n])
    return M[n];

  LL ret = 0;
  int mid = (L + R) >> 1;
  ret = solve(n << 1, L, mid) * mp[10][R - mid] % mod;
  ret = (ret + solve(n << 1 | 1, mid + 1, R));
  if (ret >= mod)
    ret %= mod;
  return ret;
}

int main() {
  memset(v, -1, sizeof(v));
  scanf("%d%d", &n, &q);
  Init();
  Insert(1, 1, n, 1, n, 1);
  while (q--) {
    int l, r, d;
    scanf("%d%d%d", &l, &r, &d);
    Insert(1, 1, n, l, r, d);
    printf("%lld\\n", M[1]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define LL long long
#define maxn 200005

using namespace std;

const LL mod = 998244353;
int n, v[maxn << 2], q, a[maxn];
LL mp[11][maxn];

void Init() {
  for (int i = 1; i <= 9; i++) {
    mp[i][0] = 0;
    for (int j = 1; j <= n; j++)
      mp[i][j] = (mp[i][j - 1] * 10 + i) % mod;
  }
  mp[10][0] = 1;
  for (int i = 1; i <= n; i++)
    mp[10][i] = mp[10][i - 1] * 10 % mod;
}

inline void push_down(int n) {
  v[n << 1] = v[n];
  v[n << 1 | 1] = v[n];
  v[n] = -1;
}

void Insert(int n, int L, int R, int l, int r, int k) {
  if (~v[n] && v[n] == k)
    return;
  if (~v[n] && L != R)
    push_down(n);
  if (l <= L && R <= r) {
    v[n] = k;
    return;
  }
  int mid = (L + R) >> 1;
  if (l <= mid)
    Insert(n << 1, L, mid, l, r, k);
  if (r > mid)
    Insert(n << 1 | 1, mid + 1, R, l, r, k);
  if (v[n << 1] == v[n << 1 | 1])
    v[n] = v[n << 1];
  return;
}

LL solve(int n, int L, int R) {
  if (~v[n])
    return mp[v[n]][R - L + 1];
  LL ret = 0;
  int mid = (L + R) >> 1;
  ret = solve(n << 1, L, mid) * mp[10][R - mid] % mod;
  ret = (ret + solve(n << 1 | 1, mid + 1, R));
  if (ret >= mod)
    ret %= mod;
  return ret;
}

int main() {
  memset(v, -1, sizeof(v));
  scanf("%d%d", &n, &q);
  Init();
  Insert(1, 1, n, 1, n, 1);
  while (q--) {
    int l, r, d;
    scanf("%d%d%d", &l, &r, &d);
    Insert(1, 1, n, l, r, d);
    // LL ans=0;
    // if (l!=1) ans=solve(1,1,l-1)*mp[10][n-(l-1)]%mod;
    printf("%lld\\n", solve(1, 1, n));
  }
  return 0;
}