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

#include <bits/stdc++.h>
// #define DEBUG
#define REP(i, nn) for (int i = 0; i < (int)nn; i++)
#define deb(x) std::cout << #x << " " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
template <typename... T> void read(T &...a) { ((cin >> a), ...); }
template <typename... T> void write(T... a) { ((cout << a << "\\n"), ...); }

template <typename... T> void write_space(T... a) { ((cout << a << " "), ...); }

constexpr int mxN = 5e5;
vector<int> appearance_index[mxN];

struct FenwickTree {
  vector<int> bit; // binary indexed tree
  int n;

  FenwickTree(int n) {
    this->n = n;
    bit.assign(n, 0);
  }

  FenwickTree(vector<int> a) : FenwickTree(a.size()) {
    for (size_t i = 0; i < a.size(); i++)
      add(i, a[i]);
  }

  int sum(int r) {
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      ret += bit[r];
    return ret;
  }

  int sum(int l, int r) { return sum(r) - sum(l - 1); }

  void add(int idx, int delta) {
    for (; idx < n; idx = idx | (idx + 1))
      bit[idx] += delta;
  }
};

void solve() {
  int n, q;
  read(n, q);
  vector<int> color(n);
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    c--;
    color[i] = c;
    appearance_index[c].push_back(i);
  }
  set<array<int, 3>> queries;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries.insert({r, l, i});
  }
  set<pair<int, int>> ans;
  vector<int> good_ball(n);
  for (int i = 0; i < n; i++) {
    if (appearance_index[i].size() > 0) {
      good_ball[appearance_index[i].back()] = 1;
    }
  }
  FenwickTree ft(good_ball);
  int current_right = n - 1;
  for (auto it = queries.rbegin(); it != queries.rend(); it++) {
    int r = (*it)[0];
    int l = (*it)[1];
    int query_index = (*it)[2];
    while (r < current_right) {
      // update good bool
      int current_color = color[current_right];
      auto it_temp =
          lower_bound(appearance_index[current_color].begin(),
                      appearance_index[current_color].end(), current_right);
      if (it_temp != appearance_index[current_color].begin()) {
        it_temp--;
        ft.add(*it_temp, 1);
      }
      current_right--;
    }
    int color_count;
    if (l > 0) {
      color_count = ft.sum(l, r);
    } else {
      color_count = ft.sum(r);
    }
    ans.insert(make_pair(query_index, color_count));
  }
  for (const auto &res : ans) {
    cout << res.second << "\\n";
  }
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG
#define REP(i, nn) for (int i = 0; i < (int)nn; i++)
#define deb(x) std::cout << #x << " " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
template <typename... T> void read(T &...a) { ((cin >> a), ...); }
template <typename... T> void write(T... a) { ((cout << a << "\\n"), ...); }

template <typename... T> void write_space(T... a) { ((cout << a << " "), ...); }

constexpr int mxN = 5e5;
set<pair<int, int>> color_track[mxN + 1];

void solve() {
  int n, q;
  read(n, q);
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    color_track[i] = color_track[i - 1];
    if (mp.find(c) == mp.end()) {
      mp[c] = i;
      color_track[i].insert(make_pair(i, c));
    } else {
      int j = mp[c];
      mp[c] = j;
      color_track[i].erase(make_pair(j, c));
      color_track[i].insert(make_pair(i, c));
    }
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    auto it = color_track[r].lower_bound(make_pair(l, 1));
    cout << distance(it, color_track[r].end()) << "\\n";
  }
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  solve();

  return 0;
}