Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T, class U> istream &operator>>(istream &is, P<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> line(n);
  cin >> line;
  vector<int> from_list(n);
  {
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
      if (mp.count(line[i]) == 0) {
        from_list[i] = -1;
      } else {
        from_list[i] = mp[line[i]];
      }
      mp[line[i]] = i;
    }
  }
  vector<P<int, int>> queries(q);
  cin >> queries;
  for (auto &e : queries) {
    e.first--;
    e.second--;
  }
  static vector<P<int, int>> r_base[500000];
  for (int i = 0; i < q; ++i)
    r_base[queries[i].second].emplace_back(queries[i].first, i);
  vector<int> vec;
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    // debug(i);
    auto it = upper_bound(vec.begin(), vec.end(), from_list[i]);
    if (it == vec.end() || *it != from_list[i])
      vec.insert(it, from_list[i]);
    for (auto e : r_base[i]) {
      ans[e.second] =
          i - e.first + 1 -
          (vec.end() - lower_bound(vec.begin(), vec.end(), e.first));
      // debug(e.first);
      // debug(e.second);
      // debug(distance(st.lower_bound(e.first),st.end()));
    }
  }
  for (auto e : ans)
    cout << e << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T, class U> istream &operator>>(istream &is, P<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> line(n);
  cin >> line;
  vector<int> from_list(n);
  {
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
      if (mp.count(line[i]) == 0) {
        from_list[i] = -1;
      } else {
        from_list[i] = mp[line[i]];
      }
      mp[line[i]] = i;
    }
  }
  vector<P<int, int>> queries(q);
  cin >> queries;
  for (auto &e : queries) {
    e.first--;
    e.second--;
  }
  static vector<P<int, int>> r_base[500000];
  for (int i = 0; i < q; ++i)
    r_base[queries[i].second].emplace_back(queries[i].first, i);
  vector<int> vec;
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    // debug(i);
    auto it = lower_bound(vec.begin(), vec.end(), from_list[i]);
    if (it == vec.end() || *it != from_list[i])
      vec.insert(it, from_list[i]);
    for (auto e : r_base[i]) {
      ans[e.second] =
          i - e.first + 1 -
          (vec.end() - lower_bound(vec.begin(), vec.end(), e.first));
      // debug(e.first);
      // debug(e.second);
      // debug(distance(st.lower_bound(e.first),st.end()));
    }
  }
  for (auto e : ans)
    cout << e << endl;
}