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

#include <bits/stdc++.h>
using namespace std;
#define N 500010

// fenwick tree
int f[N];
void init() {
  for (int i = 0; i <= N; i++)
    f[i] = 0;
}

void add(int i, int x) {
  for (; i <= N; i += i & -i)
    f[i] += x;
}

int sum(int i) {
  int ret = 0;
  for (; i > 0; i -= i & -i)
    ret += f[i];
  return ret;
}

int rangesum(int l, int r) { return sum(r) - sum(l - 1); }
// fenwick tree

int c[N];
vector<int> ql[N], qid[N];

int ans[N];

int a[N];

int main() {
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; i++)
    cin >> c[i];

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;

    ql[r].push_back(l);
    qid[r].push_back(i);
  }

  for (int i = 1; i <= n; i++) {
    if (a[c[i - 1]] > 0)
      add(a[c[i - 1]], -1);
    add(i, 1);
    a[c[i - 1]] = i;

    for (int j = 0; j < ql[i].size(); j++) {
      ans[qid[i][j]] = rangesum(ql[i][j], i);
    }
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using str = string;
using vl = vector<ll>;
using vs = vector<str>;
using listl = list<ll>;

#define min(A, B) min(static_cast<ll>(A), static_cast<ll>(B))
#define max(A, B) max(static_cast<ll>(A), static_cast<ll>(B))
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back

#define rep(i, m) for (ll i = 0, i_max = m; i < i_max; i++)
#define erep(i, m) for (ll i = 0, i_max = m; i <= i_max; i++)
#define nrep(i, b, m) for (ll i = b, i_max = m; i < i_max; i++)
#define enrep(i, b, m) for (ll i = b, i_max = m; i <= i_max; i++)
#define brep(i, m) for (ll i = m; i >= 0; i--)
#define ebrep(i, b, e) for (ll i = b; i >= e; i--)

#define _rep(m) rep(i, m)
#define _erep(m) erep(i, m)
#define _nrep(b, m) nrep(i, b, m)
#define _enrep(b, m) enrep(i, b, m)
#define _brep(m) brep(i, m)
#define _ebrep(b, e) ebrep(i, b, e)

// modint: mod 計算を int を扱うように扱える構造体
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

const int MOD = 998244353;
using mint = Fp<MOD>;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

void in(ll &I) { cin >> I; }

void in(vl &I) {
  for (auto &i : I) {
    cin >> i;
  }
}

void in(vs &I) {
  for (auto &i : I) {
    cin >> i;
  }
}

void in(listl &I) {
  for (auto &i : I) {
    cin >> i;
  }
}

void in(str &s) { cin >> s; }

void out(ll O) { cout << O; }

void out(vl &O) {
  for (auto &o : O) {
    cout << o << endl;
  }
}

void rout(bool b, str True, str False) {
  if (b)
    cout << True.c_str();
  else
    cout << False.c_str();
}

const int INF = 1LL << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[3030][3030];

int main() {
  ll n, q;
  cin >> n >> q;

  vl c(n);
  in(c);

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;

    set<int> v;
    for (int j = l - 1; j < r; j++) {
      v.insert(c[j]);
    }

    cout << v.size() << endl;
  }

  return 0;
}