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

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const int NINF = -1 * INF;
const long long LLINF = 1ll << 60;
const long long LLNINF = -1 * LLINF;

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<pi> vpi;
typedef vector<vector<int>> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef vector<vld> vvld;

#define mp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define revAll(x) x.rbegin(), x.rend()
#define gcd(a, b) __gcd(a, b)
#define popcount(x) __builtin_popcount(x)
#define isize(x) (int)(x.size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define umap unordered_map

const ll MOD = 1e9 + 7;

struct SegTree {
  int n;
  vvi T;

  SegTree(vi &B) {
    n = isize(B);
    T = vvi(4 * n + 10);
    build(B, 1, 0, n - 1);
  }

  void build(vi &B, int v, int tl, int tr) {
    if (tl == tr) {
      T[v] = {B[tl]};
    } else {
      int mid = (tl + tr) / 2;
      build(B, 2 * v, tl, mid);
      build(B, 2 * v + 1, mid + 1, tr);
      vi new_v = vi();
      merge(all(T[2 * v]), all(T[2 * v + 1]), back_inserter(new_v));
      T[v] = new_v;
    }
  }

  int qry(int l, int r, int x, int v, int tl, int tr) {
    if (l > tr or r < tl)
      return 0;
    if (l <= tl and tr <= r) {
      auto pt = lower_bound(all(T[v]), x + 1);
      return distance(pt, T[v].end());
    }
    int mid = (tl + tr) / 2;
    int L = qry(l, r, x, 2 * v, tl, mid);
    int R = qry(l, r, x, 2 * v + 1, mid + 1, tr);
    return L + R;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vi A = vi();
  rep(i, n) {
    int x;
    cin >> x;
    A.pb(x);
  }
  vi B = vi(n);
  umap<int, int> last = umap<int, int>();
  for (int i = isize(A) - 1; i >= 0; i--) {
    int val = A[i];
    if (last.find(val) == last.end()) {
      B[i] = INF;
    } else {
      B[i] = last[val];
    }
    last[val] = i;
  }
  SegTree ST = SegTree(B);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    int ans = ST.qry(l, r, r, 1, 0, isize(B) - 1);
    cout << ans << "\\n";
  }
}

Error: TLE

Fixed submission:

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const int NINF = -1 * INF;
const long long LLINF = 1ll << 60;
const long long LLNINF = -1 * LLINF;

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<pi> vpi;
typedef vector<vector<int>> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef vector<vld> vvld;

#define mp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define revAll(x) x.rbegin(), x.rend()
#define gcd(a, b) __gcd(a, b)
#define popcount(x) __builtin_popcount(x)
#define isize(x) (int)(x.size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define umap unordered_map

const ll MOD = 1e9 + 7;

struct SegTree {
  int n;
  vector<multiset<int>> T;

  SegTree(vi &B) {
    n = isize(B);
    T = vector<multiset<int>>(4 * n + 10);
    build(B, 1, 0, n - 1);
  }

  void build(vi &B, int v, int tl, int tr) {
    if (tl == tr) {
      T[v] = {B[tl]};
    } else {
      int mid = (tl + tr) / 2;
      build(B, 2 * v, tl, mid);
      build(B, 2 * v + 1, mid + 1, tr);
      multiset<int> new_set = {};
      new_set.insert(all(T[2 * v]));
      new_set.insert(all(T[2 * v + 1]));
      T[v] = new_set;
    }
  }

  int qry(int l, int r, int x, int v, int tl, int tr) {
    if (l > tr or r < tl)
      return 0;
    if (l <= tl and tr <= r) {
      auto pt = lower_bound(all(T[v]), x + 1);
      return distance(pt, T[v].end());
    }
    int mid = (tl + tr) / 2;
    int L = qry(l, r, x, 2 * v, tl, mid);
    int R = qry(l, r, x, 2 * v + 1, mid + 1, tr);
    return L + R;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vi A = vi();
  rep(i, n) {
    int x;
    cin >> x;
    A.pb(x);
  }
  vi B = vi(n);
  umap<int, int> last = umap<int, int>();
  for (int i = isize(A) - 1; i >= 0; i--) {
    int val = A[i];
    if (last.find(val) == last.end()) {
      B[i] = INF;
    } else {
      B[i] = last[val];
    }
    last[val] = i;
  }
  SegTree ST = SegTree(B);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    int ans = ST.qry(l, r, r, 1, 0, isize(B) - 1);
    cout << ans << "\\n";
  }
}