Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define LL long long

int n, q;
int S[200005], T[200005], X[200005];
vector<int> id;
vector<int> comp;
int st[200005 * 4], lazy[200005 * 4];
set<int> Set;
int Time[200005];

bool cmp(int a, int b) { return S[a] - X[a] < S[b] - X[b]; }

void build(int p, int s, int e) {
  st[p] = 1e9 + 7;
  lazy[p] = 0;
  if (s == e) {
    return;
  }

  int m = (s + e) >> 1;
  build(p * 2 + 1, s, m);
  build(p * 2 + 2, m + 1, e);
}

void pd(int p, int s, int e) {
  if (s < e && lazy[p] > 0) {
    int v = lazy[p];
    lazy[p] = 0;

    if (lazy[p * 2 + 1] == 0)
      lazy[p * 2 + 1] = v;
    else
      lazy[p * 2 + 1] = min(lazy[p * 2 + 1], v);

    if (lazy[p * 2 + 2] == 0)
      lazy[p * 2 + 2] = v;
    else
      lazy[p * 2 + 2] = min(lazy[p * 2 + 2], v);

    st[p * 2 + 1] = min(st[p * 2 + 1], v);
    st[p * 2 + 2] = min(st[p * 2 + 2], v);
  }
}

void upd(int p, int s, int e, int l, int r, int x) {
  if (s < e && lazy[p] > 0)
    pd(p, s, e);
  if (s == l && e == r) {
    st[p] = min(st[p], x);
    if (lazy[p] == 0)
      lazy[p] = x;
    else
      lazy[p] = min(lazy[p], x);
    return;
  }

  int m = (s + e) / 2;
  if (l <= m)
    upd(p * 2 + 1, s, m, l, min(m, r), x);
  if (m < r)
    upd(p * 2 + 2, m + 1, e, max(m + 1, l), r, x);
}

int query(int p, int s, int e, int x) {
  if (s < e && lazy[p] > 0)
    pd(p, s, e);
  if (s == e) {
    return st[p];
  }
  int m = (s + e) / 2;
  if (x <= m)
    return query(p * 2 + 1, s, m, x);
  else
    return query(p * 2 + 2, m + 1, e, x);
}

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &S[i], &T[i], &X[i]);
    T[i]--;
    id.push_back(i);
    Set.insert(S[i] - X[i]);
    Set.insert(T[i] - X[i]);
  }

  for (int i = 0; i < q; i++) {
    scanf("%d", &Time[i]);
    Set.insert(Time[i]);
  }

  for (auto it : Set) {
    comp.push_back(it);
  }

  sort(id.begin(), id.end(), cmp);
  build(0, 0, comp.size() - 1);
  for (int i = n - 1; i >= 0; i--) {
    int idx = id[i];
    int L =
        lower_bound(comp.begin(), comp.end(), S[idx] - X[idx]) - comp.begin();
    int R =
        lower_bound(comp.begin(), comp.end(), T[idx] - X[idx]) - comp.begin();
    upd(0, 0, comp.size() - 1, L, R, X[idx]);
  }

  for (int i = 0; i < q; i++) {
    int pos = lower_bound(comp.begin(), comp.end(), Time[i]) - comp.begin();
    int ans = query(0, 0, comp.size() - 1, pos);
    if (ans > 1e9)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define LL long long

int n, q;
int S[200005], T[200005], X[200005];
vector<int> id;
vector<int> comp;
int st[1000005 * 4], lazy[1000005 * 4];
set<int> Set;
int Time[200005];

bool cmp(int a, int b) { return S[a] - X[a] < S[b] - X[b]; }

void build(int p, int s, int e) {
  st[p] = 1e9 + 7;
  lazy[p] = 0;
  if (s == e) {
    return;
  }

  int m = (s + e) >> 1;
  build(p * 2 + 1, s, m);
  build(p * 2 + 2, m + 1, e);
}

void pd(int p, int s, int e) {
  if (s < e && lazy[p] > 0) {
    int v = lazy[p];
    lazy[p] = 0;

    if (lazy[p * 2 + 1] == 0)
      lazy[p * 2 + 1] = v;
    else
      lazy[p * 2 + 1] = min(lazy[p * 2 + 1], v);

    if (lazy[p * 2 + 2] == 0)
      lazy[p * 2 + 2] = v;
    else
      lazy[p * 2 + 2] = min(lazy[p * 2 + 2], v);

    st[p * 2 + 1] = min(st[p * 2 + 1], v);
    st[p * 2 + 2] = min(st[p * 2 + 2], v);
  }
}

void upd(int p, int s, int e, int l, int r, int x) {
  if (s < e && lazy[p] > 0)
    pd(p, s, e);
  if (s == l && e == r) {
    st[p] = min(st[p], x);
    if (lazy[p] == 0)
      lazy[p] = x;
    else
      lazy[p] = min(lazy[p], x);
    return;
  }

  int m = (s + e) / 2;
  if (l <= m)
    upd(p * 2 + 1, s, m, l, min(m, r), x);
  if (m < r)
    upd(p * 2 + 2, m + 1, e, max(m + 1, l), r, x);
}

int query(int p, int s, int e, int x) {
  if (s < e && lazy[p] > 0)
    pd(p, s, e);
  if (s == e) {
    return st[p];
  }
  int m = (s + e) / 2;
  if (x <= m)
    return query(p * 2 + 1, s, m, x);
  else
    return query(p * 2 + 2, m + 1, e, x);
}

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &S[i], &T[i], &X[i]);
    T[i]--;
    id.push_back(i);
    Set.insert(S[i] - X[i]);
    Set.insert(T[i] - X[i]);
  }

  for (int i = 0; i < q; i++) {
    scanf("%d", &Time[i]);
    Set.insert(Time[i]);
  }

  for (auto it : Set) {
    comp.push_back(it);
  }

  sort(id.begin(), id.end(), cmp);
  build(0, 0, comp.size() - 1);
  for (int i = n - 1; i >= 0; i--) {
    int idx = id[i];
    int L =
        lower_bound(comp.begin(), comp.end(), S[idx] - X[idx]) - comp.begin();
    int R =
        lower_bound(comp.begin(), comp.end(), T[idx] - X[idx]) - comp.begin();
    upd(0, 0, comp.size() - 1, L, R, X[idx]);
  }

  for (int i = 0; i < q; i++) {
    int pos = lower_bound(comp.begin(), comp.end(), Time[i]) - comp.begin();
    int ans = query(0, 0, comp.size() - 1, pos);
    if (ans > 1e9)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}