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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <climits>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define ii pair<int, int>
#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ms(x, a) memset(x, (int)(a), sizeof(x))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define vi vector<int>
#define pb push_back
#define endl '\\n'
#define ff first
#define ss second
const int N = 5e5 + 7;
// const int INF = 1e18 ;
// const int MOD = 1e9 + 7 ;
int c[N];
int ft[N];
int ans[N];
vector<array<int, 2>> v[N];
int n, q;

void upd(int i, int x) {
  for (++i; i <= n; i += i & -i)
    ft[i] += x;
}
int get(int i) {
  // answer of [0,i)
  int sm = 0;
  for (; i; i -= i & -i)
    sm += ft[i];
  return sm;
}
void solve() {
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    v[r].pb({l, i});
  }
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    if (mp.find(c[i]) != mp.end())
      upd(mp[c[i]], -1);
    upd(i, 1);
    for (auto x : v[i]) {
      ans[x[1]] = get(n) - get(x[0]);
    }
    mp[c[i]] = i;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

int32_t main() {
  FAST_IO;
  int t = 1;
  //    cin >> t ;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <climits>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define ii pair<int, int>
#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ms(x, a) memset(x, (int)(a), sizeof(x))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define vi vector<int>
#define pb push_back
#define endl '\\n'
#define ff first
#define ss second
const int N = 5e5 + 7;
// const int INF = 1e18 ;
// const int MOD = 1e9 + 7 ;
using node = set<int>;
node st[4 * N];
int c[N];

node merge(node L, node R) {
  set<int> m;
  merge(all(L), all(R), inserter(m, m.end()));
  return m;
}
node construct(int n, int ll, int rl) {
  if (ll == rl) {
    st[n] = {c[ll]};
    return st[n];
  }
  int m = (ll + rl) / 2;
  node L = construct(2 * n + 1, ll, m);
  node R = construct(2 * n + 2, m + 1, rl);
  return st[n] = merge(L, R);
}

node query(int n, int ll, int rl, int x, int y) {
  int m = (ll + rl) / 2;
  if (ll == x && rl == y)
    return st[n];
  else if (y <= m)
    return query(2 * n + 1, ll, m, x, y);
  else if (x > m)
    return query(2 * n + 2, m + 1, rl, x, y);
  else {
    node L = query(2 * n + 1, ll, m, x, m);
    node R = query(2 * n + 2, m + 1, rl, m + 1, y);
    return merge(L, R);
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  construct(0, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    cout << sz(query(0, 0, n - 1, l, r)) << endl;
  }
}

int32_t main() {
  FAST_IO;
  int t = 1;
  //    cin >> t ;
  while (t--) {
    solve();
  }
}