Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

template <typename T> struct BIT {
  int N;
  vector<T> dat;

  // [0, N], 1-indexing!!!
  explicit BIT(int n) : N(n), dat(N + 1, 0) {}

  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += dat[i];
      i -= i & -i; // i = i & (i-1)
    }

    return s;
  }

  void add(int i, T x) {
    while (i <= N) {
      dat[i] += x;
      i += i & -i;
    }
  }

  // min_x {x | v1 + v2 + ⋯ + vx ≧ w}
  // Assuming v_q >= 0
  int lower_bound(T w) {
    if (w <= 0)
      return 0;

    int x = 0;
    T k = 1;
    while (k * 2 <= N)
      k <<= 1;
    for (; k > 0; k /= 2) {
      if (x + k <= N && dat[x + k] < w) {
        w -= dat[x + k];
        x += k;
      }
    }
    return x + 1;
  }
};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N, Q;
  cin >> N >> Q;

  vll c(N);
  REP(i, N) cin >> c[i];

  vector<pair<pair<int, int>, int>> query;
  REP(i, Q) {
    int l, r;
    cin >> l >> r;
    query.pb({{r, l}, i});
  }

  sort(ALL(query));

  vector<ll> last(N, -1);

  BIT<ll> bit(N);

  int r_prev = 1;
  vector<pair<int, ll>> ans;
  REP(i, Q) {
    auto p = query[i];
    int l = p.fi.se, r = p.fi.fi;

    for (int j = r_prev; j <= r; ++j) {
      if (last[c[j - 1]] > -1)
        bit.add(last[c[j - 1]], -1);
      last[c[j - 1]] = j;
      bit.add(j, 1);
    }
    r_prev = r;

    ans.eb(p.se, bit.sum(r) - bit.sum(l - 1));
  }

  sort(ALL(ans));

  REP(i, ans.size()) { print(ans[i].se); }
}


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

template <typename T> struct BIT {
  int N;
  vector<T> dat;

  // [0, N], 1-indexing!!!
  explicit BIT(int n) : N(n), dat(N + 1, 0) {}

  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += dat[i];
      i -= i & -i; // i = i & (i-1)
    }

    return s;
  }

  void add(int i, T x) {
    while (i <= N) {
      dat[i] += x;
      i += i & -i;
    }
  }

  // min_x {x | v1 + v2 + ⋯ + vx ≧ w}
  // Assuming v_q >= 0
  int lower_bound(T w) {
    if (w <= 0)
      return 0;

    int x = 0;
    T k = 1;
    while (k * 2 <= N)
      k <<= 1;
    for (; k > 0; k /= 2) {
      if (x + k <= N && dat[x + k] < w) {
        w -= dat[x + k];
        x += k;
      }
    }
    return x + 1;
  }
};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N, Q;
  cin >> N >> Q;

  vll c(N);
  REP(i, N) cin >> c[i];

  vector<pair<pair<int, int>, int>> query;
  REP(i, Q) {
    int l, r;
    cin >> l >> r;
    query.pb({{r, l}, i});
  }

  sort(ALL(query));

  vector<ll> last(N, -1);

  BIT<ll> bit(N);

  int r_prev = 1;
  vector<pair<int, ll>> ans;
  REP(i, Q) {
    auto p = query[i];
    int l = p.fi.se, r = p.fi.fi;

    for (int j = r_prev; j <= r; ++j) {
      if (last[c[j - 1]] > 0)
        bit.add(last[c[j - 1]], -1);
      last[c[j - 1]] = j;
      bit.add(j, 1);
    }
    r_prev = r;

    ans.eb(p.se, bit.sum(r) - bit.sum(l - 1));
  }

  sort(ALL(ans));

  REP(i, ans.size()) { print(ans[i].se); }
}