Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

struct Node {
  int x, y, index;
  bool operator<(const Node &n) const {
    return make_pair(x, y) < make_pair(n.x, n.y);
  }
};

class Segment_Tree {
  vector<long long int> v;
  int num;
  long long int ret;
  bool is_min;
  long long int Update(int place) {
    if (place >= v.size() / 2) {
      return v[place];
    }
    if (is_min) {
      v[place] = min(Update(place * 2), Update(place * 2 + 1));
      return v[place];
    }
    v[place] = max(Update(place * 2), Update(place * 2 + 1));
    return v[place];
  }

public:
  Segment_Tree(int n, bool min) {
    n++;
    num = 1;
    while (num < n * 2) {
      num *= 2;
    }
    is_min = min;
    if (min) {
      v.resize(num, MOD * MOD);
    } else
      v.resize(num, -MOD * MOD);
  }
  void Insert(int place, long long int num, bool update) {
    place += v.size() / 2;
    v[place] = num;
    if (!update)
      return;
    place /= 2;
    while (place) {
      if (is_min)
        v[place] = min(v[place * 2], v[place * 2 + 1]);
      else
        v[place] = max(v[place * 2], v[place * 2 + 1]);
      place /= 2;
    }
  }
  void TopDown() { Update(1); }
  long long int RMQ(int a, int b) {
    if (is_min)
      ret = INT_MAX;
    else
      ret = INT_MIN;
    if (is_min) {
      b++;
      for (a += num / 2, b += num / 2; a < b; a >>= 1, b >>= 1) {
        if (a & 1)
          ret = min(ret, v[a++]);
        if (b & 1)
          ret = min(ret, v[--b]);
      }
    } else {
      b++;
      for (a += num / 2, b += num / 2; a < b; a >>= 1, b >>= 1) {
        if (a & 1)
          ret = max(ret, v[a++]);
        if (b & 1)
          ret = max(ret, v[--b]);
      }
    }
    return ret;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  vector<Node> v(N);
  vector<int> y_to_x(N);
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i].x >> v[i].y;
    v[i].x--, v[i].y--;
    v[i].index = i;
    x[i] = y_to_x[v[i].y] = v[i].x;
  }
  sort(v.begin(), v.end());
  vector<int> ans(N);
  Segment_Tree msg(N, true);
  Segment_Tree Msg(N, false);
  for (int i = 0; i < N; i++) {
    msg.Insert(i, v[i].y, false);
    Msg.Insert(i, v[i].y, false);
  }
  msg.TopDown();
  Msg.TopDown();
  for (auto i : v) {
    if (ans[i.index])
      continue;
    int cnt = 1;
    queue<int> Q;
    vector<int> box;
    Q.push(i.x);
    box.push_back(i.index);
    msg.Insert(i.x, MOD, true);
    Msg.Insert(i.x, -MOD, true);
    while (!Q.empty()) {
      int cx = Q.front();
      int cy = v[cx].y;
      Q.pop();
      while (Msg.RMQ(cx + 1, N - 1) > cy) {
        cnt++;
        auto xp = y_to_x[Msg.RMQ(cx + 1, N - 1)];
        Q.push(xp);
        box.push_back(v[xp].index);
        Msg.Insert(xp, -MOD, true);
        msg.Insert(xp, MOD, true);
      }
      while (msg.RMQ(0, cx - 1) < cy) {
        cnt++;
        auto xp = y_to_x[msg.RMQ(0, cx - 1)];
        Q.push(xp);
        box.push_back(v[xp].index);
        msg.Insert(xp, MOD, true);
        Msg.Insert(xp, -MOD, true);
      }
    }
    for (auto j : box) {
      ans[j] = cnt;
    }
  }
  for (auto i : ans)
    cout << i << endl;
}

Error: -11

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

struct Node {
  int x, y, index;
  bool operator<(const Node &n) const {
    return make_pair(x, y) < make_pair(n.x, n.y);
  }
};

class Add_Segment_Tree {
  vector<long long int> v;
  long long int ret;
  int num;
  long long int Update(int place) {
    if (place >= v.size() / 2) {
      return v[place];
    }
    v[place] = Update(place * 2) + Update(place * 2 + 1);
    return v[place];
  }

public:
  Add_Segment_Tree(int n) {
    n++;
    num = 1;
    while (num < n * 2)
      num *= 2;
    v.resize(num, 0);
  }
  void Add(int place, long long int num, bool update) {
    place += v.size() / 2;
    v[place] += num;
    if (!update)
      return;
    place /= 2;
    while (place) {
      v[place] = v[place * 2] + v[place * 2 + 1];
      place /= 2;
    }
  }
  void TopDown() { Update(1); }
  long long int Sum(int a, int b) {
    ret = 0;
    b++;
    for (a += num / 2, b += num / 2; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        ret += v[a++];
      if (b & 1)
        ret += v[--b];
    }
    return ret;
  }
};

class UnionFind {
  vector<int> parent;
  vector<int> rank;

public:
  UnionFind(int num) {
    num++;
    parent.resize(num);
    rank.resize(num);
    for (int i = 0; i < num; i++) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  int Find(int node) {
    if (parent[node] == node)
      return node;
    else
      return parent[node] = Find(parent[node]);
  }
  void Unite(int u, int v) {
    u = Find(u);
    v = Find(v);
    if (u == v)
      return;
    if (rank[u] < rank[v])
      parent[u] = v;
    else {
      parent[v] = u;
      if (rank[u] == rank[v])
        rank[u]++;
    }
  }
  bool Check_Same(int u, int v) { return Find(u) == Find(v); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  vector<Node> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i].x >> v[i].y;
    v[i].index = i;
  }
  sort(v.begin(), v.end());
  vector<int> ans(N);
  UnionFind uf(N);
  {
    map<int, int> mp;
    for (auto i : v) {
      if (prev(mp.lower_bound(i.y)) != mp.end())
        uf.Unite(i.index, prev(mp.lower_bound(i.y))->second);
      mp[i.y] = i.index;
    }
  }
  {
    map<int, int> mp;
    reverse(v.begin(), v.end());
    for (auto i : v) {
      if (mp.lower_bound(i.y) != mp.end())
        uf.Unite(i.index, mp.lower_bound(i.y)->second);
      mp[i.y] = i.index;
    }
  }
  for (int i = 0; i < N; i++) {
    ans[uf.Find(i)]++;
  }
  for (int i = 0; i < N; i++) {
    cout << ans[uf.Find(i)] << endl;
  }
}