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

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define umap unordered_map
#define mii map<int, int>
#define pii pair<int, int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define pqmin priority_queue<int, vector<int>, greater<int>>
#define pqmax priority_queue<int>
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define ps(x, y) fixed << setprecision(y) << x
#define out(x) cout << x << endl;
#define print(a, n)                                                            \\
  rep(i, n) cout << a[i] << " ";                                               \\
  cout << endl;
#define clr(a, x) memset(a, x, sizeof a)

const int mod = 1e9 + 7;
const int mod2 = 998244353;
const int N = 2e5;
const int inf = 1e18;

//=====================================================

template <typename... T> void read(T &...args) { ((cin >> args), ...); }
template <typename... T> void write(T &&...args) {
  ((cout << args << " "), ...);
}
template <typename... T> void writeline(T &&...args) {
  ((cout << args << " "), ...);
  cout << endl;
}

int add(int a, int b, int mod) {
  int res = (a + b) % mod;
  return (res < 0) ? res + mod : res;
}
int mul(int a, int b, int mod) {
  int res = (a * 1LL * b) % mod;
  return (res < 0) ? res + mod : res;
}
string binary(int x) {
  string res = "";
  rep(i, 32) res += (1 << i & x) ? '1' : '0';
  return res;
}
int in(int x, int y, int z) {
  if (y > z)
    swap(y, z);
  return (x >= y && x <= z);
}
struct customCompare {
  bool operator()(pii x, pii y) { return x.first > y.second; }
};

//=====================================================

int tree[1048576], lazy[1048576];
int p[N + 5];
int mod9, n, q;

int ModExp(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}

void update(int i, int j, int l, int r, int k, int x) {
  if (i > j)
    return;

  if (lazy[k] != 0) {
    int res = (mod2 + p[j - i + 1] - 1) % mod2;
    res = (res * mod9) % mod2;
    res = (res * lazy[k]) % mod2;
    res = (res * p[n - j]) % mod2;
    tree[k] = res;

    if (i != j)
      lazy[2 * k] = lazy[2 * k + 1] = lazy[k];
    lazy[k] = 0;
  }

  if (i > r || j < l)
    return;

  if (i >= l && r >= j) {
    int res = (mod2 + p[j - i + 1] - 1) % mod2;
    res = (res * mod9) % mod2;
    res = (res * x) % mod2;
    res = (res * p[n - j]) % mod2;
    tree[k] = res;

    if (i != j)
      lazy[2 * k] = lazy[2 * k + 1] = x;

    return;
  }

  int mid = (i + j) / 2;
  update(i, mid, l, r, 2 * k, x);
  update(mid + 1, j, l, r, 2 * k + 1, x);
  tree[k] = (tree[2 * k] + tree[2 * k + 1]) % mod2;
}

void solve() {
  cin >> n >> q;

  mod9 = ModExp(9, mod2 - 2, mod2);
  p[0] = 1;
  fo(i, 1, n + 1) { p[i] = (p[i - 1] * 10) % mod2; }

  fo(i, 1, n + 1) { update(1, n, i, i, 1, 1); }

  while (q--) {
    int l, r, d;
    read(l, r, d);

    update(1, n, l, r, 1, d);
    cout << tree[1] << endl;
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define umap unordered_map
#define mii map<int, int>
#define pii pair<int, int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define pqmin priority_queue<int, vector<int>, greater<int>>
#define pqmax priority_queue<int>
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define ps(x, y) fixed << setprecision(y) << x
#define out(x) cout << x << endl;
#define print(a, n)                                                            \\
  rep(i, n) cout << a[i] << " ";                                               \\
  cout << endl;
#define clr(a, x) memset(a, x, sizeof a)

const int mod = 1e9 + 7;
const int mod2 = 998244353;
const int N = 2e5;
const int inf = 1e18;

//=====================================================

template <typename... T> void read(T &...args) { ((cin >> args), ...); }
template <typename... T> void write(T &&...args) {
  ((cout << args << " "), ...);
}
template <typename... T> void writeline(T &&...args) {
  ((cout << args << " "), ...);
  cout << endl;
}

int add(int a, int b) {
  int res = (a + b) % mod;
  return (res < 0) ? res + mod : res;
}
int mul(int a, int b) {
  int res = (a * 1LL * b) % mod;
  return (res < 0) ? res + mod : res;
}
string binary(int x) {
  string res = "";
  rep(i, 32) res += (1 << i & x) ? '1' : '0';
  return res;
}
int in(int x, int y, int z) {
  if (y > z)
    swap(y, z);
  return (x >= y && x <= z);
}
struct customCompare {
  bool operator()(pii x, pii y) { return x.first > y.second; }
};

//=====================================================

int dp[10][N + 5];
int tree[1048576];
int p[N + 5];

void build(int i, int j, int k) {
  if (i == j) {
    tree[k] = 1;
    return;
  }

  int mid = (i + j) / 2;
  build(i, mid, 2 * k);
  build(mid + 1, j, 2 * k + 1);

  tree[k] = (tree[2 * k] * p[j - mid]) % mod2;
  tree[k] = (tree[k] + tree[2 * k + 1]) % mod2;
}

void rangeUpdate(int i, int j, int l, int r, int k, int x) {
  if (i > r || j < l)
    return;
  if (i == j) {
    tree[k] = x;
    return;
  }

  int mid = (i + j) / 2;
  rangeUpdate(i, mid, l, r, 2 * k, x);
  rangeUpdate(mid + 1, j, l, r, 2 * k + 1, x);
  tree[k] = (tree[2 * k] * p[j - mid]) % mod2;
  tree[k] = (tree[k] + tree[2 * k + 1]) % mod2;
}

int query(int i, int j, int l, int r, int k) {
  if (i > r || j < l)
    return 0;
  if (i >= l && j <= r) {
    return (tree[k] * p[r - j]) % mod2;
  }

  int mid = (i + j) / 2;
  return (query(i, mid, l, r, 2 * k) + query(mid + 1, j, l, r, 2 * k + 1)) %
         mod2;
}

void solve() {
  int n, q;
  cin >> n >> q;

  p[0] = 1;
  fo(i, 1, n + 1) { p[i] = (p[i - 1] * 10) % mod2; }

  build(1, n, 1);

  while (q--) {
    int l, r, d;
    read(l, r, d);

    rangeUpdate(1, n, l, r, 1, d);
    cout << query(1, n, 1, n, 1) << endl;
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}