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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> class segtree {
  int n;
  vector<T> data;
  T id = 0;
  T operation(T a, T b) { return a + b; };

public:
  segtree(int _n) {
    n = 1;
    while (n < _n + 2)
      n <<= 1;
    data = vector<T>(2 * n, id);
  }
  void change(int i, T x) {
    i += n;
    data[i] = x;
    while (i > 1) {
      i >>= 1;
      data[i] = operation(data[i << 1], data[i << 1 | 1]);
    }
  }
  void add(int i, T x) { change(i, data[i + n] + x); }
  T get(int a, int b) {
    T left = id;
    T right = id;
    a += n;
    b += n;
    while (a < b) {
      if (a & 1)
        left = operation(left, data[a++]);
      if (b & 1)
        right = operation(data[--b], right);
      a >>= 1;
      b >>= 1;
    }
    return operation(left, right);
  }
  T get_all() { return data[1]; }
  T operator[](int i) { return data[i + n]; }
};

bool comp(pair<P, int> a, pair<P, int> b) {
  return a.first.second < b.first.second;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<P, int>> p(q);
  rep(i, q) {
    cin >> p[i].first.first >> p[i].first.second;
    p[i].first.first--;
    p[i].first.second--;
    p[i].second = i;
  }
  sort(all(p), comp);
  vector<int> lst(n, -1);
  segtree<int> seg(n);
  vector<P> ans(q);
  int prev = -1;
  rep(i, q) {
    int pf = p[i].first.first;
    int ps = p[i].first.second;
    for (int j = prev + 1; j <= ps; j++) {
      if (lst[a[j]] != -1)
        seg.add(lst[a[j]], -1);
      seg.add(j, 1);
      lst[a[j]] = j;
    }
    ans[i].second = seg.get(pf, ps + 1);
    prev = ps;
  }
  rep(i, q) ans[i].first = p[i].second;
  sort(all(ans));
  for (auto i : ans)
    cout << i.second << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> class segtree {
  int n;
  vector<T> data;
  T id = 0;
  T operation(T a, T b) { return a + b; };

public:
  segtree(int _n) {
    n = 1;
    while (n < _n + 2)
      n <<= 1;
    data = vector<T>(2 * n, id);
  }
  void change(int i, T x) {
    i += n;
    data[i] = x;
    while (i > 1) {
      i >>= 1;
      data[i] = operation(data[i << 1], data[i << 1 | 1]);
    }
  }
  void add(int i, T x) { change(i, data[i + n] + x); }
  T get(int a, int b) {
    T left = id;
    T right = id;
    a += n;
    b += n;
    while (a < b) {
      if (a & 1)
        left = operation(left, data[a++]);
      if (b & 1)
        right = operation(data[--b], right);
      a >>= 1;
      b >>= 1;
    }
    return operation(left, right);
  }
  T get_all() { return data[1]; }
  T operator[](int i) { return data[i + n]; }
};

bool comp(pair<P, int> a, pair<P, int> b) {
  return a.first.second < b.first.second;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<P, int>> p(q);
  rep(i, q) {
    cin >> p[i].first.first >> p[i].first.second;
    p[i].first.first--;
    p[i].first.second--;
    p[i].second = i;
  }
  sort(all(p), comp);
  vector<int> lst(n, -1);
  segtree<int> seg(n);
  vector<P> ans(q);
  int prev = -1;
  rep(i, q) {
    int pf = p[i].first.first;
    int ps = p[i].first.second;
    for (int j = prev + 1; j <= ps; j++) {
      if (lst[a[j]] != -1)
        seg.add(lst[a[j]], -1);
      seg.add(j, 1);
      lst[a[j]] = j;
    }
    ans[i].second = seg.get(pf, ps + 1);
  }
  rep(i, q) ans[i].first = p[i].second;
  sort(all(ans));
  for (auto i : ans)
    cout << i.second << '\\n';
}