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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int n, c[maxn], last[maxn];
vector<pair<int, int>> queries[maxn];
int sum[maxn];
void add(int pos, int val) {
  while (pos <= n) {
    sum[pos] += val;
    pos += pos & (-pos);
  }
  return;
}
int query(int pos) {
  int res = 0;
  while (pos) {
    res += sum[pos];
    pos -= pos & (-pos);
  }
  return res;
}
int ans[maxn];
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= m; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    queries[r].push_back(make_pair(l, i));
  }
  memset(last, -1, sizeof(last));
  for (int i = 1; i <= n; i++) {
    if (~last[c[i]])
      add(last[c[i]], -1);
    last[c[i]] = i;
    add(i, 1);
    for (int j = 0; j < (int)queries[i].size(); j++) {
      int s = queries[i][j].first;
      ans[queries[i][j].second] = query(i) - query(s - 1);
    }
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
/*
4 3
1 2 1 3
1 3
2 4
3 3

10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8
 */


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;

using point = complex<double>;
using ii = pair<int, ll>;

void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

const int dx[] = {0, -1, 1, 0, 1, -1, 1, -1};
const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

const double EPS = 1e-9;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const int N = 5e5 + 5;
const ll CONSTANT = 9e18 + 7;

int n, q;
int a[N];
set<int> Tree[N << 4];
void build(int p, int l, int r) {

  if (l == r) {
    Tree[p].insert(a[l]);
    return;
  }
  int m = l + r >> 1;
  build(p << 1, l, m);
  build(p << 1 | 1, m + 1, r);
  set_union(all(Tree[p << 1]), all(Tree[p << 1 | 1]),
            inserter(Tree[p], Tree[p].begin()));
}
set<int> query(int p, int l, int r, int ql, int qr) {
  if (l > qr || r < ql)
    return set<int>();
  if (l >= ql and r <= qr)
    return Tree[p];
  int m = l + r >> 1;
  set<int> x, y;
  x = query(p << 1, l, m, ql, qr);
  y = query(p << 1 | 1, m + 1, r, ql, qr);
  set<int> ret;
  set_union(all(x), all(y), inserter(ret, ret.begin()));
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0); // TLE
  cin >> n >> q;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  build(1, 0, n - 1);
  while (q--) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    cout << query(1, 0, n - 1, x, y).size() << '\\n';
  }

  return 0;
}