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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

#define bug(v) cerr << #v << " = " << (v) << endl;
#define bug2(v1, v2)                                                           \\
  cerr << #v1 << " = " << (v1) << ' ' << #v2 << " = " << (v2) << endl;
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue<int>
#define khtm "\\n"
#define F first
#define S second
#define fr(i, a, n) for (i = a; i < n; i++)
#define mem(arr, l) memset(arr, l, sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i, a, n) for (i = n - 1; i >= a; i--)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define testcase                                                               \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)

const double pi = 3.14159265359;
const int MAX = 1e6 + 2;
const ll inf = 1e18;
const int N = 1010;
const ll mod = 1000000007;
inline ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
inline ll mul(ll a, ll b) {
  return (((a + mod) % mod) * (b + mod) % mod) % mod;
}
inline ll power(ll a, ll b) {
  ll rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}
inline ll modInverse(ll n, ll p) { return power(n, p - 2) % p; }

void stArt() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
void eNd() {
#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif
}

ll i, j, k, l, m, r, n;
ll a[MAX], b[MAX];
ll q, lst[MAX], ans[MAX];

struct bit {
  vll BIT;
  bit(int sz) {
    n = sz + 4;
    BIT = vector<ll>(n + 1);
  }
  void update(ll x, ll val) {
    x++;
    for (; x <= n; x += x & -x) {
      BIT[x] += val;
    }
  }
  ll query(ll x) {
    x++;
    ll sum = 0;
    for (; x > 0; x -= x & -x)
      sum += BIT[x];
    return sum;
  }

  ll query(ll l, ll r) {
    if (l > r)
      return 0;
    ll ret = query(r) - (l <= 0 ? 0 : query(l - 1));
    return ret;
  }
};

vpll Q[MAX];

int main() {

  // stArt();
  fast;
  cin >> n >> q;
  fr(i, 1, 1 + n) cin >> a[i];
  fr(i, 0, q) {
    cin >> l >> r;
    Q[r].pb(l, i);
  }
  bit A(n);
  fr(i, 1, 1 + n) {
    A.update(i, 1);
    if (lst[a[i]])
      A.update(lst[a[i]], -1);
    lst[a[i]] = i;
    for (auto it : Q[i]) {
      ans[it.S] = A.query(i) - A.query(it.F - 1);
    }
  }
  fr(i, 0, q) cout << ans[i] << khtm;
  eNd();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define tr(c, it) for (auto it = c.begin(); it != c.end(); it++)
#define cpresent(c, e) (find(all(c), e) != c.end())
#define present(c, e) (c.find(e) != c.end())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;

struct item {
  unordered_set<int> s;
};

struct segtree {
  int size;
  vector<item> values;

  item NEUTRAL_ELEMENT = {};

  item merge(item a, item b) {
    unordered_set<int> s;
    s.insert(all(a.s));
    s.insert(all(b.s));
    return {s};
  }

  item single(int v) {
    unordered_set<int> s = {v};
    return {s};
  }

  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;
    values.resize(2 * size);
  }

  void build(vector<int> &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < sz(a)) {
        values[x] = single(a[lx]);
      }
      return;
    }
    int m = (lx + rx) / 2;
    build(a, 2 * x + 1, lx, m);
    build(a, 2 * x + 2, m, rx);
    values[x] = merge(values[2 * x + 1], values[2 * x + 2]);
  }

  void build(vector<int> &a) { build(a, 0, 0, size); }

  void setv(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      values[x] = single(v);
      return;
    }
    int m = (lx + rx) / 2;
    if (i < m) {
      setv(i, v, 2 * x + 1, lx, m);
    } else {
      setv(i, v, 2 * x + 2, m, rx);
    }
    values[x] = merge(values[2 * x + 1], values[2 * x + 2]);
  }

  void setv(int i, int v) { setv(i, v, 0, 0, size); }

  item calc(int l, int r, int x, int lx, int rx) {
    if (lx >= r || l >= rx)
      return NEUTRAL_ELEMENT;
    if (lx >= l && rx <= r)
      return values[x];
    int m = (lx + rx) / 2;
    item s1 = calc(l, r, 2 * x + 1, lx, m);
    item s2 = calc(l, r, 2 * x + 2, m, rx);
    return merge(s1, s2);
  }

  item calc(int l, int r) { return calc(l, r, 0, 0, size); }
};
int main() {
  int n, q;
  cin >> n >> q;
  vint a(n);
  fr(i, 0, n) { cin >> a[i]; }
  segtree st;
  st.init(n);
  st.build(a);
  fr(i, 0, q) {
    int l, r;
    cin >> l >> r;
    l--;
    cout << st.calc(l, r).s.size() << "\\n";
  }
  return 0;
}