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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct Query {
  int i;
  int l, r;
};
bool operator<(const Query &a, const Query &b) { return a.r < b.r; }

struct FenwickTree {
  int n;
  vector<int> tree;

public:
  FenwickTree(int n_) : tree((1 << 21) + 1, 0) { n = n_; }
  void add(int i, int value) {
    for (; i < n; i |= i + 1)
      tree[i] += value;
  }
  int sum(int i) {
    int s = 0;
    for (; i >= 0; i = (i & i + 1) - 1)
      s += tree[i];
    return s;
  }
};
int lst[500005];

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> v;

  rep(i, N) {
    int a;
    cin >> a;
    v.push_back(a);
  }

  rep(i, 500005) lst[i] = -1;

  vector<Query> w;
  rep(q, Q) {
    Query query;
    query.i = q;
    cin >> query.l >> query.r;
    query.l--;
    query.r--;
    w.push_back(query);
  }
  sort(ALLOF(w));

  vector<int> ret(Q, -1);
  FenwickTree ft(N + 5);
  int L = -1, R = -1;
  rep(i, Q) {
    Query query = w[i];
    if (R < query.r) {
      REP(j, R + 1, query.r + 1) {
        if (lst[v[j]] != -1) {
          ft.add(lst[v[j]], -1);
        }
        lst[v[j]] = j;
        ft.add(j, 1);
      }
      R = query.r;
    }
    ret[query.i] = ft.sum(query.r) - ft.sum(query.l - 1);
  }

  rep(i, ret.size()) { cout << ret[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct Query {
  int i;
  int l, r;
};
bool operator<(const Query &a, const Query &b) { return a.r < b.r; }

struct FenwickTree {
  int n;
  vector<int> tree;

public:
  FenwickTree(int n_) : tree((1 << 21) + 1, 0) { n = n_; }
  void add(int i, int value) {
    for (; i < n; i |= i + 1)
      tree[i] += value;
  }
  int sum(int i) {
    int s = 0;
    for (; i >= 0; i = (i & i + 1) - 1)
      s += tree[i];
    return s;
  }
};
int lst[500005];

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> v;

  rep(i, N) {
    int a;
    cin >> a;
    v.push_back(a);
  }

  rep(i, 500005) lst[i] = -1;

  vector<Query> w;
  rep(q, Q) {
    Query query;
    query.i = q;
    cin >> query.l >> query.r;
    query.l--;
    query.r--;
    w.push_back(query);
  }
  sort(ALLOF(w));

  vector<int> ret(Q, -1);
  FenwickTree ft(N + 5);
  int L = -1, R = -1;
  rep(i, Q) {
    Query query = w[i];
    if (R < query.r) {
      REP(j, R + 1, query.r + 1) {
        if (lst[v[j]] != -1) {
          ft.add(lst[v[j]], -1);
        }
        lst[v[j]] = j;
        ft.add(j, 1);
      }
    }
    ret[query.i] = ft.sum(query.r) - ft.sum(query.l - 1);
  }

  rep(i, ret.size()) { cout << ret[i] << endl; }

  return 0;
}