Problem p02599 (C++) — buggy submission, verdict: 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 big = 1e18;
const double PI = 2 * asin(1);

int N;
int bit[500005];
map<int, int> amap;

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

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

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


Error: TLE

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 big = 1e18;
const double PI = 2 * asin(1);

vector<vector<int>> segtree((1 << 20));

void add(int ind, int c) {
  ind += (1 << 19) - 1;
  bool ok;
  while (true) {
    ok = true;
    for (int i = 0; i < segtree[ind].size(); ++i) {
      if (segtree[ind][i] == c)
        ok = false;
    }
    if (ok) {
      segtree[ind].push_back(c);
    } else
      break;
    if (ind == 0)
      break;
    ind = (ind - 1) / 2;
  }
}

int get(int l, int r) {
  l += (1 << 19) - 1;
  r += (1 << 19) - 1;
  map<int, int> amap;
  vector<int> tmparr;
  while (l <= r) {
    if (l == r) {
      for (int i = 0; i < segtree[l].size(); ++i) {
        if (amap[segtree[l][i]] == 0)
          tmparr.push_back(segtree[l][i]);
        amap[segtree[l][i]]++;
      }
      break;
    }
    if (l % 2 == 0) {
      for (int i = 0; i < segtree[l].size(); ++i) {
        if (amap[segtree[l][i]] == 0)
          tmparr.push_back(segtree[l][i]);
        amap[segtree[l][i]]++;
      }
      l++;
    }
    l = (l - 1) / 2;
    if (r % 2 == 1) {
      for (int i = 0; i < segtree[r].size(); ++i) {
        if (amap[segtree[r][i]] == 0)
          tmparr.push_back(segtree[r][i]);
        amap[segtree[r][i]]++;
      }
      r--;
    }
    r = (r - 1) / 2;
  }
  return tmparr.size();
}

int main() {
  int N, Q;
  cin >> N >> Q;
  int c;
  for (int i = 0; i < N; ++i) {
    cin >> c;
    add(i, c);
  }
  int l, r;
  for (int i = 0; i < Q; ++i) {
    cin >> l >> r;
    l--;
    r--;
    cout << get(l, r) << endl;
  }
}