Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define L (nod << 1)
#define R (L | 1)
#define smax(X, Y) ((X) > (Y) ? (X) : (Y))
#define smin(X, Y) ((X) < (Y) ? (X) : (Y))
#define N 200005

int n, q, lzy[4 * N], d[N], sol[N];

struct lol {
  int t, s, x;
  bool operator<(const lol &aux) const { return (x < aux.x); }
} a[N];

int bs(int val) {
  int ans = 0;
  for (int i = 1 << 17; i; i >>= 1)
    if (ans + i <= q && d[ans + i] < val)
      ans += i;
  return ans + 1;
}

void upd(int nod, int st, int dr, int l, int r, int val) {
  if (st >= l && dr <= r) {
    lzy[nod] = val;
    return;
  }
  int mid = (st + dr) >> 1;
  if (l <= mid)
    upd(L, st, mid, l, r, val);
  if (r > mid)
    upd(R, mid + 1, dr, l, r, val);
}

void build(int nod, int st, int dr) {
  if (st == dr) {
    sol[st] = (!lzy[nod] ? -1 : lzy[nod]);
    return;
  }
  int mid = (st + dr) >> 1;
  if (lzy[nod]) {
    if (lzy[L])
      lzy[L] = smin(lzy[L], lzy[nod]);
    else
      lzy[L] = lzy[nod];
    if (lzy[R])
      lzy[R] = smin(lzy[R], lzy[nod]);
    else
      lzy[R] = lzy[nod];
  }
  build(L, st, mid);
  build(R, mid + 1, dr);
}

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0);
  // ifstream cin ("test.in");
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i].s >> a[i].t >> a[i].x;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= q; i++)
    cin >> d[i];
  for (int i = n; i; i--) {
    int st = a[i].s - a[i].x, dr = a[i].t - a[i].x;
    st = bs(st);
    dr = bs(dr) - 1;
    if (dr < st) {
      for (;;)
        ;
    }
    // cout << a[i].x << ' ' << a[i].s << ' ' << st << ' ' << dr << '\n';
    upd(1, 1, q, st, dr, a[i].x);
  }
  build(1, 1, q);
  for (int i = 1; i <= q; i++)
    cout << sol[i] << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define L (nod << 1)
#define R (L | 1)
#define smax(X, Y) ((X) > (Y) ? (X) : (Y))
#define smin(X, Y) ((X) < (Y) ? (X) : (Y))
#define N 200005

int n, q, lzy[4 * N], d[N], sol[N];

struct lol {
  int t, s, x;
  bool operator<(const lol &aux) const { return (x < aux.x); }
} a[N];

int bs(int val) {
  int ans = 0;
  for (int i = 1 << 17; i; i >>= 1)
    if (ans + i <= q && d[ans + i] < val)
      ans += i;
  return ans + 1;
}

void upd(int nod, int st, int dr, int l, int r, int val) {
  if (st >= l && dr <= r) {
    lzy[nod] = val;
    return;
  }
  int mid = (st + dr) >> 1;
  if (l <= mid)
    upd(L, st, mid, l, r, val);
  if (r > mid)
    upd(R, mid + 1, dr, l, r, val);
}

void build(int nod, int st, int dr) {
  if (st == dr) {
    sol[st] = (!lzy[nod] ? -1 : lzy[nod]);
    return;
  }
  int mid = (st + dr) >> 1;
  if (lzy[nod]) {
    if (lzy[L])
      lzy[L] = smin(lzy[L], lzy[nod]);
    else
      lzy[L] = lzy[nod];
    if (lzy[R])
      lzy[R] = smin(lzy[R], lzy[nod]);
    else
      lzy[R] = lzy[nod];
  }
  build(L, st, mid);
  build(R, mid + 1, dr);
}

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0);
  // ifstream cin ("test.in");
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i].s >> a[i].t >> a[i].x;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= q; i++)
    cin >> d[i];
  for (int i = n; i; i--) {
    int st = a[i].s - a[i].x, dr = a[i].t - a[i].x;
    st = bs(st);
    dr = bs(dr) - 1;
    if (dr < st)
      continue;
    // cout << a[i].x << ' ' << a[i].s << ' ' << st << ' ' << dr << '\n';
    upd(1, 1, q, st, dr, a[i].x);
  }
  build(1, 1, q);
  for (int i = 1; i <= q; i++)
    cout << sol[i] << '\n';
  return 0;
}