Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

void update(int idx, vector<int> &bit, int val) {

  while (idx <= 500005) {
    bit[idx] += val;
    idx += (idx & (-idx));
  }
  return;
}

ll query(vector<int> &bit, int idx) {
  ll s = 0;
  while (idx > 0) {
    //	cout<<idx<<"  ";
    s += bit[idx];
    idx -= (idx & (-idx));
  } // cout<<endl;
  return s;
}
void print(vector<int> &bit) {

  for (int i = 0; i <= 10; i++)
    cout << bit[i] << " ";
  cout << endl;
  return;
}

int main() {
  io;
  ll n, t;
  cin >> n >> t;
  vector<ll> v(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> v[i];
  }

  vector<pair<ll, pair<ll, ll>>> q(t);
  for (ll i = 0; i < t; i++) {
    cin >> q[i].ss.ff >> q[i].ff;
    q[i].ss.ss = i;
  }

  sort(q.begin(), q.end());
  vector<int> ans(t);
  vector<int> bit(500009), last(500009, -1);
  int counter = 0;

  for (ll i = 1; i <= n; i++) {
    if (last[v[i]] != -1) {
      update(last[v[i]], bit, -1);
    }
    last[v[i]] = i;
    update(last[v[i]], bit, 1);
    // print(bit);
    while (counter < t && q[counter].ff == i) {
      // ans[q[counter].ss.ss]
      ans[q[counter].ss.ss] = query(bit, i) - query(bit, q[counter].ss.ff - 1);
      // cout<<val<<" "<<q[counter].ss.ss<<endl;
      counter++;
    }
  }

  for (int i = 0; i < t; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF 2000000000
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i, a) for (typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define REP(p, a, b) for (int p = a; p < b; p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a, b) make_pair(a, b)
#define fst first
#define snd second
#define pii pair<int, int>
#define ins(a) insert(a)
#define N 500010
#define A 500010
#define BLOCK 725
#define M 500010

int answer;
struct query {
  int l, r, ind;
} Q[M];
int ans[M];
int a[N];
int cnt[A];

void add(int index) {
  cnt[a[index]]++;
  if (cnt[a[index]] == 1)
    answer++;
}

void remove(int index) {
  cnt[a[index]]--;
  if (cnt[a[index]] == 0)
    answer--;
}

bool cmp(query f, query s) {
  if ((f.l / BLOCK) != (s.l / BLOCK))
    return (f.l / BLOCK) < (s.l / BLOCK);
  else
    return f.r < s.r;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  REP(i, 0, n) {
    cin >> a[i];
    // scanf("%d",&a[i]);
  }

  REP(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    // scanf("%d%d",&Q[i].l,&Q[i].r);
    Q[i].ind = i;
    Q[i].l--;
    Q[i].r--;
  }
  sort(Q, Q + q, cmp);
  int cl = 0, cr = 0;
  REP(i, 0, q) {
    int left = Q[i].l, right = Q[i].r;
    while (cl < left)
      remove(cl), cl++;
    while (cl > left)
      add(cl - 1), cl--;
    while (cr <= right)
      add(cr), cr++;
    while (cr > (right + 1))
      remove(cr - 1), cr--;
    ans[Q[i].ind] = answer;
  }
  REP(i, 0, q)
  printf("%d\\n", ans[i]);
  return 0;
}