Problem p03641 (C++)

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

class Node {
public:
  Node(pair<LL, LL> v = unitValue) : value(v.first), index(v.second) {}

  Node operator+(const Node &monoid) const {
    if (value < monoid.value) {
      return Node(make_pair(value, index));
    } else {
      return monoid;
    }
  }

  static Node unit() { return Node(unitValue); }

  void setValue(LL l) { this->value = l; }

  pair<LL, LL> getValue() const { return make_pair(this->value, this->index); }

private:
  LL value;
  LL index;
  static const pair<LL, LL> unitValue;
};
const pair<LL, LL> Node::unitValue = make_pair(INF_INT_MAX, (LL)-1);

template <class T> class SegmentTree {
public:
  SegmentTree(int size = 1) { init(size); }

  void init(int size) {
    vec.clear();
    k = 1;
    while (k < size) {
      k *= 2;
    }
    for (int i = 0; i < 2 * k; i++) {
      vec.push_back(T::unit());
    }
  }

  void update(int i, T x) { // iをxに変更する
    i += k - 1;
    vec[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      vec[i] = vec[i * 2 + 1] + vec[i * 2 + 2];
    }
  }

  T find(int l, int r) { return _find(l, r, 0, 0, k); }

private:
  vector<T> vec;
  int k;

  T _find(int l, int r, int k, int x, int y) {
    if (r <= x || y <= l) { // 重なってない場合
      return T::unit();
    } else if (l <= x && y <= r) { // 完全にかぶってる場合
      return vec[k];
    } else { // 中途半端にかぶってる場合
      int mid = (x + y) / 2;
      T lNode = _find(l, r, 2 * k + 1, x, mid);
      T rNode = _find(l, r, 2 * k + 2, mid, y);
      return lNode + rNode;
    }
  }
};

using II = pair<int, int>;

const int MAX_N = 100005;
int N;
int P[MAX_N];
SegmentTree<Node> sg[2];

class PQNode {
public:
  PQNode(int l = 0, int r = 0, int value = -1) : l(l), r(r), value(value) {}
  int l, r, value;
  bool operator<(const PQNode &pqNode) const { return value < pqNode.value; }
  bool operator>(const PQNode &pqNode) const { return value > pqNode.value; }
};

signed main() {
  cin >> N;

  sg[0].init(N);
  sg[1].init(N);

  REP(i, N) {
    cin >> P[i];
    Node n(make_pair(P[i], i));
    sg[i % 2].update(i, n);
  }

  vector<int> ans;
  priority_queue<PQNode, vector<PQNode>, greater<PQNode>> pq;
  pq.push(PQNode(0, N, sg[0].find(0, N).getValue().first));

  while (pq.size()) {
    auto p = pq.top();
    pq.pop();
    int l = p.l, r = p.r;
    if (r - l <= 0) {
      continue;
    }
    int i = sg[l % 2].find(l, r).getValue().second;
    int j = sg[(i + 1) % 2].find(i + 1, r).getValue().second;
    ans.push_back(P[i]);
    ans.push_back(P[j]);
    pq.push(PQNode(l, i, sg[l % 2].find(l, i).getValue().first));
    pq.push(PQNode(i + 1, j, sg[(i + 1) % 2].find(i + 1, j).getValue().first));
    pq.push(PQNode(j + 1, r, sg[(j + 1) % 2].find(j + 1, r).getValue().first));
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i];
    if (i < ans.size() - 1) {
      cout << " ";
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

class Node {
public:
  Node(pair<LL, LL> v = unitValue) : value(v.first), index(v.second) {}

  Node operator+(const Node &monoid) const {
    if (value < monoid.value) {
      return Node(make_pair(value, index));
    } else {
      return monoid;
    }
  }

  static Node unit() { return Node(unitValue); }

  void setValue(LL l) { this->value = l; }

  pair<LL, LL> getValue() const { return make_pair(this->value, this->index); }

private:
  LL value;
  LL index;
  static const pair<LL, LL> unitValue;
};
const pair<LL, LL> Node::unitValue = make_pair(INF_INT_MAX, (LL)-1);

template <class T> class SegmentTree {
public:
  SegmentTree(int size = 1) { init(size); }

  void init(int size) {
    vec.clear();
    k = 1;
    while (k < size) {
      k *= 2;
    }
    for (int i = 0; i < 2 * k; i++) {
      vec.push_back(T::unit());
    }
  }

  void update(int i, T x) { // iをxに変更する
    i += k - 1;
    vec[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      vec[i] = vec[i * 2 + 1] + vec[i * 2 + 2];
    }
  }

  T find(int l, int r) { return _find(l, r, 0, 0, k); }

private:
  vector<T> vec;
  int k;

  T _find(int l, int r, int k, int x, int y) {
    if (r <= x || y <= l) { // 重なってない場合
      return T::unit();
    } else if (l <= x && y <= r) { // 完全にかぶってる場合
      return vec[k];
    } else { // 中途半端にかぶってる場合
      int mid = (x + y) / 2;
      T lNode = _find(l, r, 2 * k + 1, x, mid);
      T rNode = _find(l, r, 2 * k + 2, mid, y);
      return lNode + rNode;
    }
  }
};

using II = pair<int, int>;

const int MAX_N = 200005;
int N;
int P[MAX_N];
SegmentTree<Node> sg[2];

class PQNode {
public:
  PQNode(int l = 0, int r = 0, int value = -1) : l(l), r(r), value(value) {}
  int l, r, value;
  bool operator<(const PQNode &pqNode) const { return value < pqNode.value; }
  bool operator>(const PQNode &pqNode) const { return value > pqNode.value; }
};

signed main() {
  cin >> N;

  sg[0].init(N);
  sg[1].init(N);

  REP(i, N) {
    cin >> P[i];
    Node n(make_pair(P[i], i));
    sg[i % 2].update(i, n);
  }

  vector<int> ans;
  priority_queue<PQNode, vector<PQNode>, greater<PQNode>> pq;
  pq.push(PQNode(0, N, sg[0].find(0, N).getValue().first));

  while (pq.size()) {
    auto p = pq.top();
    pq.pop();
    int l = p.l, r = p.r;
    if (r - l <= 0) {
      continue;
    }
    int i = sg[l % 2].find(l, r).getValue().second;
    int j = sg[(i + 1) % 2].find(i + 1, r).getValue().second;
    ans.push_back(P[i]);
    ans.push_back(P[j]);
    pq.push(PQNode(l, i, sg[l % 2].find(l, i).getValue().first));
    pq.push(PQNode(i + 1, j, sg[(i + 1) % 2].find(i + 1, j).getValue().first));
    pq.push(PQNode(j + 1, r, sg[(j + 1) % 2].find(j + 1, r).getValue().first));
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i];
    if (i < ans.size() - 1) {
      cout << " ";
    }
  }
  cout << endl;

  return 0;
}