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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

#define INF 2147483647LL
// #define NUL      2147483647LL
#define NUL -1LL
typedef long long int llong;

llong n_array;
llong queries;
vector<llong> D, Lazy;

void printDL();

void lazyEv(llong k, llong l, llong r) {
  if (Lazy[k] == NUL) {
    return;
  }
  D[k] = Lazy[k];
  // if it is not leaf
  if (r - l > 1) {
    Lazy[2 * k + 1] = Lazy[k];
    Lazy[2 * k + 2] = Lazy[k];
  }
  Lazy[k] = NUL;
}

void init(llong reqsz) {
  n_array = 1;
  while (n_array < reqsz) {
    n_array *= 2;
  }
  D.resize(2 * n_array - 1, 0);
  Lazy.resize(2 * n_array - 1, NUL);

  for (llong i = 0; i < reqsz; i++) {
    D[i + n_array - 1] = INF; // initialize the leaves
  }
  for (llong i = n_array - 2; i >= 0; i--) {
    D[i] = min(D[i * 2 + 1], D[i * 2 + 2]); // first update segment tree
  }
}

void updateRange(llong a, llong b, llong x, llong k = 0, llong l = 0,
                 llong r = -1) {
  if (r < 0) {
    r = n_array;
  }

  lazyEv(k, l, r);
  if (b <= l || r <= a) { // case 1
    return;
  } else if (a <= l && r <= b) { // case 2
    Lazy[k] = x;
    lazyEv(k, l, r);
  } else { // case 3
    updateRange(a, b, x, 2 * k + 1, l, (l + r) / 2);
    updateRange(a, b, x, 2 * k + 2, (l + r) / 2, r);
    D[k] = min(D[2 * k + 1], D[2 * k + 2]);
  }
}

llong find(llong a, llong b, llong k = 0, llong l = 0, llong r = -1) {
  if (r < 0) {
    r = n_array;
  }
  lazyEv(k, l, r);
  if (b <= l || r <= a) { // case 1
    return INF;
  } else if (a <= l && r <= b) { // case 2
    return D[k];
  } else { // case 3
    llong vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    llong vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

void printDL() { // For debug
  llong i = 0;
  llong j = 0;
  for (const auto &e : D) {
    printf("D[%lld]=", i);
    i++;
    std::cout << e << std::endl;
  }
  for (const auto &e : Lazy) {
    printf("Lazy[%lld]=", j);
    j++;
    std::cout << e << std::endl;
  }
}

int main() {
  llong reqsz;
  scanf("%lld %lld", &reqsz, &queries);
  init(reqsz);
  for (llong i = 0; i < queries; i++) {
    llong com;
    scanf("%lld", &com);
    if (com == 0) {
      llong s, t, x;
      scanf("%lld %lld %lld", &s, &t, &x);
      updateRange(s, t + 1, x);
    } else {
      llong idx;
      scanf("%lld", &idx);
      llong result = find(idx, idx + 1);
      printf("%lld\\n", result);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

#define INF 2147483647
typedef unsigned int uint;

uint n_array;
uint queries;
vector<uint> D;

void init(uint n_req) {
  n_array = 1;
  while (n_array < n_req) {
    n_array *= 2;
  }
  D.resize(2 * n_array - 1);
  for (uint i = 0; i <= 2 * n_array - 1; i++) {
    D[i] = INF;
  }
}

void update(uint k, uint a) {
  k += n_array - 1; // convert array index to tree index
  D[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    D[k] = min(D[k * 2 + 1], D[k * 2 + 2]);
  }
}

void updateRange(const uint s, const uint t, const uint x) {
  for (uint i = s; i <= t; i++) {
    update(i, x);
  }
}

uint query(uint a, uint b, uint k, uint l, uint r) {
  if (r <= a || b <= l) { // case 1
    return INF;
  } else if (a <= l && r <= b) { // case 2
    return D[k];
  }
  // case 3
  uint vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  uint vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(vl, vr);
}

uint findMin(uint a, uint b) { return query(a, b + 1, 0, 0, n_array); }

void printD() {
  int i = 0;
  for (const auto &e : D) {
    printf("[%d]", i);
    i++;
    std::cout << e << std::endl;
  }
}

int main() {
  uint n_req;
  scanf("%d %d", &n_req, &queries);
  init(n_req);
  for (uint i = 0; i < queries; i++) {
    uint com;
    scanf("%d", &com);
    if (com == 0) {
      uint s, t, x;
      scanf("%d %d %d", &s, &t, &x);
      updateRange(s, t, x);
    } else {
      uint idx;
      scanf("%d", &idx);
      idx += n_array - 1; // convert array index to tree index
      printf("%d\\n", D[idx]);
    }
  }

  return 0;
}