Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(v) v.begin(), v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define vecunique(vec)                                                         \
  sort(vec.begin(), vec.end());                                                \
  decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());        \
  vec.erase(result, vec.end())
using namespace std;

bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return x > 1;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x) {
  if (x == 0)
    return 1;
  return kai(x - 1) * x % mod;
}
int mod_pow(int x, int y, int mod_) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % mod_;
    }
    x = x * x % mod_;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x) * mod_pow(kai(x - y), mod - 2, mod) % mod *
         mod_pow(kai(y), mod - 2, mod) % mod;
}
/*--------Library Zone!--------*/

P dat[1000000];
int size_ = 1;
void init(int x) {
  while (size_ < x)
    size_ *= 2;
  rep(i, 2 * size_ - 1) dat[i] = {inf, inf};
}
void eval(int k, int l, int r) {
  if (dat[k].second != inf) {
    dat[k].first = dat[k].second;
    if (r - l > 1) {
      dat[k * 2 + 1].second = dat[k].second;
      dat[k * 2 + 2].second = dat[k].second;
    }
    dat[k].second = inf;
  }
}
void update(int a, int b, int k, int x, int l, int r) {
  eval(k, l, r);
  if (r <= a || b <= l)
    return;
  if (a <= l && r <= b) {
    dat[k].second = x;
    eval(k, l, r);
  } else {
    update(a, b, k * 2 + 1, x, l, (l + r) / 2);
    update(a, b, k * 2 + 2, x, (l + r) / 2, r);
    dat[k].first = min(dat[k * 2 + 1].first, dat[k * 2 + 2].first);
  }
}
int query(int a, int b, int k, int l, int r) {
  eval(k, l, r);
  if (r <= a || b <= l)
    return inf;
  if (a <= l && r <= b) {
    return dat[k].first;
  }
  int lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(lv, rv);
}
int n, q;
PiP p[214514];
signed main() {
  cin >> n >> q;
  vector<int> vec;
  rep(i, n) {
    cin >> p[i].second.first >> p[i].second.second >> p[i].first;
    vec.push_back(p[i].second.first - p[i].first);
    vec.push_back(p[i].second.second - p[i].first);
  }
  vecunique(vec);
  map<int, int> mp;
  rep(i, vec.size()) { mp[vec[i]] = i; }
  init(vec.size());
  sort(p, p + n, greater<>());
  rep(i, n) {
    update(mp[p[i].second.first - p[i].first],
           mp[p[i].second.second - p[i].first], 0, p[i].first, 0, size_);
  }
  rep(i, q) {
    int a;
    cin >> a;
    int k = upper_bound(all(vec), a) - vec.begin();
    if (k == 0)
      cout << -1 << endl;
    else {
      int m = query(mp[vec[k - 1]], mp[vec[k - 1]] + 1, 0, 0, size_);
      if (m == inf)
        cout << -1 << endl;
      else
        cout << m << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(v) v.begin(), v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define vecunique(vec)                                                         \
  sort(vec.begin(), vec.end());                                                \
  decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());        \
  vec.erase(result, vec.end())
using namespace std;

bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return x > 1;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x) {
  if (x == 0)
    return 1;
  return kai(x - 1) * x % mod;
}
int mod_pow(int x, int y, int mod_) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % mod_;
    }
    x = x * x % mod_;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x) * mod_pow(kai(x - y), mod - 2, mod) % mod *
         mod_pow(kai(y), mod - 2, mod) % mod;
}
/*--------Library Zone!--------*/

P dat[10000000];
int size_ = 1;
void init(int x) {
  while (size_ < x)
    size_ *= 2;
  rep(i, 2 * size_ - 1) dat[i] = {inf, inf};
}
void eval(int k, int l, int r) {
  if (dat[k].second != inf) {
    dat[k].first = dat[k].second;
    if (r - l > 1) {
      dat[k * 2 + 1].second = dat[k].second;
      dat[k * 2 + 2].second = dat[k].second;
    }
    dat[k].second = inf;
  }
}
void update(int a, int b, int k, int x, int l, int r) {
  eval(k, l, r);
  if (r <= a || b <= l)
    return;
  if (a <= l && r <= b) {
    dat[k].second = x;
    eval(k, l, r);
  } else {
    update(a, b, k * 2 + 1, x, l, (l + r) / 2);
    update(a, b, k * 2 + 2, x, (l + r) / 2, r);
    dat[k].first = min(dat[k * 2 + 1].first, dat[k * 2 + 2].first);
  }
}
int query(int a, int b, int k, int l, int r) {
  eval(k, l, r);
  if (r <= a || b <= l)
    return inf;
  if (a <= l && r <= b) {
    return dat[k].first;
  }
  int lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(lv, rv);
}
int n, q;
PiP p[214514];
signed main() {
  cin >> n >> q;
  vector<int> vec;
  rep(i, n) {
    cin >> p[i].second.first >> p[i].second.second >> p[i].first;
    vec.push_back(p[i].second.first - p[i].first);
    vec.push_back(p[i].second.second - p[i].first);
  }
  vecunique(vec);
  map<int, int> mp;
  rep(i, vec.size()) { mp[vec[i]] = i; }
  init(vec.size());
  sort(p, p + n, greater<>());
  rep(i, n) {
    update(mp[p[i].second.first - p[i].first],
           mp[p[i].second.second - p[i].first], 0, p[i].first, 0, size_);
  }
  rep(i, q) {
    int a;
    cin >> a;
    int k = upper_bound(all(vec), a) - vec.begin();
    if (k == 0)
      cout << -1 << endl;
    else {
      int m = query(mp[vec[k - 1]], mp[vec[k - 1]] + 1, 0, 0, size_);
      if (m == inf)
        cout << -1 << endl;
      else
        cout << m << endl;
    }
  }
}