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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
#define Seg_Max_N (1 << 18)

using Value = vector<int>;
const vector<int> ini = vector<int>(0);
struct segtree {
  int N;
  vector<Value> dat;

  segtree() {}
  segtree(int n) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
  }
  segtree(int n, vector<int> &v) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
    int num = N;
    for (int i = 0; i < N; ++i) {
      if (i < n) {
        dat[i + N - 1] = vector<int>(1, v[i]);
      }
    }
    num /= 2;
    while (num) {
      for (int i = 0; i < num; ++i) {
        int k = i + num - 1;

        const Value al(dat[k * 2 + 1]);
        const Value ar(dat[k * 2 + 2]);
        dat[k] = merge(al, ar);
      }
      num /= 2;
    }
  }
  vector<int> merge(const Value &l, const Value &r) {
    auto it = l.begin();
    auto jt = r.begin();
    Value v;
    while (it != l.end() || jt != r.end()) {
      int inum, jnum;
      if (it == l.end())
        inum = 1e9;
      else
        inum = *it;
      if (jt == r.end())
        jnum = 1e9;
      else
        jnum = *jt;
      if (inum < jnum) {
        v.emplace_back(inum);
        it++;
      } else {
        v.emplace_back(jnum);
        jt++;
      }
    }
    return v;
  }

  // min [a, b)
  int query(const int al, const int ar, int a, int b) {
    return query(al, ar, a, b, 0, 0, N);
  }
  int query(const int al, const int ar, int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return 0;
    if (a <= l and r <= b) {

      auto it = lower_bound(dat[k].begin(), dat[k].end(), al);
      auto jt = upper_bound(dat[k].begin(), dat[k].end(), ar);
      return jt - it;
    }
    const int m = (l + r) / 2;
    const int aal(query(al, ar, a, b, k * 2 + 1, l, m));
    const int aar(query(al, ar, a, b, k * 2 + 2, m, r));
    return aal + aar;
  }
};
int main() {
  int N;
  cin >> N;
  vector<int> as(N);
  for (int i = 0; i < N; ++i) {
    cin >> as[i];
  }
  segtree seg(N, as);
  int Q;
  cin >> Q;
  while (Q--) {
    int l, r, t;
    cin >> l >> r >> t;
    l--;
    r--;
    int a = as[l], b = as[r];
    int amin = min(a, b) - t;
    int amax = max(a, b) + t;
    int vs(seg.query(amin, amax, l, r + 1));
    cout << r - l + 1 - vs << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
#define Seg_Max_N (1 << 18)

using Value = vector<int>;
const vector<int> ini = vector<int>(0);
struct segtree {
  int N;
  vector<Value> dat;

  segtree() {}
  segtree(int n) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
  }
  segtree(int n, vector<int> &v) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
    int num = N;
    for (int i = 0; i < N; ++i) {
      if (i < n) {
        dat[i + N - 1] = vector<int>(1, v[i]);
      }
    }
    num /= 2;
    while (num) {
      for (int i = 0; i < num; ++i) {
        int k = i + num - 1;

        const Value al(dat[k * 2 + 1]);
        const Value ar(dat[k * 2 + 2]);
        dat[k] = merge(al, ar);
      }
      num /= 2;
    }
  }
  vector<int> merge(const Value &l, const Value &r) {
    auto it = l.begin();
    auto jt = r.begin();
    Value v;
    while (it != l.end() || jt != r.end()) {
      int inum, jnum;
      if (it == l.end())
        inum = 1e9;
      else
        inum = *it;
      if (jt == r.end())
        jnum = 1e9;
      else
        jnum = *jt;
      if (inum < jnum) {
        v.emplace_back(inum);
        it++;
      } else {
        v.emplace_back(jnum);
        jt++;
      }
    }
    return v;
  }

  // min [a, b)
  Value query(int a, int b) { return query(a, b, 0, 0, N); }
  Value query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return ini;
    if (a <= l and r <= b)
      return dat[k];
    const int m = (l + r) / 2;
    const Value &al(query(a, b, k * 2 + 1, l, m));
    const Value &ar(query(a, b, k * 2 + 2, m, r));
    return merge(al, ar);
  }
};
int main() {
  int N;
  cin >> N;
  vector<int> as(N);
  for (int i = 0; i < N; ++i) {
    cin >> as[i];
  }
  segtree seg(N, as);
  int Q;
  cin >> Q;
  while (Q--) {
    int l, r, t;
    cin >> l >> r >> t;
    l--;
    r--;
    int a = as[l], b = as[r];
    int amin = min(a, b) - t;
    int amax = max(a, b) + t;
    auto vs(seg.query(l, r + 1));
    auto it = lower_bound(vs.begin(), vs.end(), amin);
    auto jt = upper_bound(vs.begin(), vs.end(), amax);
    int ans = jt - it;
    cout << r - l + 1 - ans << endl;
  }
  return 0;
}