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

#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#define int long long int
#define mod 998244353
int power[200001];
int g;
void function2() {
  power[0] = 1;
  int j;
  for (j = 1; j <= 200000; j++) {
    power[j] = (10 * power[j - 1]) % mod;
  }
}
int fast_power(int a, int b) {
  if (b == 0)
    return 1;
  else {
    int r = fast_power(a, b / 2);
    if (b % 2 == 0) {
      return (r * r) % mod;
    } else {
      int x = (r * r) % mod;
      return (a * x) % mod;
    }
  }
}
int sum(int left, int right) {
  int val1 = power[left];
  int val2 = power[right - left + 1] - 1;
  val1 *= val2;
  val1 %= mod;
  val1 *= g;
  val1 %= mod;
  return val1;
}
void build_tree(int tree[], int start, int end, int n, int index) {
  if (start == end) {
    int here = power[n - 1 - start];
    tree[index] = here;
    return;
  } else {
    int mid = (start + end) >> 1;
    build_tree(tree, start, mid, n, 2 * index);
    build_tree(tree, mid + 1, end, n, 2 * index + 1);
    tree[index] = tree[2 * index] + tree[2 * index + 1];
    tree[index] %= mod;
  }
}
void update(int tree[], int lazy[], int start, int end, int left, int right,
            int d, int n, int index) {
  if (lazy[index] != 0) {
    tree[index] = lazy[index] * sum(n - 1 - end, n - 1 - start);
    tree[index] %= mod;
    if (start != end) {
      lazy[2 * index] = lazy[index];
      lazy[2 * index + 1] = lazy[index];
    }
    lazy[index] = 0;
  }
  if (left > end || start > right)
    return;
  if (start >= left && end <= right) {
    tree[index] = d * sum(n - 1 - end, n - 1 - start);
    tree[index] %= mod;
    if (start != end) {
      lazy[2 * index] = d;
      lazy[2 * index + 1] = d;
    }
    return;
  } else {
    int mid = (start + end) >> 1;
    update(tree, lazy, start, mid, left, right, d, n, 2 * index);
    update(tree, lazy, mid + 1, end, left, right, d, n, 2 * index + 1);
    tree[index] = tree[2 * index] + tree[2 * index + 1];
    tree[index] %= mod;
  }
}
int sum_query(int tree[], int lazy[], int start, int end, int left, int right,
              int n, int index) {
  if (lazy[index] != 0) {
    tree[index] = lazy[index] * sum(n - 1 - end, n - 1 - start);
    tree[index] %= mod;
    if (start != end) {
      lazy[2 * index] = lazy[index];
      lazy[2 * index + 1] = lazy[index];
    }
    lazy[index] = 0;
  }
  if (left > end || start > right)
    return 0;
  if (start >= left && end <= right)
    return tree[index];
  else {
    int mid = (start + end) >> 1;
    int x = sum_query(tree, lazy, start, mid, left, right, n, 2 * index);
    int y = sum_query(tree, lazy, mid + 1, end, left, right, n, 2 * index + 1);
    return (x + y) % mod;
  }
}
int32_t main() {
  function2();
  g = fast_power(9, mod - 2);
  int n, q;
  scanf("%lld%lld", &n, &q);
  int tree[4 * n + 1] = {0}, lazy[4 * n + 1] = {0};
  build_tree(tree, 0, n - 1, n, 1);
  while (q--) {
    int l, r, d;
    scanf("%lld%lld%lld", &l, &r, &d);
    l--;
    r--;
    update(tree, lazy, 0, n - 1, l, r, d, n, 1);
    int answer = sum_query(tree, lazy, 0, n - 1, 0, n - 1, n, 1);
    printf("%lld\\n", answer);
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#define int long long int
#define mod 998244353
const int N = 200000 + 100;
int tree[4 * N + 1] = {0}, lazy[4 * N + 1] = {0};
int power(int a, int b) {
  if (b == 0)
    return 1;
  else {
    int r = power(a, b / 2);
    if (b % 2 == 0) {
      return (r * r) % mod;
    } else {
      int x = (r * r) % mod;
      return (a * x) % mod;
    }
  }
}
int sum(int left, int right) {
  int val1 = power(10, left);
  int val2 = power(10, right - left + 1) - 1;
  val1 *= val2;
  val1 %= mod;
  val1 *= power(9, mod - 2);
  val1 %= mod;
  return val1;
}
void build_tree(int tree[], int start, int end, int n, int index) {
  if (start == end) {
    int here = power(10, n - 1 - start);
    tree[index] = here;
    return;
  } else {
    int mid = (start + end) / 2;
    build_tree(tree, start, mid, n, 2 * index);
    build_tree(tree, mid + 1, end, n, 2 * index + 1);
    tree[index] = tree[2 * index] + tree[2 * index + 1];
    tree[index] %= mod;
  }
}
void update(int tree[], int lazy[], int start, int end, int left, int right,
            int d, int n, int index) {
  if (lazy[index] != 0) {
    tree[index] = lazy[index] * sum(n - 1 - end, n - 1 - start);
    tree[index] %= mod;
    if (start != end) {
      lazy[2 * index] = lazy[index];
      lazy[2 * index + 1] = lazy[index];
    }
    lazy[index] = 0;
  }
  if (left > end || start > right)
    return;
  if (start >= left && end <= right) {
    tree[index] = d * sum(n - 1 - end, n - 1 - start);
    tree[index] %= mod;
    if (start != end) {
      lazy[2 * index] = d;
      lazy[2 * index + 1] = d;
    }
    return;
  } else {
    int mid = (start + end) / 2;
    update(tree, lazy, start, mid, left, right, d, n, 2 * index);
    update(tree, lazy, mid + 1, end, left, right, d, n, 2 * index + 1);
    tree[index] = tree[2 * index] + tree[2 * index + 1];
    tree[index] %= mod;
  }
}
int sum_query(int tree[], int lazy[], int start, int end, int left, int right,
              int n, int index) {
  if (lazy[index] != 0) {
    tree[index] = lazy[index] * sum(n - 1 - end, n - 1 - start);
    tree[index] %= mod;
    if (start != end) {
      lazy[2 * index] = lazy[index];
      lazy[2 * index + 1] = lazy[index];
    }
    lazy[index] = 0;
  }
  if (left > end || start > right)
    return 0;
  if (start >= left && end <= right)
    return tree[index];
  else {
    int mid = (start + end) / 2;
    int x = sum_query(tree, lazy, start, mid, left, right, n, 2 * index);
    int y = sum_query(tree, lazy, mid + 1, end, left, right, n, 2 * index + 1);
    return (x + y) % mod;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  build_tree(tree, 0, n - 1, n, 1);
  int answer = sum_query(tree, lazy, 0, n - 1, 0, n - 1, n, 1);
  // cout<<answer<<endl;
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    update(tree, lazy, 0, n - 1, l, r, d, n, 1);
    int answer = sum_query(tree, lazy, 0, n - 1, 0, n - 1, n, 1);
    answer %= mod;
    cout << answer << endl;
  }
}