Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
ll mod = 1e9 + 7;
// const double pi = acos(-1.0);
/*
ll kmp(string x)
{
    ll n= x.length();
    //cout<<x<<endl;
    vector<ll> lcp(x.length(), 0);
    for(ll i=1;i<n;i++)
    {
        ll j=lcp[i-1];
        while(j>0 && x[j]!=x[i])
            j=lcp[j-1];
        if(x[i]==x[j])
            j++;
        lcp[i]=j;
    }
    return lcp[n-1];
}

int N = 1000002;
int ar[100002];
void sieve()
{
    for(int i=1;i<=N;i++)
        ar[i] = 1;
    ar[0] = 0;
    for(int i=2;i*i<=N;i++)
    {
        if(ar[i] && i*i<=N)
        {
            for(int j = i*i; j<=N;j+=i)
                ar[j] = 0;
        }
    }
}

ll modex(ll a,ll b){
    if(b<=0){
        return 1;
    }
    if(b%2==0)
    {
        ll x = modex(a, b/2);
        return ((x*x)%mod);
    }
    else
        return ((a%mod * modex(a, b-1))%mod);
}

ll c[1010][1010];

ll combina(ll i, ll j){
        for (int i=0;i<=1005;i++)
        for (int j=0;j<=i;j++)
        {
            if (j==i||j==0)
                c[i][j]=1;
            else
            c[i][j]=c[i-1][j-1]+c[i-1][j];
            c[i][j]%=mod;
        }
        return c[i][j];
}
void find_fac(int x){
    vector<ll> di;
    for(ll i = 2; i*i <= x; i++)
    {
        if(x%i==0){
            di.push_back(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x > 1)
        di.push_back(x);
}
*/

vector<int> ar; // Bit Array
vector<int> a;
int la[100001];

ll n;
struct node {
  int l, r, in;
};
vector<node> qu;
int comp(node &a, node &b) { return a.r < b.r; }
void update(ll x, ll v) {
  // cout<<x<<endl;
  // x += 1;
  while (x <= (n)) {
    // cout<<x<<endl;
    ar[x] += v;
    x += (x & (-x));
  }
}
ll query(ll x) {
  // x+=1;
  ll an = 0;
  while (x > 0) {
    an += ar[x];
    x = x & (x - 1);
  }
  return an;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cout.precision(12);
  ll q;
  cin >> n >> q;
  a.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  memset(la, -1, sizeof(la));
  ar.assign(n + 1, 0);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    qu.push_back({l, r, i});
  }
  sort(qu.begin(), qu.end(), comp);
  ll j = 0;
  /*for(ll i=0;i<q;i++)
      cout<<qu[i].l<<" "<<qu[i].r<<" "<<qu[i].in<<endl;*/
  vector<int> an(q);
  for (int i = 0; i < n; i++) {
    // cout<<a[i]<<" "<<la[a[i]]<<endl;
    if (la[a[i]] == -1) {
      la[a[i]] = i + 1;
      update(la[a[i]], 1);
    } else {
      update(la[a[i]], -1);
      la[a[i]] = i + 1;
      update(la[a[i]], 1);
    }
    while (j < q && qu[j].r == (i + 1)) {
      int l = qu[j].l, r = qu[j].r, in = qu[j].in;
      // cout<<in<<" "<<query(r)<<" "<<query(l-1)<<endl;
      an[in] = query(r) - query(l - 1);
      j++;
    }
  }
  for (ll i = 0; i < q; i++)
    cout << an[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
ll mod = 1e9 + 7;
// const double pi = acos(-1.0);
/*
ll kmp(string x)
{
    ll n= x.length();
    //cout<<x<<endl;
    vector<ll> lcp(x.length(), 0);
    for(ll i=1;i<n;i++)
    {
        ll j=lcp[i-1];
        while(j>0 && x[j]!=x[i])
            j=lcp[j-1];
        if(x[i]==x[j])
            j++;
        lcp[i]=j;
    }
    return lcp[n-1];
}

int N = 1000002;
int ar[100002];
void sieve()
{
    for(int i=1;i<=N;i++)
        ar[i] = 1;
    ar[0] = 0;
    for(int i=2;i*i<=N;i++)
    {
        if(ar[i] && i*i<=N)
        {
            for(int j = i*i; j<=N;j+=i)
                ar[j] = 0;
        }
    }
}

ll modex(ll a,ll b){
    if(b<=0){
        return 1;
    }
    if(b%2==0)
    {
        ll x = modex(a, b/2);
        return ((x*x)%mod);
    }
    else
        return ((a%mod * modex(a, b-1))%mod);
}

ll c[1010][1010];

ll combina(ll i, ll j){
        for (int i=0;i<=1005;i++)
        for (int j=0;j<=i;j++)
        {
            if (j==i||j==0)
                c[i][j]=1;
            else
            c[i][j]=c[i-1][j-1]+c[i-1][j];
            c[i][j]%=mod;
        }
        return c[i][j];
}
void find_fac(int x){
    vector<ll> di;
    for(ll i = 2; i*i <= x; i++)
    {
        if(x%i==0){
            di.push_back(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x > 1)
        di.push_back(x);
}
*/

vector<int> ar; // Bit Array
vector<int> a;
int la[500001];

ll n;
struct node {
  int l, r, in;
};
vector<node> qu;
int comp(node &a, node &b) { return a.r < b.r; }
void update(ll x, ll v) {
  // cout<<x<<endl;
  // x += 1;
  while (x <= (n)) {
    // cout<<x<<endl;
    ar[x] += v;
    x += (x & (-x));
  }
}
ll query(ll x) {
  // x+=1;
  ll an = 0;
  while (x > 0) {
    an += ar[x];
    x = x & (x - 1);
  }
  return an;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cout.precision(12);
  ll q;
  cin >> n >> q;
  a.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  memset(la, -1, sizeof(la));
  ar.assign(n + 1, 0);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    qu.push_back({l, r, i});
  }
  sort(qu.begin(), qu.end(), comp);
  ll j = 0;
  /*for(ll i=0;i<q;i++)
      cout<<qu[i].l<<" "<<qu[i].r<<" "<<qu[i].in<<endl;*/
  vector<int> an(q);
  for (int i = 0; i < n; i++) {
    // cout<<a[i]<<" "<<la[a[i]]<<endl;
    if (la[a[i]] == -1) {
      la[a[i]] = i + 1;
      update(la[a[i]], 1);
    } else {
      update(la[a[i]], -1);
      la[a[i]] = i + 1;
      update(la[a[i]], 1);
    }
    while (j < q && qu[j].r == (i + 1)) {
      int l = qu[j].l, r = qu[j].r, in = qu[j].in;
      // cout<<in<<" "<<query(r)<<" "<<query(l-1)<<endl;
      an[in] = query(r) - query(l - 1);
      j++;
    }
  }
  for (ll i = 0; i < q; i++)
    cout << an[i] << endl;
  return 0;
}