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

/*
 * John Wick is a man of
 * focus, commitment & sheer will
 */
#include <bits/stdc++.h>
using namespace std;
// -----------------<fast IO>------------------
#define thisIsSpeed                                                            \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FLSH fflush(stdout)
#define fileIO(name)                                                           \\
  freopen(name ".in", "r", stdin);                                             \\
  freopen(name ".out", "w", stdout);
#define PRECISION(x) cout << fixed << setprecision(x);
#define endl "\\n"
// -----------------<Container manipulation / traversal
// macros>------------------
#define pb push_back
#define sz(x) ((ll)((x).size()))
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define forn(i, a, b) for (ll(i) = (a); (i) <= (b); (i)++)
#define ford(i, a, b) for (ll(i) = (b); (i) >= (a); (i)--)
#define fill(l, a) memset((l), (a), sizeof(l))
#define ss second
#define ff first
// -----------------<Some common useful functions>------------------
#define checkbit(n, b) ((n >> b) & 1)
#define in(b, a) ((b).find(a) != (b).end())
// -----------------<TypeDef Start>------------------
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vi;
typedef map<ll, ll> mii;
// -----------------<Constants>------------------
const ll MAXN = 5000005;
const ll SQRTN = 1003;
const ll LOGN = 22;
const double PI = acos(-1);
const ll INF = 1e9;
const ll MOD = 1000000009;
const ll FMOD = 998244353;
const double eps = 1e-9;
// -----------------<Modular Arithmetic>------------------
template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); }
template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); }
template <typename T> T add(T a, T b, T c = MOD) {
  T res = a + b;
  return (res >= c ? res - c : res);
}
template <typename T> T mod_neg(T a, T b, T c = MOD) {
  T res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
template <typename T> T mul(T a, T b, T c = MOD) {
  ll res = (ll)a * b;
  return (res >= c ? res % c : res);
}
inline ll mulmod(ll a, ll b, ll m = MOD) {
  ll q = (ll)(((ld)a * (ld)b) / (ld)m);
  ll r = a * b - q * m;
  if (r > m)
    r %= m;
  if (r < 0)
    r += m;
  return r;
}
template <typename T> T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T power(T e, T n, T m = MOD) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T> T extended_euclid(T a, T b, T &x, T &y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T> T mod_inverse(T a, T n = MOD) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}
// ------------------nCr---------------------------
/*
ll modInverse(ll n, ll p){return power(n, p-2);}
ll nCr(ll n, ll r, ll p=MOD){if (r==0) return 1;ll
fac[n+1];fac[0]=1;for(lli=1;i<=n;i++)fac[i]=fac[i-1]*i%p; return
(fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;}
*/
//-----------------sieve--------------------------
/*
//bool C[MAXN];
ll C[MAXN]={0};
vi V1;
void sieve(){
        ll ans=1;
    ll i,j;
    for(i=2;i<MAXN;i++){if(!C[i] && i<20){
                ans*=i;
        // V1.pb(i);
        for(j=2*i;j<MAXN;j=j+i){C[j]=true;}}}
        cout<<ans<<endl;
}
*/
//-----------------custom structuree----------------

struct Query {
  int l, r, idx;
  // string str;
};

//-----------------custom compare---------------------

bool compare(Query i1, Query i2) { return i1.r < i2.r; }

// --------------------------------------------------</TEMPLATE>--------------------------------------------------

const int MAX = 1000001;

// 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 mainn() {
  int z = 0, k = 0, t = 0, n = 0, x = 0, m = 0, curr = 0, num = 0, n1 = 0,
      n2 = 0, n3 = 0, n4 = 0, i = 0, j = 0;
  ll root = 0, sum = 0, diff = 0, q = 0, choice = 0, d = 0, len = 1, beg = 0,
     end = 0, pos = 0, cnt = 0;
  ll lo = 0, hi = 0, mid = 0;
  bool flag = false;
  string s1, s2, s3, str;
  char ch = 'a', ch1, ch2, ch3, *ptr;
  double dub = 0;
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  cin >> n >> t;
  int arr[n];
  Query queries[t];
  rep(i, n) { cin >> arr[i]; }

  rep(i, t) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].l--;
    queries[i].r--;
    queries[i].idx = i;
  }

  sort(queries, queries + t, compare);
  answeringQueries(arr, n, queries, t);
}

