Problem p03033 (C++)

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

template <class type> class SegmentTree {
public:
  int size;
  vector<type> tree;
  type def;
  type (*marge)(type, type);

  SegmentTree(int n, type d, type (*m)(type, type)) {
    size = 1;
    def = d;
    marge = m;
    n++;

    while (size < n) {
      size *= 2;
    }

    tree.resize(2 * size);

    for (int i = 0; i < size; i++) {
      tree[size - 1 + i] = def;
    }

    for (int i = size - 2; i >= 0; i--) {
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  SegmentTree(int n, type array[], type d, type (*m)(type, type)) {
    size = 1;
    def = d;
    marge = m;
    n++;

    while (size < n) {
      size *= 2;
    }

    tree.resize(2 * size);

    for (int i = 0; i < size; i++) {
      if (i < n)
        tree[size - 1 + i] = array[i];
      else
        tree[size - 1 + i] = def;
    }

    for (int i = size - 2; i >= 0; i--) {
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  void update(int i, type val) {
    i = size - 1 + i;
    tree[i] = val;

    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  void add(int i, type val) {
    i = size - 1 + i;
    tree[i] = tree[i] + val;

    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  type get(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return tree[k];

    type vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    type vr = get(a, b, 2 * k + 2, (l + r) / 2, r);

    return marge(vl, vr);
  }

  type get(int a, int b) { return get(a, b, 0, 0, size); }
};

int marge(int a, int b) { return min(a, b); }

typedef pair<int, pair<int, int>> query;

int main() {
  int n, q;
  cin >> n >> q;
  priority_queue<query, vector<query>, greater<query>> que;

  int a, b, c;

  vector<int> za;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    que.push(make_pair((a - c) * 2, make_pair(1, c)));
    que.push(make_pair((b - c) * 2, make_pair(2, c)));
    za.push_back(c);
  }

  int d;
  sort(za.begin(), za.end());

  for (int i = 0; i < q; i++) {
    cin >> d;
    que.push(make_pair(d * 2 + 1, make_pair(3, 0)));
  }

  SegmentTree<int> seg(za.size(), INT_MAX, marge);

  int cnt[100010] = {};

  while (!que.empty()) {
    if (que.top().second.first == 1) {
      int idx = lower_bound(za.begin(), za.end(), que.top().second.second) -
                za.begin();
      if (cnt[idx] == 0) {
        seg.update(lower_bound(za.begin(), za.end(), que.top().second.second) -
                       za.begin(),
                   que.top().second.second);
      }
      cnt[idx]++;
    } else if (que.top().second.first == 2) {
      int idx = lower_bound(za.begin(), za.end(), que.top().second.second) -
                za.begin();
      cnt[idx]--;
      if (cnt[idx] == 0) {
        seg.update(lower_bound(za.begin(), za.end(), que.top().second.second) -
                       za.begin(),
                   INT_MAX);
      }
    } else {
      int ans = seg.get(0, za.size());
      if (ans == INT_MAX) {
        cout << -1 << endl;
      } else {
        cout << ans << endl;
      }
    }
    que.pop();
  }

  return 0;
}


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

template <class type> class SegmentTree {
public:
  int size;
  vector<type> tree;
  type def;
  type (*marge)(type, type);

  SegmentTree(int n, type d, type (*m)(type, type)) {
    size = 1;
    def = d;
    marge = m;
    n++;

    while (size < n) {
      size *= 2;
    }

    tree.resize(2 * size);

    for (int i = 0; i < size; i++) {
      tree[size - 1 + i] = def;
    }

    for (int i = size - 2; i >= 0; i--) {
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  SegmentTree(int n, type array[], type d, type (*m)(type, type)) {
    size = 1;
    def = d;
    marge = m;
    n++;

    while (size < n) {
      size *= 2;
    }

    tree.resize(2 * size);

    for (int i = 0; i < size; i++) {
      if (i < n)
        tree[size - 1 + i] = array[i];
      else
        tree[size - 1 + i] = def;
    }

    for (int i = size - 2; i >= 0; i--) {
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  void update(int i, type val) {
    i = size - 1 + i;
    tree[i] = val;

    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  void add(int i, type val) {
    i = size - 1 + i;
    tree[i] = tree[i] + val;

    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] = marge(tree[i * 2 + 1], tree[i * 2 + 2]);
    }

    return;
  }

  type get(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return tree[k];

    type vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    type vr = get(a, b, 2 * k + 2, (l + r) / 2, r);

    return marge(vl, vr);
  }

  type get(int a, int b) { return get(a, b, 0, 0, size); }
};

int marge(int a, int b) { return min(a, b); }

typedef pair<int, pair<int, int>> query;

int main() {
  int n, q;
  cin >> n >> q;
  priority_queue<query, vector<query>, greater<query>> que;

  int a, b, c;

  vector<int> za;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    que.push(make_pair((a - c) * 2, make_pair(1, c)));
    que.push(make_pair((b - c) * 2, make_pair(2, c)));
    za.push_back(c);
  }

  int d;
  sort(za.begin(), za.end());

  for (int i = 0; i < q; i++) {
    cin >> d;
    que.push(make_pair(d * 2 + 1, make_pair(3, 0)));
  }

  SegmentTree<int> seg(za.size(), INT_MAX, marge);

  int cnt[200010] = {};

  while (!que.empty()) {
    if (que.top().second.first == 1) {
      int idx = lower_bound(za.begin(), za.end(), que.top().second.second) -
                za.begin();
      if (cnt[idx] == 0) {
        seg.update(lower_bound(za.begin(), za.end(), que.top().second.second) -
                       za.begin(),
                   que.top().second.second);
      }
      cnt[idx]++;
    } else if (que.top().second.first == 2) {
      int idx = lower_bound(za.begin(), za.end(), que.top().second.second) -
                za.begin();
      cnt[idx]--;
      if (cnt[idx] == 0) {
        seg.update(lower_bound(za.begin(), za.end(), que.top().second.second) -
                       za.begin(),
                   INT_MAX);
      }
    } else {
      int ans = seg.get(0, za.size());
      if (ans == INT_MAX) {
        cout << -1 << endl;
      } else {
        cout << ans << endl;
      }
    }
    que.pop();
  }

  return 0;
}