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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 500005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
int bit[N];
void update(int u, int k, int n) {
  for (int i = u; i <= n; i += (i & -i))
    bit[i] += k;
}
int getsum(int u) {
  int ans = 0;
  for (int i = u; i; i -= (i & -i))
    ans += bit[i];
  return ans;
}

void solve() {
  int n, q;
  cin >> n >> q;
  int c[n];
  rep(i, n) cin >> c[i];
  vector<pii> qry;
  int ans[q], l[q];
  memset(ans, 0, sizeof(ans));
  rep(i, q) {
    int l1, r;
    cin >> l1 >> r;
    // if(l>1)qry.pb(pii(l-2,i));
    l[i] = l1 - 1;
    qry.pb(pii(r - 1, i));
  }
  int last_visit[n + 1];
  memset(last_visit, -1, sizeof(last_visit));
  sort(qry.begin(), qry.end());
  int j = 0;
  for (int i = 0; i < n; i++) {
    if (last_visit[c[i]] != -1)
      update(last_visit[c[i]] + 1, -1, n);
    last_visit[c[i]] = i;
    update(i + 1, 1, n);
    while (j < qry.size() && i == qry[j].fi) {
      int id = qry[j].se;
      ans[id] = getsum(i + 1) - getsum(l[id]);
      // cout<<ans[id]<<" "<<id<<" "<<getsum(i+1)<<" "<<getsum(l[id])<<"\\n";
      j++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 500005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
void solve() {
  int n, q;
  cin >> n >> q;
  int sz = 500;
  int c[n + 1];
  set<int> s[n / sz + 5];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    s[i / sz].insert(c[i]);
  }
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    set<int> ltor;
    while (l % sz && l <= r)
      ltor.insert(c[l++]);
    while (r % sz != sz - 1 && l <= r)
      ltor.insert(c[r--]);
    if (l < r)
      for (int i = l / sz; i <= r / sz; i++) {
        for (int j : s[i])
          ltor.insert(j);
      }
    cout << ltor.size() << "\\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}