Problem p02599 (C++)

Buggy submission (status: Runtime Error):
// Author: Vinay Khilwani
// Language: C++

// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB,
// CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank

// Never Stop Trying.
// Trying to be Better than Myself.

// while(true)
// {
//     if(AC)
//     {
//         break;
//     }
//     else if(Contest Over)
//     {
//         Try.
//         Check out Editorial.
//         Understand.
//         Find out your Mistake.
//         Learn the topic (if new).
//         Solve Problems on that topic (if new).
//         Upsolve that problem.
//         break;
//     }
//     else
//     {
//         Try.
//         Use Pen-Paper.
//         Find errors, edge cases, etc.
//         continue;
//     }
// }

// Optimizations
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")

// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

// Debugging
#define dbg(a) cerr << a << "\n";
#define debug_a(a)                                                             \
  for (auto x : a) {                                                           \
    cerr << x << " ";                                                          \
  }                                                                            \
  cerr << "\n";
#define debug_b(a)                                                             \
  for (auto x : a) {                                                           \
    cerr << "[" << x.first << ", " << x.second << "]"                          \
         << "\n";                                                              \
  }                                                                            \
  cerr << "\n";
#define debug_c(a)                                                             \
  for (auto x : a) {                                                           \
    debug_a(x)                                                                 \
  }                                                                            \
  cerr << "\n";
#define debug_d(a)                                                             \
  for (auto x : a) {                                                           \
    debug_b(x)                                                                 \
  }                                                                            \
  cerr << "\n";
#define debug_e(a)                                                             \
  cerr << "[" << a.first << ", " << a.second << "]"                            \
       << "\n";

// Defines
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cout.tie(NULL);                                                              \
  cin.tie(NULL);
#define loop(i, a, n) for (int i = a; i < n; i++)
#define rloop(i, a, n) for (int i = a; i >= n; i--)
#define fr(i, a, n, b) for (int i = a; i < n; i += b)
#define rfr(i, a, n, b) for (int i = a; i >= n; i -= b)
#define IN cin >>
#define OUT cout <<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(), (a).end()
#define each(a, b) for (auto &a : b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a)                                                               \
  for (auto x : a) {                                                           \
    OUT x << " ";                                                              \
  }                                                                            \
  OUT nl;
#define read(a)                                                                \
  for (auto &x : a) {                                                          \
    IN x;                                                                      \
  }
// #define oset
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll = long long int;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vi = vector<int>;
const ll mod = (ll)(1e9) + 7LL;
const ll M = 988244353LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ld pi = acos(-1);

// General Functions
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll P(ll B, ll power, ll modulo) // Fast Power
{
  ll ans = 1LL;
  while (power > 0LL) {
    if (power % 2LL == 1LL) {
      ans = (ans * B) % modulo;
    }
    B = (B * B) % modulo;
    power /= 2LL;
  }
  return ans;
}
bool isPrime(ll n) {
  if (n <= 1LL) {
    return false;
  }
  if (n <= 3LL) {
    return true;
  }
  if (n % 2 == 0LL || n % 3 == 0LL) {
    return false;
  }
  for (ll i = 5LL; (i * i) <= n; i += 6LL) {
    if (n % i == 0LL || n % (i + 2LL) == 0LL) {
      return false;
    }
  }
  return true;
}
void vok() {
  fast
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}

// Global Variables
const int mxN = int(1e5) + 100;
set<int> *seg;

// Solver Function(s)
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx) {
    bit[idx] += val;
  }
}
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx) {
    sum += bit[idx];
  }
  return sum;
}
void run(int arr[], int n, vector<ar<int, 3>> v, int q) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));
  int last_visit[mxN];
  memset(last_visit, -1, sizeof(last_visit));
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {

    if (last_visit[arr[i]] != -1) {
      update(last_visit[arr[i]] + 1, -1, bit, n);
    }
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);
    while (query_counter < q && v[query_counter][0] == i) {
      ans[v[query_counter][2]] = query(v[query_counter][0] + 1, bit, n) -
                                 query(v[query_counter][1], bit, n);
      query_counter++;
    }
  }
  loop(i, 0, q) { OUT ans[i] << nl; }
}
void solve() {
  int n, q;
  IN n >> q;
  int v[n];
  loop(i, 0, n) { IN v[i]; }
  vector<ar<int, 3>> vv(q);
  loop(i, 0, q) {
    IN vv[i][1] >> vv[i][0];
    vv[i][2] = i;
    vv[i][0]--, vv[i][1]--;
  }
  sort(all(vv));
  run(v, n, vv, q);
}

