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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e18 + 10;
const double PI = acos(-1);

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};

struct BIT { // sample add
  vector<int> data;
  int nodes;

  BIT(int N) {
    nodes = N;
    data.resize(nodes * 2 + 1);
    for (auto i : data)
      i = 0; // initialize;
  }

  void add(int k, int a) { // add a to element k
    k++;
    int tmp = 0;
    while (k <= nodes) {
      data[k] += a;
      k += k & -k;
      // cout<<k<<endl;
      if (tmp > 10)
        break;
    }
  }

  ll sum(int i) { // calc sum of [0,i)
    i++;
    ll ret = 0ll;
    while (i > 0) {
      ret += data[i];
      i -= i & -i;
    }
    return ret;
  }
};

void solve(int cn) {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; ++i) {
    cin >> c[i];
    c[i]--;
  }
  vector<pair<pair<int, int>, int>> q(Q);
  int l, r;
  for (int i = 0; i < Q; ++i) {
    cin >> l >> r;
    --l;
    --r;
    q[i] = make_pair(make_pair(r, l), i);
  }
  sort(q.begin(), q.end());
  vector<int> ans(Q), right(N, -1);
  BIT bit = BIT(N);
  int idx = 0;
  for (int i = 0; i < Q; ++i) {
    while (idx <= q[i].first.first) {
      int color = c[idx];
      if (right[color] >= 0) {
        bit.add(right[color], -1);
      }
      right[color] = idx;
      bit.add(right[color], 1);
      idx++;
    }
    ans[q[i].second] =
        bit.sum(q[i].first.first) - bit.sum(q[i].first.second - 1);
  }

  for (auto i : ans)
    cout << i << endl;
}

#define SINGLE
int main() {
#ifdef SINGLE
  solve(1);
#else
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    solve(i);
  }
#endif
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e18 + 10;
const double PI = acos(-1);

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};

int N, A[500001], Q;
int ans[500001];
int cnt[500001], sum;

struct Mo {
  vector<int> left, right, order;
  vector<bool> v;
  int width;
  int nl, nr, ptr;

  explicit Mo(int n) : v(n), width((int)sqrt(n)), nl(0), nr(0), ptr(0) {}

  void insert(int l, int r) /* [l, r) */
  {
    left.push_back(l);
    right.push_back(r);
  }

  /* ソート */
  void build() {
    order.resize(left.size());
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int a, int b) {
      if (left[a] / width != left[b] / width)
        return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process() {
    if (ptr == order.size())
      return (-1);
    const auto id = order[ptr];
    while (nl > left[id]) {
      --nl;
      distribute(nl);
    }
    while (nr < right[id]) {
      distribute(nr);
      nr++;
    }
    while (nl < left[id]) {
      distribute(nl);
      nl++;
    }
    while (nr > right[id]) {
      --nr;
      distribute(nr);
    }
    // cout<<order[ptr]<<":"<<sum<<endl;
    int ret = order[ptr];
    ptr++;
    return ret;
    // return (order[ptr++]);
  }

  void distribute(int idx) {
    v[idx].flip();
    // v[idx]= !v[idx];
    if (v[idx])
      add(idx);
    else
      del(idx);
  }

  static void add(int idx);

  static void del(int idx);
};

void Mo::add(int idx) {
  if (cnt[A[idx]]++ == 0)
    ++sum;
}

void Mo::del(int idx) {
  if (--cnt[A[idx]] == 0)
    --sum;
}

void solve(int cn) {
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
  }
  Mo mo(N);

  for (int i = 0; i < Q; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    mo.insert(--a, b);
  }
  mo.build();
  for (int i = 0; i < Q; i++) {
    int val = mo.process();
    ans[val] = sum;
    // ans[mo.process()] = sum;
  }
  for (int i = 0; i < Q; i++) {
    printf("%d\\n", ans[i]);
  }
}

#define SINGLE
int main() {
#ifdef SINGLE
  solve(1);
#else
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    solve(i);
  }
#endif
  return 0;
}