Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

/* RangeMinimumQuery(0-index) */

struct RMQ {
  typedef pair<int, int> Type;
  int segn2;
  Type inf;
  vector<Type> data;

  RMQ(int n) {
    inf = {-INF, 0}; // init value
    for (segn2 = 1; segn2 < n; segn2 *= 2)
      ;
    data.assign(segn2 * 2, inf);

    for (int i = 0; i < n; i++) {
      data[i + segn2 - 1] = {INF, i};
    }

    for (int i = segn2 - 2; i >= 0; i--) {
      data[i] = max(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }

  // get minimum value [a,b]
  Type query(int a, int b, int l = 0, int r = -1, int k = 0) {
    if (r == -1)
      r += segn2;
    if (a <= l && r <= b)
      return data[k];
    if (r < a || b < l)
      return inf;
    return max(query(a, b, l, (l + r) / 2, k * 2 + 1),
               query(a, b, (l + r) / 2 + 1, r, k * 2 + 2));
  }

  // set kth number x
  void set(int k, int x2) {
    pair<int, int> x = {x2, k};
    k += segn2 - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
    }
    return;
  }
};

int main() {
  int n, m;
  int L[SIZE], R[SIZE];

  vector<pair<int, int>> vec;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++) {
    scanf("%d%d", L + i, R + i);

    vec.push_back({R[i], L[i]});
  }

  sort(vec.begin(), vec.end());

  RMQ rmq(m + 1);
  priority_queue<int> pq;

  for (int i = n - 1; i >= 0; i--) {
    int r = vec[i].first;
    int l = vec[i].second;

    if (r > m) {
      pq.push(-l);
      continue;
    }

    auto p = rmq.query(r, m);

    if (p.first == INF) {
      rmq.set(p.second, l);
    } else {
      if (p.first > l) {
        rmq.set(p.second, l);
        pq.push(-p.first);
      } else {
        pq.push(-l);
      }
    }
  }

  int ans = 0;

  while (pq.size()) {

    auto p = rmq.query(1, -pq.top());

    if (p.first == INF) {
      rmq.set(p.second, 1);
    } else {
      ans++;
    }

    pq.pop();
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

/* RangeMinimumQuery(0-index) */

struct RMQ {
  typedef pair<int, int> Type;
  int segn2;
  Type inf;
  vector<Type> data;

  RMQ(int n) {
    inf = {-INF, 0}; // init value
    for (segn2 = 1; segn2 < n; segn2 *= 2)
      ;
    data.assign(segn2 * 2, inf);

    for (int i = 0; i < n; i++) {
      data[i + segn2 - 1] = {INF, i};
    }

    for (int i = segn2 - 2; i >= 0; i--) {
      data[i] = max(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }

  // get minimum value [a,b]
  Type query(int a, int b, int l = 0, int r = -1, int k = 0) {
    if (r == -1)
      r += segn2;
    if (a <= l && r <= b)
      return data[k];
    if (r < a || b < l)
      return inf;
    return max(query(a, b, l, (l + r) / 2, k * 2 + 1),
               query(a, b, (l + r) / 2 + 1, r, k * 2 + 2));
  }

  // set kth number x
  void set(int k, int x2) {
    pair<int, int> x = {x2, k};
    k += segn2 - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
    }
    return;
  }
};

int main() {
  int n, m;
  int L[SIZE], R[SIZE];

  vector<pair<int, int>> vec;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++) {
    scanf("%d%d", L + i, R + i);

    vec.push_back({R[i], L[i]});
  }

  sort(vec.begin(), vec.end());

  RMQ rmq(m + 1);
  priority_queue<int> pq;

  for (int i = n - 1; i >= 0; i--) {
    int r = vec[i].first;
    int l = vec[i].second;

    if (r > m) {
      pq.push(-l);
      continue;
    }

    auto p = rmq.query(r, m);

    if (p.first == INF) {
      rmq.set(p.second, l);
    } else {
      if (p.first > l) {
        rmq.set(p.second, l);
        pq.push(-p.first);
      } else {
        pq.push(-l);
      }
    }
  }

  int ans = 0;

  while (pq.size()) {

    auto p = rmq.query(1, -pq.top());

    if (p.first == INF) {
      rmq.set(p.second, 1);
    } else {
      ans++;
    }

    pq.pop();
  }

  cout << ans << endl;

  return 0;
}