// Main Function
int main() {
  vok();
  int t = 1;
  // IN t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
// Author: Vinay Khilwani
// Language: C++

// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB,
// CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank

// Never Stop Trying.
// Trying to be Better than Myself.

// while(true)
// {
//     if(AC)
//     {
//         break;
//     }
//     else if(Contest Over)
//     {
//         Try.
//         Check out Editorial.
//         Understand.
//         Find out your Mistake.
//         Learn the topic (if new).
//         Solve Problems on that topic (if new).
//         Upsolve that problem.
//         break;
//     }
//     else
//     {
//         Try.
//         Use Pen-Paper.
//         Find errors, edge cases, etc.
//         continue;
//     }
// }

// Optimizations
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")

// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

// Debugging
#define dbg(a) cerr << a << "\n";
#define debug_a(a)                                                             \
  for (auto x : a) {                                                           \
    cerr << x << " ";                                                          \
  }                                                                            \
  cerr << "\n";
#define debug_b(a)                                                             \
  for (auto x : a) {                                                           \
    cerr << "[" << x.first << ", " << x.second << "]"                          \
         << "\n";                                                              \
  }                                                                            \
  cerr << "\n";
#define debug_c(a)                                                             \
  for (auto x : a) {                                                           \
    debug_a(x)                                                                 \
  }                                                                            \
  cerr << "\n";
#define debug_d(a)                                                             \
  for (auto x : a) {                                                           \
    debug_b(x)                                                                 \
  }                                                                            \
  cerr << "\n";
#define debug_e(a)                                                             \
  cerr << "[" << a.first << ", " << a.second << "]"                            \
       << "\n";

// Defines
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cout.tie(NULL);                                                              \
  cin.tie(NULL);
#define loop(i, a, n) for (int i = a; i < n; i++)
#define rloop(i, a, n) for (int i = a; i >= n; i--)
#define fr(i, a, n, b) for (int i = a; i < n; i += b)
#define rfr(i, a, n, b) for (int i = a; i >= n; i -= b)
#define IN cin >>
#define OUT cout <<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(), (a).end()
#define each(a, b) for (auto &a : b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a)                                                               \
  for (auto x : a) {                                                           \
    OUT x << " ";                                                              \
  }                                                                            \
  OUT nl;
#define read(a)                                                                \
  for (auto &x : a) {                                                          \
    IN x;                                                                      \
  }
// #define oset
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll = long long int;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vi = vector<int>;
const ll mod = (ll)(1e9) + 7LL;
const ll M = 988244353LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ld pi = acos(-1);

// General Functions
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll P(ll B, ll power, ll modulo) // Fast Power
{
  ll ans = 1LL;
  while (power > 0LL) {
    if (power % 2LL == 1LL) {
      ans = (ans * B) % modulo;
    }
    B = (B * B) % modulo;
    power /= 2LL;
  }
  return ans;
}
bool isPrime(ll n) {
  if (n <= 1LL) {
    return false;
  }
  if (n <= 3LL) {
    return true;
  }
  if (n % 2 == 0LL || n % 3 == 0LL) {
    return false;
  }
  for (ll i = 5LL; (i * i) <= n; i += 6LL) {
    if (n % i == 0LL || n % (i + 2LL) == 0LL) {
      return false;
    }
  }
  return true;
}
void vok() {
  fast
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}

// Global Variables
const int mxN = int(5e5) + 100;
set<int> *seg;

// Solver Function(s)
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx) {
    bit[idx] += val;
  }
}
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx) {
    sum += bit[idx];
  }
  return sum;
}
void run(int arr[], int n, vector<ar<int, 3>> v, int q) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));
  int last_visit[mxN];
  memset(last_visit, -1, sizeof(last_visit));
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {

    if (last_visit[arr[i]] != -1) {
      update(last_visit[arr[i]] + 1, -1, bit, n);
    }
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);
    while (query_counter < q && v[query_counter][0] == i) {
      ans[v[query_counter][2]] = query(v[query_counter][0] + 1, bit, n) -
                                 query(v[query_counter][1], bit, n);
      query_counter++;
    }
  }
  loop(i, 0, q) { OUT ans[i] << nl; }
}
void solve() {
  int n, q;
  IN n >> q;
  int v[n];
  loop(i, 0, n) { IN v[i]; }
  vector<ar<int, 3>> vv(q);
  loop(i, 0, q) {
    IN vv[i][1] >> vv[i][0];
    vv[i][2] = i;
    vv[i][0]--, vv[i][1]--;
  }
  sort(all(vv));
  run(v, n, vv, q);
}

// Main Function
int main() {
  vok();
  int t = 1;
  // IN t;
  while (t--) {
    solve();
  }
  return 0;
}