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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template <class Abel> struct BIT {
  vector<Abel> dat[2];
  Abel UNITY_SUM = 0; // to be set

  /* [1, n] */
  BIT(int n) { init(n); }
  void init(int n) {
    for (int iter = 0; iter < 2; ++iter)
      dat[iter].assign(n + 1, UNITY_SUM);
  }

  /* a, b are 1-indexed, [a, b) */
  inline void sub_add(int p, int a, Abel x) {
    for (int i = a; i < (int)dat[p].size(); i += i & -i)
      dat[p][i] = dat[p][i] + x;
  }
  inline void add(int a, int b, Abel x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  /* a is 1-indexed, [a, b) */
  inline Abel sub_sum(int p, int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[p][i];
    return res;
  }
  inline Abel sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat[0].size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int N;
  cin >> N;
  int Q;
  cin >> Q;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];

  vector<int> lefts(Q), rights(Q), ids(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> lefts[i] >> rights[i];
    --lefts[i];
    ids[i] = i;
  }
  sort(ids.begin(), ids.end(),
       [&](int i, int j) { return rights[i] < rights[j]; });

  BIT<int> bit(N + 5);
  vector<int> prev(1100000, -1);
  vector<int> res(Q, 0);
  int r = 0;
  for (auto i : ids) {
    for (; r < rights[i]; ++r) {
      bit.add(prev[a[r]] + 2, r + 2, 1);
      prev[a[r]] = r;
    }
    int tmp = bit.sum(lefts[i] + 1, lefts[i] + 2);
    res[i] = max(res[i], tmp);
  }
  for (int i = 0; i < Q; ++i)
    cout << res[i] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b)
    a = b;
}

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()

lint cnt[500001];
lint ans[500001];
lint a[500001];
lint n, q;
int sum = 0; // 区間内の種類数

/* do your best */
void add(int idx) {
  if (cnt[a[idx]] == 0)
    sum++;
  cnt[a[idx]]++;
};
void del(int idx) {
  if (cnt[a[idx]] == 1)
    sum--;
  cnt[a[idx]]--;
  assert(cnt[a[idx]] >= 0);
};
void rem(int idx) { ans[idx] = sum; };

struct Mo {

  int width;
  vector<int> left, right, order;
  vector<bool> v;

  Mo(int n, int q) : width((int)sqrt(n)), order(q), v(n) {
    iota(begin(order), end(order), 0);
  }

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

  void run() {
    assert(left.size() == order.size());
    sort(begin(order), end(order), [&](int a, int b) {
      int ablock = left[a] / width, bblock = left[b] / width;
      if (ablock != bblock)
        return ablock < bblock;
      if (ablock & 1)
        return right[a] < right[b];
      return right[a] > right[b];
    });
    int nl = 0, nr = 0;
    auto push = [&](int idx) {
      v[idx].flip();
      if (v[idx])
        add(idx);
      else
        del(idx);
    };
    for (auto idx : order) {
      while (nl > left[idx])
        push(--nl);
      while (nr < right[idx])
        push(nr++);
      while (nl < left[idx])
        push(nl++);
      while (nr > right[idx])
        push(--nr);
      rem(idx);
    }
  }
};

// 区間 [l, r) の値の種類数を求めよ
int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  cin >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  Mo mo(n, q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    mo.add_query(l, r);
  }

  mo.run();
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}