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

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 5e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

struct BITree {
  int t[N];
  int Lowbit(int x) { return x & -x; }
  void Add(int x, int det) {
    if (x < 0)
      while (1)
        ;
    for (; x < N; x += Lowbit(x))
      t[x] += det;
  }
  int Query(int x) {
    if (x < 0)
      while (1)
        ;
    int ret = 0;
    for (; x; x -= Lowbit(x))
      ret += t[x];
    return ret;
  }
};
BITree bt;

struct Query {
  int l, r, id, ans;
};
Query qry[N];
vector<int> pos[N];

int main() {
  ios::sync_with_stdio(0);

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    pos[c].push_back(i);
  }
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qry[i] = (Query){l, r, i, 0};
  }
  sort(qry + 1, qry + q + 1, [](Query a, Query b) { return a.r < b.r; });

  static vector<tint> pts;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < pos[i].size(); j++) {
      pts.push_back(make_tuple(pos[i][j], pos[i][j], 1));
      if (j > 0)
        pts.push_back(make_tuple(pos[i][j - 1], pos[i][j], -1));
    }
  }
  sort(all(pts), [](tint a, tint b) { return get<1>(a) < get<1>(b); });

  auto p = pts.begin();
  for (int i = 1; i <= q; i++) {
    int l = qry[i].l, r = qry[i].r;
    while (p != pts.end() && get<1>(*p) <= r) {
      bt.Add(get<0>(*p), get<2>(*p));
      p++;
    }
    qry[i].ans = bt.Query(n) - bt.Query(l - 1);
  }
  sort(qry + 1, qry + q + 1, [](Query a, Query b) { return a.id < b.id; });

  for (int i = 1; i <= q; i++)
    cout << qry[i].ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 5e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

struct BITree {
  int t[N];
  int Lowbit(int x) { return x & -x; }
  int Add(int x, int det) {
    for (; x < N; x += Lowbit(x))
      t[x] += det;
  }
  int Query(int x) {
    int ret = 0;
    for (; x; x -= Lowbit(x))
      ret += t[x];
    return ret;
  }
};
BITree bt;

struct Query {
  int l, r, id, ans;
};
Query qry[N];
vector<int> pos[N];

int main() {
  ios::sync_with_stdio(0);

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    pos[c].push_back(i);
  }
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qry[i] = (Query){l, r, i, 0};
  }
  sort(qry + 1, qry + q + 1, [](Query a, Query b) { return a.r < b.r; });

  static vector<tint> pts;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < pos[i].size(); j++) {
      pts.push_back(make_tuple(pos[i][j], pos[i][j], 1));
      if (j > 0)
        pts.push_back(make_tuple(pos[i][j - 1], pos[i][j], -1));
    }
  }
  sort(all(pts), [](tint a, tint b) { return get<1>(a) < get<1>(b); });

  auto p = pts.begin();
  for (int i = 1; i <= q; i++) {
    int l = qry[i].l, r = qry[i].r;
    while (p != pts.end() && get<1>(*p) <= r) {
      bt.Add(get<0>(*p), get<2>(*p));
      p++;
    }
    qry[i].ans = bt.Query(n) - bt.Query(l - 1);
  }
  sort(qry + 1, qry + q + 1, [](Query a, Query b) { return a.id < b.id; });

  for (int i = 1; i <= q; i++)
    cout << qry[i].ans << endl;

  return 0;
}