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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double LD;
#define int ll
#define double LD
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define mem(a, b) memset(a, b, sizeof a)
#define append push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define print(x) cout << (x) << endl
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      cout << "Case #" << i << ": ";                                           \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vci vector<pair<char, int>>
#define vic vector<pair<int, char>>
#define N 200005
#define pb push_back
const double PI = acos(-1);
const int MOD = 1000000007;
const int FMOD = 998244353;

set<int> *segment;

const int MAX = 3000001;
struct Query {
  int l, r, idx;
};

// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }

// updating the bit array
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

// querying the bit array
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int arr[], int n, Query queries[], int q) {
  // initialising bit array
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  // holds the rightmost index of any number
  // as numbers of a[i] are less than or equal to 10^6
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  // answer for each query
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    // If last visit is not -1 update -1 at the
    // idx equal to last_visit[arr[i]]
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    // Setting last_visit[arr[i]] as i and updating
    // the bit array accordingly
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    // If i is equal to r of any query  store answer
    // for that query in ans[]
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

  // print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

void solve() {
  int n, q;
  cin >> n >> q;
  int a[n];
  loop(i, n) cin >> a[i];

  Query queries[q];
  loop(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].idx = i;
  }

  sort(queries, queries + q, cmp);
  answeringQueries(a, n, queries, q);
}
signed main() { solve(); }

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double LD;
#define int ll
#define double LD
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define mem(a, b) memset(a, b, sizeof a)
#define append push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define print(x) cout << (x) << endl
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      cout << "Case #" << i << ": ";                                           \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vci vector<pair<char, int>>
#define vic vector<pair<int, char>>
#define N 200005
#define pb push_back
const double PI = acos(-1);
const int MOD = 1000000007;
const int FMOD = 998244353;

set<int> *segment;

void build(int i, int s, int e, int arr[]) {

  // If start is equal to end then
  // insert the array element
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  // Else divide the range into two halves
  // (start to mid) and (mid+1 to end)
  // first half will be the left node
  // and the second half will be the right node
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

  // Insert the sets of right and left
  // node of the segment tree
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  // If the range is out of the bounds
  // of this segment
  if (b < l || a > r)
    return result;

  // If the range lies in this segment
  if (a <= l && r <= b)
    return segment[node];

  // Else query for the right and left
  // leaf node of this subtree
  // and insert them into the set
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  // Return the result
  return result;
}

void init(int n) {
  // Get the height of the segment tree
  int h = (int)ceil(log2(n));
  h = (2 * (pow(2, h))) - 1;

  // Initialize the segment tree
  segment = new set<int>[h];
}

// Function to get the result for the
// subarray from arr[l] to arr[r]
void getDistinct(int l, int r, int n) {
  // Query for the range set
  set<int> ans = query(1, 0, n - 1, l, r);

  cout << ans.size() << endl;
}
void solve() {
  int n, q;
  cin >> n >> q;

  int arr[n];
  loop(i, n) cin >> arr[i];
  //  int n = sizeof(arr) / sizeof(arr[0]);

  init(n);

  // Bulid the segment tree
  build(1, 0, n - 1, arr);

  // Query in range 0 to 4

  loop(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    getDistinct(l, r, n);
  }
}
signed main() { solve(); }