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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

typedef struct node {
  int s, e, val, pos;
  bool operator<(const node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  node(){};
  node(int ss, int ee, int v, int p) { s = ss, e = ee, val = v, pos = p; }
} node;

int occs[(int)1e6 + 5], tree[1000123], n, ans[1000123];

void update(int x, int val) {
  while (x <= n) {
    tree[x] += val;
    x += x & -x;
  }
}

int query(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {

  int q, s, e;
  vector<node> v;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s);
    v.push_back(node(i, i, s, -1));
  }
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &e);
    v.push_back(node(s, e, 0, i));
  }

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

  for (int i = 0; i < v.size(); i++) {
    if (v[i].val == 0) {
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    } else {
      if (occs[v[i].val] != 0)
        update(occs[v[i].val], -1);
      update(v[i].s, 1);
      occs[v[i].val] = v[i].s;
    }
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: 0

Fixed submission:

// C++ Program to find the distinct
// elements in a range
#include <bits/stdc++.h>
using namespace std;

// Function to perform queries in a range
long long query(int start, int end, int left, int right, int node,
                long long seg[]) {
  // No overlap
  if (end < left || start > right) {
    return 0;
  }

  // Totally Overlap
  else if (start >= left && end <= right) {
    return seg[node];
  }

  // Parital Overlap
  else {
    int mid = (start + end) / 2;

    // Finding the Answer
    // for the left Child
    long long leftChild = query(start, mid, left, right, 2 * node, seg);

    // Finding the Answer
    // for the right Child
    long long rightChild = query(mid + 1, end, left, right, 2 * node + 1, seg);

    // Combining the BitMasks
    return (leftChild | rightChild);
  }
}

// Function to perform update operation
// in the Segment seg
void update(int left, int right, int index, int Value, int node, int ar[],
            long long seg[]) {
  if (left == right) {
    ar[index] = Value;

    // Forming the BitMask
    seg[node] = (1LL << Value);
    return;
  }

  int mid = (left + right) / 2;
  if (index > mid) {

    // Updating the left Child
    update(mid + 1, right, index, Value, 2 * node + 1, ar, seg);
  } else {

    // Updating the right Child
    update(left, mid, index, Value, 2 * node, ar, seg);
  }

  // Updating the BitMask
  seg[node] = (seg[2 * node] | seg[2 * node + 1]);
}

// Building the Segment Tree
void build(int left, int right, int node, int ar[], long long seg[]) {
  if (left == right) {

    // Building the Initial BitMask
    seg[node] = (1LL << ar[left]);

    return;
  }

  int mid = (left + right) / 2;

  // Building the left seg tree
  build(left, mid, 2 * node, ar, seg);

  // Building the right seg tree
  build(mid + 1, right, 2 * node + 1, ar, seg);

  // Forming the BitMask
  seg[node] = (seg[2 * node] | seg[2 * node + 1]);
}

// Utility Function to answer the queries
void getDistinctCount(vector<vector<int>> &queries, int ar[], long long seg[],
                      int n) {

  for (int i = 0; i < queries.size(); i++) {

    int op = queries[i][0];

    if (op == 2) {

      int l = queries[i][1], r = queries[i][2];

      long long tempMask = query(0, n - 1, l - 1, r - 1, 1, seg);

      int countOfBits = 0;

      // Counting the set bits which denote the
      // distinct elements
      for (int i = 63; i >= 0; i--) {

        if (tempMask & (1LL << i)) {

          countOfBits++;
        }
      }

      cout << countOfBits << '\\n';
    } else {

      int index = queries[i][1];
      int val = queries[i][2];

      // Updating the value
      update(0, n - 1, index - 1, val, 1, ar, seg);
    }
  }
}

const int mx = 5e5 + 123;
long long seg[5 * mx] = {0};
// Driver Code
int main() {

  //	int n = 7;
  int ar[] = {};

  // long long seg[4 * n] = { 0 };
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  build(0, n - 1, 1, ar, seg);

  vector<vector<int>> queries;

  while (q--) {
    int l, r;
    cin >> l >> r;
    // l--, r--;
    queries.push_back({2, l, r});
  }

  getDistinctCount(queries, ar, seg, n);

  return 0;
}