Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int N;
int bit[500005]; // bit 1-index
map<int, int> amap;

void add(int i, int x) {
  int tmp;
  if (amap[x] == 0) {
    tmp = i;
    while (tmp <= N) {
      bit[tmp]++;
      tmp += tmp & -tmp;
    }
  } else {
    tmp = i;
    while (tmp <= N) {
      bit[tmp]++;
      tmp += tmp & -tmp;
    }
    tmp = amap[x];
    while (tmp <= N) {
      bit[tmp]--;
      tmp += tmp & -tmp;
    }
  }
  amap[x] = i;
}

int sum(int l) {
  int ans = 0;
  while (l > 0) {
    ans += bit[l];
    l -= l & -l;
  }
  return ans;
}

int main() {
  int Q;
  cin >> N >> Q;
  int c[N];
  for (int i = 0; i < N; ++i)
    cin >> c[i];
  vector<tuple<int, int, int>> query(Q);
  int l, r;
  for (int i = 0; i < Q; ++i) {
    cin >> l >> r;
    query[i] = make_tuple(r, l, i);
  }
  sort(query.begin(), query.end());
  int ans[Q];
  int index = 0;
  for (int i = 0; i < Q; ++i) {
    l = get<1>(query[i]);
    r = get<0>(query[i]);
    for (int j = index; j < r; ++j) {
      add(j + 1, c[j]);
      ans[get<2>(query[i])] = sum(r) - sum(l - 1);
    }
  }
  for (int i = 0; i < Q; ++i)
    cout << ans[i] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int N;
int bit[500005]; // bit 1-index
map<int, int> amap;

void add(int i, int x) {
  int tmp;
  if (amap[x] == 0) {
    tmp = i;
    while (tmp <= N) {
      bit[tmp]++;
      tmp += tmp & -tmp;
    }
  } else {
    tmp = i;
    while (tmp <= N) {
      bit[tmp]++;
      tmp += tmp & -tmp;
    }
    tmp = amap[x];
    while (tmp <= N) {
      bit[tmp]--;
      tmp += tmp & -tmp;
    }
  }
  amap[x] = i;
}

int sum(int l) {
  int ans = 0;
  while (l > 0) {
    ans += bit[l];
    l -= l & -l;
  }
  return ans;
}

int main() {
  int Q;
  cin >> N >> Q;
  int c[N];
  for (int i = 0; i < N; ++i)
    cin >> c[i];
  vector<tuple<int, int, int>> query(Q);
  int l, r;
  for (int i = 0; i < Q; ++i) {
    cin >> l >> r;
    query[i] = make_tuple(r, l, i);
  }
  sort(query.begin(), query.end());
  int ans[Q];
  int index = 0;
  for (int i = 0; i < Q; ++i) {
    l = get<1>(query[i]);
    r = get<0>(query[i]);
    for (int j = index; j < r; ++j) {
      add(j + 1, c[j]);
      ans[get<2>(query[i])] = sum(r) - sum(l - 1);
    }
    index = r - 1;
  }
  for (int i = 0; i < Q; ++i)
    cout << ans[i] << endl;
}