Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define Freed freopen("0in.txt", "r", stdin);
#define Fout freopen("0out.txt", "w", stdout);
#define ll long long
#define pb push_back
#define mp make_pair
#define Mod 1000000007
#define limit 200008
using namespace std;
int n, cnt[limit], ans[limit], List[limit];
struct str {
  int l, r, ind;
};
bool cmp(str a, str b) { return a.r < b.r; }
str query[limit];
void up(int ind, int key) {
  while (ind <= n) {
    cnt[ind] += key;
    ind += (ind & -ind);
  }
}
int qu(int ind) {
  int sum = 0;
  while (ind > 0) {
    sum += cnt[ind];
    ind -= (ind & -ind);
  }
  return sum;
}
void solve(int t) {
  int i, j, q, m, k, l, r; /// Binary index tree
  for (i = 0; i < limit; i++) {
    List[i] = -1;
    cnt[i] = 0;
    ans[i] = 0;
  }
  cin >> n >> q;
  int D[n + 5];
  for (i = 1; i <= n; i++)
    cin >> D[i];
  for (i = 1; i <= q; i++) {
    cin >> l >> r;
    query[i].l = l;
    query[i].r = r;
    query[i].ind = i;
  }
  sort(query + 1, query + q + 1, cmp);
  k = 1;
  for (i = 1; i <= n; i++) {
    if (List[D[i]] != -1) {
      up(List[D[i]], -1);
    }
    List[D[i]] = i;
    up(i, 1);

    while (k <= q &&
           query[k].r == i) { // cout <<k<<" "<<qu(query[k].r)<<"
                              // "<<qu(query[k].l-1)<<" "<<query[k].ind<<endl;
      ans[query[k].ind] = qu(query[k].r) - qu(query[k].l - 1);
      k++;
    }
  }
  for (i = 1; i <= q; i++)
    cout << ans[i] << endl;
}
int main() {
  //    Fast
  //    Freed
  //    Fout
  int tt = 1, t;
  //    cin >> tt;
  for (t = 1; t <= tt; t++)
    solve(t);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define Freed freopen("0in.txt", "r", stdin);
#define Fout freopen("0out.txt", "w", stdout);
#define ll long long
#define pb push_back
#define mp make_pair
#define Mod 1000000007
#define limit 500008
using namespace std;
int n, cnt[limit], ans[limit], List[limit];
struct str {
  int l, r, ind;
};
bool cmp(str a, str b) { return a.r < b.r; }
str query[limit];
void up(int ind, int key) {
  while (ind <= n) {
    cnt[ind] += key;
    ind += (ind & -ind);
  }
}
int qu(int ind) {
  int sum = 0;
  while (ind > 0) {
    sum += cnt[ind];
    ind -= (ind & -ind);
  }
  return sum;
}
void solve(int t) {
  int i, j, q, m, k, l, r; /// Binary index tree
  for (i = 0; i < limit; i++) {
    List[i] = -1;
    cnt[i] = 0;
    ans[i] = 0;
  }
  cin >> n >> q;
  int D[n + 5];
  for (i = 1; i <= n; i++)
    cin >> D[i];
  for (i = 1; i <= q; i++) {
    cin >> l >> r;
    query[i].l = l;
    query[i].r = r;
    query[i].ind = i;
  }
  sort(query + 1, query + q + 1, cmp);
  k = 1;
  for (i = 1; i <= n; i++) {
    if (List[D[i]] != -1) {
      up(List[D[i]], -1);
    }
    List[D[i]] = i;
    up(i, 1);

    while (k <= q &&
           query[k].r == i) { // cout <<k<<" "<<qu(query[k].r)<<"
                              // "<<qu(query[k].l-1)<<" "<<query[k].ind<<endl;
      ans[query[k].ind] = qu(query[k].r) - qu(query[k].l - 1);
      k++;
    }
  }
  for (i = 1; i <= q; i++)
    cout << ans[i] << endl;
}
int main() {
  //    Fast
  //    Freed
  //    Fout
  int tt = 1, t;
  //    cin >> tt;
  for (t = 1; t <= tt; t++)
    solve(t);
  return 0;
}