int main() {
  thisIsSpeed;
  ll t = 1;
  // cin>>t;
  while (t--) {
    mainn();
  }
  return 0;
}


Error: 0

Fixed submission:

/*
 * John Wick is a man of
 * focus, commitment & sheer will
 */
#include <bits/stdc++.h>
using namespace std;
// -----------------<fast IO>------------------
#define thisIsSpeed                                                            \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FLSH fflush(stdout)
#define fileIO(name)                                                           \\
  freopen(name ".in", "r", stdin);                                             \\
  freopen(name ".out", "w", stdout);
#define PRECISION(x) cout << fixed << setprecision(x);
#define endl "\\n"
// -----------------<Container manipulation / traversal
// macros>------------------
#define pb push_back
#define sz(x) ((int)((x).size()))
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define forn(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define ford(i, a, b) for (int(i) = (b); (i) >= (a); (i)--)
#define fill(l, a) memset((l), (a), sizeof(l))
#define ss second
#define ff first
// -----------------<Some common useful functions>------------------
#define checkbit(n, b) ((n >> b) & 1)
#define in(b, a) ((b).find(a) != (b).end())
// -----------------<TypeDef Start>------------------
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vi;
typedef map<ll, ll> mii;
// -----------------<Constants>------------------
const int MAXN = 1000005;
const int SQRTN = 1003;
const int LOGN = 22;
const double PI = acos(-1);
const int INF = 1e9;
const int MOD = 1000000009;
const int FMOD = 998244353;
const double eps = 1e-9;
// -----------------<Modular Arithmetic>------------------
template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); }
template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); }
template <typename T> T add(T a, T b, T c = MOD) {
  T res = a + b;
  return (res >= c ? res - c : res);
}
template <typename T> T mod_neg(T a, T b, T c = MOD) {
  T res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
template <typename T> T mul(T a, T b, T c = MOD) {
  ll res = (ll)a * b;
  return (res >= c ? res % c : res);
}
inline ll mulmod(ll a, ll b, ll m = MOD) {
  ll q = (ll)(((ld)a * (ld)b) / (ld)m);
  ll r = a * b - q * m;
  if (r > m)
    r %= m;
  if (r < 0)
    r += m;
  return r;
}
template <typename T> T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T power(T e, T n, T m = MOD) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T> T extended_euclid(T a, T b, T &x, T &y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T> T mod_inverse(T a, T n = MOD) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}
// ------------------nCr---------------------------
/*
ll modInverse(ll n, ll p){return power(n, p-2);}
ll nCr(ll n, ll r, ll p=MOD){if (r==0) return 1;ll
fac[n+1];fac[0]=1;for(lli=1;i<=n;i++)fac[i]=fac[i-1]*i%p; return
(fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;}
*/
//-----------------sieve--------------------------
/*
//bool C[MAXN];
ll C[MAXN]={0};
vi V1;
void sieve(){
        ll ans=1;
    ll i,j;
    for(i=2;i<MAXN;i++){if(!C[i] && i<20){
                ans*=i;
        // V1.pb(i);
        for(j=2*i;j<MAXN;j=j+i){C[j]=true;}}}
        cout<<ans<<endl;
}
*/
//-----------------custom structuree----------------
/*
struct cus{
        ll x,y;
        //string str;
};
*/
//-----------------custom compare---------------------
/*
bool compare(cus i1, cus i2){
    if(i1.x != i2.x)
                return i1.x < i2.x;
        else return i1.y < i2.y;
}
*/
// --------------------------------------------------</TEMPLATE>--------------------------------------------------

set<int> *segment;
void build(int i, int s, int e, int arr[]) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);
  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 (b < l || a > r)
    return result;
  if (a <= l && r <= b)
    return segment[node];

  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];
}

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;
}

// Driver code
int main() {
  int n, q;
  cin >> n >> q;
  int arr[n];
  rep(i, n) { cin >> arr[i]; }
  n = sizeof(arr) / sizeof(arr[0]);

  init(n);

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

  rep(i, q) {
    int n1, n2;
    cin >> n1 >> n2;
    getDistinct(n1 - 1, n2 - 1, n);
  }
}