Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define ll long long
#define PB push_back
#define ld long double
#define ff first
#define ss second
#define st(v) (v).begin(), (v).end()
const int dx[4] = {1, 0, 0, -1};
const int dy[4] = {0, -1, 1, 0};
const int nmax = 2e5 + 5;
vector<int> seg_tree(4 * nmax, INT_MAX);
vector<int> lazy(4 * nmax, INT_MAX);
void update(int node, int start, int end, int l, int r, int val) {
  if (lazy[node] != INT_MAX) {
    seg_tree[node] = min(seg_tree[node], lazy[node]);
    if (start != end) {
      lazy[node * 2] = min(lazy[node], lazy[node * 2]);
      lazy[node * 2 + 1] = min(lazy[node], lazy[node * 2 + 1]);
    }
    lazy[node] = INT_MAX;
  }
  if (start > r || end < l || l > r) {
    return;
  }
  if (start >= l && end <= r) {
    seg_tree[node] = min(seg_tree[node], val);
    if (start != end) {
      lazy[node * 2] = min(val, lazy[node * 2]);
      lazy[node * 2 + 1] = min(val, lazy[node * 2 + 1]);
    }
    return;
  }
  int mid = (start + end) / 2;
  update(node * 2, start, mid, l, r, val);
  update(node * 2 + 1, mid + 1, end, l, r, val);
  seg_tree[node] = min(seg_tree[2 * node], seg_tree[2 * node + 1]);
}
int Find(int node, int start, int end, int pos) {
  if (lazy[node] != INT_MAX) {
    seg_tree[node] = min(seg_tree[node], lazy[node]);
    if (start != end) {
      lazy[node * 2] = min(lazy[node], lazy[node * 2]);
      lazy[node * 2 + 1] = min(lazy[node], lazy[node * 2 + 1]);
    }
    lazy[node] = INT_MAX;
  }
  if (start == end && start == pos) {
    return seg_tree[node];
  }
  int mid = (start + end) / 2;
  if (pos <= mid) {
    Find(node * 2, start, mid, pos);
  } else
    Find(node * 2 + 1, mid + 1, end, pos);
}
void solve() {
  int n, q;
  cin >> n >> q;
  vector<int> b;
  vector<pair<int, pair<int, int>>> range(n);
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    s = max(0, s - x);
    t = max(0, t - x - 1);
    range[i].ff = s;
    range[i].ss.ff = t;
    range[i].ss.ss = x;
    b.PB(s);
    b.PB(t);
  }
  vector<int> query(q);
  for (int i = 0; i < q; i++) {
    cin >> query[i];
    b.PB(query[i]);
  }
  sort(st(b));
  int k = 0;
  map<int, int> mp;
  for (int i = 0; i < b.size(); i++) {
    if (mp.find(b[i]) == mp.end()) {
      mp[b[i]] = k++;
    }
  }
  for (int i = 0; i < n; i++) {
    int l = mp[range[i].ff];
    int r = mp[range[i].ss.ff];
    int val = range[i].ss.ss;
    // cout << l << " " << r << " " << val << '\n' ;
    update(1, 0, nmax - 1, l, r, val);
  }
  for (int i = 0; i < q; i++) {
    int x = mp[query[i]];
    int ans = Find(1, 0, nmax - 1, x);
    if (ans != INT_MAX)
      cout << ans << "\n";
    else
      cout << -1 << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 0; i < q; i++) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define ll long long
#define PB push_back
#define ld long double
#define ff first
#define ss second
#define st(v) (v).begin(), (v).end()
const int dx[4] = {1, 0, 0, -1};
const int dy[4] = {0, -1, 1, 0};
const int nmax = 8e5 + 5;
vector<int> seg_tree(4 * nmax, INT_MAX);
vector<int> lazy(4 * nmax, INT_MAX);
void update(int node, int start, int end, int l, int r, int val) {
  if (lazy[node] != INT_MAX) {
    seg_tree[node] = min(seg_tree[node], lazy[node]);
    if (start != end) {
      lazy[node * 2] = min(lazy[node], lazy[node * 2]);
      lazy[node * 2 + 1] = min(lazy[node], lazy[node * 2 + 1]);
    }
    lazy[node] = INT_MAX;
  }
  if (start > r || end < l || l > r) {
    return;
  }
  if (start >= l && end <= r) {
    seg_tree[node] = min(seg_tree[node], val);
    if (start != end) {
      lazy[node * 2] = min(val, lazy[node * 2]);
      lazy[node * 2 + 1] = min(val, lazy[node * 2 + 1]);
    }
    return;
  }
  int mid = (start + end) / 2;
  update(node * 2, start, mid, l, r, val);
  update(node * 2 + 1, mid + 1, end, l, r, val);
  seg_tree[node] = min(seg_tree[2 * node], seg_tree[2 * node + 1]);
}
int Find(int node, int start, int end, int pos) {
  if (lazy[node] != INT_MAX) {
    seg_tree[node] = min(seg_tree[node], lazy[node]);
    if (start != end) {
      lazy[node * 2] = min(lazy[node], lazy[node * 2]);
      lazy[node * 2 + 1] = min(lazy[node], lazy[node * 2 + 1]);
    }
    lazy[node] = INT_MAX;
  }
  if (start == end && start == pos) {
    return seg_tree[node];
  }
  int mid = (start + end) / 2;
  if (pos <= mid) {
    Find(node * 2, start, mid, pos);
  } else
    Find(node * 2 + 1, mid + 1, end, pos);
}
void solve() {
  int n, q;
  cin >> n >> q;
  vector<int> b;
  vector<pair<int, pair<int, int>>> range(n);
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    s = max(0, s - x);
    t = max(0, t - x - 1);
    range[i].ff = s;
    range[i].ss.ff = t;
    range[i].ss.ss = x;
    b.PB(s);
    b.PB(t);
  }
  vector<int> query(q);
  for (int i = 0; i < q; i++) {
    cin >> query[i];
    b.PB(query[i]);
  }
  sort(st(b));
  int k = 0;
  map<int, int> mp;
  for (int i = 0; i < b.size(); i++) {
    if (mp.find(b[i]) == mp.end()) {
      mp[b[i]] = k++;
    }
  }
  for (int i = 0; i < n; i++) {
    int l = mp[range[i].ff];
    int r = mp[range[i].ss.ff];
    int val = range[i].ss.ss;
    // cout << l << " " << r << " " << val << '\n' ;
    update(1, 0, nmax - 1, l, r, val);
  }
  for (int i = 0; i < q; i++) {
    int x = mp[query[i]];
    int ans = Find(1, 0, nmax - 1, x);
    if (ans != INT_MAX)
      cout << ans << "\n";
    else
      cout << -1 << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 0; i < q; i++) {
    solve();
  }
}