alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  int c[N];
  int las[N];
  int ans[Q];
  int l[Q];
  int r[Q];
  vector<int> point[500010];
  vector<P> query[500010];
  BIT<int> bit(500010);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
    las[i] = -1;
  }
  for (int i = 0; i < N; i++) {
    if (las[c[i]] == -1) {
      las[c[i]] = i;
    } else {
      point[las[c[i]]].push_back(i);
      las[c[i]] = i;
    }
  }
  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
    query[l[i]].emplace_back(r[i], i);
  }

  for (int i = 500008; i >= 0; i--) {
    for (int j = 0; j < (int)point[i].size(); j++) {
      bit.add(point[i][j]);
    }
    for (int j = 0; j < (int)query[i].size(); j++) {
      ans[query[i][j].S] =
          (r[query[i][j].S] - l[query[i][j].S] + 1) - bit.sum(query[i][j].F);
    }
  }

  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N, Q;
  cin >> N >> Q;
  int c[N];
  int s = ((N - 1) / 700) + 1;
  vector<map<int, bool>> mp(s + 3);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  for (int i = 0; i < s - 1; i++) {
    if (i == s - 2) {
      for (int j = 0; j < (N + 1) % 700; j++) {
        mp[i][c[(i * 700) + j]] = true;
        ;
      }
      continue;
    }
    for (int j = 0; j < 700; j++) {
      mp[i][c[(i * 700) + j]] = true;
    }
  }

  for (int q = 0; q < Q; q++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vector<int> an(N);
    int ln = (l + 699) / 700;
    int rn = ((r + 1) / 700) - 1;
    int ans = 0;
    if (ln > rn) {

    } else {
      for (int i = ln; i <= rn; i++) {
        for (auto p : mp[i]) {
          if (an[p.first] == 0) {
            an[p.first] = 1;
            ans++;
          }
        }
      }
    }
    for (int i = min({N - 1, (ln - 1) * 700, r}); i >= l; i--) {
      if (an[c[i]] == 0) {
        an[c[i]] = 1;
        ans++;
      }
    }
    for (int i = max(l, (rn + 1) * 700); i <= r; i++) {
      if (an[c[i]] == 0) {
        an[c[i]] = 1;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;

// Binary Indexed Tree (Fenwick tree)
// O(log n)
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}

  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }

  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }

  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

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

  vector<int> c(n);
  rep(i, n) cin >> c[i];

  vector<int> pi(n + 1, -1); // 配列内のある要素が出現したindex
  vector<vector<int>> ps(
      n); // 配列内のある要素が二回目以降出現したindexのリスト
  rep(i, n) {
    int l = pi[c[i]];
    if (l != -1)
      ps[l].push_back(i); // 配列内のある要素が既に出現していたら
    pi[c[i]] = i;
  }

  vector<vector<P>> qs(
      n); // query区間のleftを行, rightとquery indexのペアを列に持つ
  rep(qi, q) {
    int l, r;
    cin >> l >> r;
    --l;
    --r; // queryは1-index想定で入力されるが, c[n]が0-indexなので合わせる
    qs[l].emplace_back(r, qi);
  }

  vector<int> ans(q);
  BIT<int> d(n);
  for (int x = n - 1; x >= 0; --x) {
    for (int y : ps[x])
      d.add(y, 1); // BITにおける, index 1からy+1までに現れる重複の総数
    for (P query : qs[x]) {
      int r = query.first, i = query.second;
      ans[i] = (r - x + 1) -
               d.sum(r); // 区間内の値の種類数は, 区間内の要素数 - 重複の数
    }
  }

  rep(i, q) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, Q;
  cin >> N >> Q;

  int c[N];
  for (int i = 1; i <= N; i++)
    cin >> c[i];

  for (int i = 1; i <= Q; i++) {
    int start, end;
    cin >> start >> end;
    set<int> s;
    for (int j = start; j <= end; j++) {
      s.insert(c[j]);
    }
    cout << s.size() << endl;
    s.clear();
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
const ll maxn = 5e5 + 5;
ll cnt[maxn];
ll a[maxn];
ll pos[maxn];
ll ans[maxn];
ll res = 0;
ll read() {
  ll res = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    res = (res << 1) + (res << 3) + c - 48, c = getchar();
  return res;
}
void printi(ll x) {
  if (x / 10)
    printi(x / 10);
  putchar(x % 10 + '0');
}
struct Q {
  ll l, r, k;
} QQ[maxn];
bool cmp(Q a, Q b) {
  return pos[a.l] == pos[b.l] ? a.r < b.r : pos[a.l] < pos[b.l];
}
void Sub(ll i) {
  cnt[a[i]]--;
  if (!cnt[a[i]])
    res--;
}
void Add(ll i) {
  cnt[a[i]]++;
  if (cnt[a[i]] == 1)
    res++;
}
int main() {
  ll n, q;
  n = read();
  q = read();
  ll siz = sqrt(n);
  for (ll i = 1; i <= n; i++) {
    a[i] = read();
    pos[i] = i / siz;
  }
  for (ll i = 0; i < q; i++) {
    QQ[i].l = read();
    QQ[i].r = read();
    QQ[i].k = i;
  }
  sort(QQ, QQ + q, cmp);
  ll l = 1, r = 0;
  for (ll i = 0; i < q; i++) {
    while (l < QQ[i].l)
      Sub(l++);
    while (l > QQ[i].l)
      Add(--l);
    while (r < QQ[i].r)
      Add(++r);
    while (r > QQ[i].r)
      Sub(r--);
    ans[QQ[i].k] = res;
  }
  for (ll i = 0; i < q; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
const ll maxn = 5e5 + 5;
ll cnt[maxn];
ll a[maxn];
ll pos[maxn];
ll ans[maxn];
ll res = 0;
struct Q {
  ll l, r, k;
} QQ[maxn];
bool cmp(Q a, Q b) {
  return pos[a.l] == pos[b.l] ? a.r < b.r : pos[a.l] < pos[b.l];
}
void Sub(ll i) {
  cnt[a[i]]--;
  if (!cnt[a[i]])
    res--;
}
void Add(ll i) {
  cnt[a[i]]++;
  if (cnt[a[i]] == 1)
    res++;
}
int main() {
  ll n, q;
  scanf("%lld%lld", &n, &q);
  ll siz = sqrt(n);
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    pos[i] = i / siz;
  }
  for (ll i = 0; i < q; i++) {
    scanf("%lld%lld", &QQ[i].l, &QQ[i].r);
    QQ[i].k = i;
  }
  sort(QQ, QQ + q, cmp);
  ll l = 1, r = 0;
  for (ll i = 0; i < q; i++) {
    while (l < QQ[i].l)
      Sub(l++);
    while (l > QQ[i].l)
      Add(--l);
    while (r < QQ[i].r)
      Add(++r);
    while (r > QQ[i].r)
      Sub(r--);
    ans[QQ[i].k] = res;
  }
  for (ll i = 0; i < q; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1LL << 60;
const int INF = 1LL << 17;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll bit[500500];
ll n;
#define N 500010
// ll sum(ll i){
//     ll res=0;
//     while(0<i){
//         res += bit[i];
//         i -= i&-i;
//     }
//     return res;
// }
// void add(ll i, ll x){
//     while(i<=n){
//         bit[i] += x;
//         i += i&-i;
//     }
// }

void add(int i, int x) {
  for (; i < N; i += i & -i)
    bit[i] += x;
}
int sum(int i) {
  int res = 0;
  for (; i > 0; i -= i & -i)
    res += bit[i];
  return res;
}
ll rsum(ll l, ll r) { return sum(r) - sum(l - 1); }
vector<vector<ll>> qr(N), qri(N);
vector<ll> a(N), c(N), res(N);
int main() {
  ll q;
  cin >> n >> q;

  rep(i, n) cin >> c[i + 1];

  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    qr[b].push_back(a);
    qri[b].push_back(i);
  }

  FOR(i, 1, n + 1) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, 1);
    a[c[i]] = i;
    rep(j, qr[i].size()) { res[qri[i][j]] = rsum(qr[i][j], i); }
  }
  rep(i, q) cout << res[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1LL << 60;
const int INF = 1LL << 17;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll bit[500500];
ll n;
ll sum(ll i) {
  ll res = 0;
  while (0 < i) {
    res += bit[i];
    i -= i & -i;
  }
  return res;
}
void add(ll i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}
ll rsum(ll l, ll r) { return sum(r) - sum(l - 1); }
vector<vector<ll>> qr(500500), qri(500500);
int main() {
  ll q;
  cin >> n >> q;
  vector<ll> c(n + 1);

  rep(i, n) cin >> c[i + 1];
  vector<ll> a(500500, 0);
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    qr[b].push_back(a);
    qri[b].push_back(i);
  }
  vector<ll> res(n + 1);
  FOR(i, 1, n + 1) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, 1);
    a[c[i]] = i;
    rep(j, qr[i].size()) { res[qri[i][j]] = rsum(qr[i][j], i); }
  }
  rep(i, q) cout << res[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  vector<int> p(n + 1, -1);
  vector<vector<int>> ps(n);
  rep(i, n) {
    int l = p[c[i]];
    if (l != -1)
      ps[l].push_back(i);
    p[c[i]] = i;
  }

  vector<vector<pi>> qs(n);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    qs[l].emplace_back(r, i);
  }

  vector<int> ans(q);
  BIT<int> d(n);
  rrep(x, n) {
    for (auto y : ps[x])
      d.add(y, 1);
    for (pi query : qs[x]) {
      int r = query.first;
      int i = query.second;
      ans[i] = (r - x + 1) - d.sum(r);
    }
  }

  rep(i, q) cout << ans[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n), l(q), r(q);
  rep(i, n) cin >> c[i];
  rep(i, q) cin >> l[i] >> r[i];

  map<int, int> m;
  int color_count = 0;
  rep(i, n) {
    c[i]--;
    if (m.count(c[i]) == 0) {
      m[c[i]] = color_count;
      color_count++;
    }
  }
  rep(i, n) c[i] = m[c[i]];

  BIT<ll> bits[color_count];
  rep(i, color_count) bits[i] = BIT<ll>(n + 1);

  rep(i, n) {
    int cc = c[i];
    bits[cc].add(i + 1, 1);
  }

  //  for(auto cc:c)cout<<cc<<' ';cout<<endl;

  rep(i, q) {
    int cl = l[i] - 1;
    int cr = r[i];
    // cout<<cl<<'-'<<cr<<endl;
    int result = 0;
    rep(j, color_count) {
      if ((bits[j].sum(cr) - bits[j].sum(cl)))
        result++;
      //      cout<<j<<':'<<result<<'
      //      '<<bits[j].sum(cr)-bits[j].sum(cl)<<'='<<bits[j].sum(cr)<<'-'<<bits[j].sum(cl)<<endl;
    }
    cout << result << endl;
  }

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define M 1000000007
#define lli long long int
using namespace std;

class Query {
public:
  int l, r, id;
  inline Query(int a, int b, int i) { l = a, r = b, id = i; }
};
int block = 0;
int unik;
bool cmp(const Query a, const Query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return (a.l / block) & 1 ? a.r < b.r : a.r > b.r;
}

int arr[500007];
int freq[500007];
vector<Query> all;
int ans[500007];

void add(int d) {
  freq[arr[d]]++;
  if (freq[arr[d]] == 1)
    unik++;
}
void del(int d) {
  freq[arr[d]]--;
  if (freq[arr[d]] == 0)
    unik--;
}

int main() {
  int n, i, j, k, p, q, l, r;

  scanf("%d %d", &n, &q);

  for (i = 1; i <= n; i++)
    scanf("%d", &arr[i]);

  for (i = 1; i <= q; i++) {
    scanf("%d %d", &l, &r);
    all.push_back(Query(l, r, i));
  }

  block = sqrt(n) + 1;
  sort(all.begin(), all.end(), cmp);

  int cur_l = 1, cur_r = 0;
  for (i = 0; i < q; i++) {
    l = all[i].l, r = all[i].r;
    while (cur_l > l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_l < l) {
      cur_l++;
      del(cur_l - 1);
    }
    while (cur_r > r) {
      cur_r--;
      del(cur_r + 1);
    }
    while (cur_r < r) {
      cur_r++;
      add(cur_r);
    }

    ans[all[i].id] = unik;
  }

  for (i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define M 1000000007
#define lli long long int
using namespace std;

class Query {
public:
  int l, r, id;
  Query(int a, int b, int i) { l = a, r = b, id = i; }
};
int block, unik;
bool cmp(Query a, Query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

int arr[500007];
int freq[500007];
vector<Query> all;
int ans[500007];

void add(int d) {
  freq[arr[d]]++;
  if (freq[arr[d]] == 1)
    unik++;
}
void del(int d) {
  freq[arr[d]]--;
  if (freq[arr[d]] == 0)
    unik--;
}

int main() {
  int n, i, j, k, p, q, l, r;

  scanf("%d %d", &n, &q);

  for (i = 1; i <= n; i++)
    scanf("%d", &arr[i]);

  for (i = 1; i <= q; i++) {
    scanf("%d %d", &l, &r);
    all.push_back(Query(l, r, i));
  }

  block = sqrt(n) + 1;
  sort(all.begin(), all.end(), cmp);

  int cur_l = 1, cur_r = 0;
  for (i = 0; i < q; i++) {
    l = all[i].l, r = all[i].r;
    while (cur_l > l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_l < l) {
      cur_l++;
      del(cur_l - 1);
    }
    while (cur_r > r) {
      cur_r--;
      del(cur_r + 1);
    }
    while (cur_r < r) {
      cur_r++;
      add(cur_r);
    }

    ans[all[i].id] = unik;
  }

  for (i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define foreach(it, v)                                                         \\
  for (__typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define chmax(a, b) a = max(a, b)
int n, Q, bl;
int c[500005];
int ans = 0, cnt[500005];
struct query {
  int l, r, id;
} q[500005];
int qans[500005];
bool cmp(query a, query b) {
  return (a.r / bl) == (b.r / bl) ? a.l < b.l : a.r < b.r;
}
void add(int x) {
  if (!cnt[c[x]]) {
    ans++;
  }
  cnt[c[x]]++;
}
void del(int x) {
  cnt[c[x]]--;
  if (!cnt[c[x]]) {
    ans--;
  }
}
int main() {
  cin >> n >> Q;
  bl = n / sqrt(Q * 2 / 3);
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  for (int i = 1; i <= Q; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }
  sort(q + 1, q + 1 + Q, cmp);
  int l = 0, r = 0;
  for (int i = 1; i <= Q; ++i) {
    int ql = q[i].l, qr = q[i].r;
    //		cout<<ql<<' '<<qr<<'\\n';
    while (l < ql) {
      del(l);
      l++;
    }
    while (l > ql) {
      l--;
      add(l);
    }
    while (r > qr) {
      del(r);
      r--;
    }
    while (r < qr) {
      r++;
      add(r);
    }
    qans[q[i].id] = ans;
  }
  for (int i = 1; i <= Q; ++i) {
    cout << qans[i] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second

int n, q, a[500005];
pair<int, int> b[500005];

int SolVe(int l, int r) {
  set<int> s;
  for (int i = l; i <= r; ++i) {
    s.insert(a[i]);
  }
  return s.size();
}
int main() {
  //    freopen("Bao.inp","r", stdin);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= q; ++i)
    cin >> b[i].x >> b[i].second;
  for (int i = 1; i <= q; ++i) {
    cout << SolVe(b[i].x, b[i].y) << "\\n";
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

int main() {
  class BIT {
    ll n;
    vector<ll> bitree;

  public:
    BIT(unsigned long _n) : bitree(_n + 1, 0) { n = _n; }
    void add(ll id, ll x) {
      while (id <= n) {
        bitree[id] += x;
        id += id & -id;
      }
      return;
    }
    ll sum(ll id) {
      ll temp = 0;
      while (id > 0) {
        temp += bitree[id];
        id -= id & -id;
      }
      return temp;
    }
  };
  cin >> N >> Q;
  vec c(N), l(Q), r(Q), ord(Q + 1), ans(Q);
  rep(i, N) {
    cin >> c[i];
    --c[i];
  }
  rep(i, Q) {
    cin >> l[i] >> r[i];
    --l[i];
    ord[i] = i;
  }
  ord[Q] = Q;
  l.push_back(-1);
  r.push_back(-1);
  sort(ALL(ord), [&](int x, int y) { return l[x] > l[y]; });
  vector<set<ll>> col(N);
  BIT bit(N + 10);
  ll i = 0;
  Rrep(L, N) {
    if (!col[c[L]].empty()) {
      bit.add(*col[c[L]].begin(), 1);
    }
    col[c[L]].insert(L);
    while (l[ord[i]] == L) {
      ll id = ord[i];
      ans[id] = r[id] - l[id] - bit.sum(r[id] - 1);
      ++i;
    }
  }
  rep(j, Q) { cout << ans[j] << endl; }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

int main() {
  class BIT {
    ll n;
    vector<ll> bitree;

  public:
    BIT(unsigned long _n) : bitree(_n + 1, 0) { n = _n; }
    void add(ll id, ll x) {
      while (id <= n) {
        bitree[id] += x;
        id += id & -id;
      }
      return;
    }
    ll sum(ll id) {
      ll temp = 0;
      while (id > 0) {
        temp += bitree[id];
        id -= id & -id;
      }
      return temp;
    }
  };
  cin >> N >> Q;
  vec c(N), l(Q), r(Q), ord(Q + 1), ans(Q);
  rep(i, N) cin >> c[i];
  rep(i, Q) {
    cin >> l[i] >> r[i];
    --l[i];
    ord[i] = i;
  }
  ord[Q] = Q;
  l.push_back(-1);
  r.push_back(-1);
  sort(ALL(ord), [&](int x, int y) { return l[x] > l[y]; });
  vector<set<ll>> col(N);
  BIT bit(N + 10);
  ll i = 0;
  Rrep(L, N) {
    if (!col[c[L]].empty()) {
      bit.add(*col[c[L]].begin(), 1);
    }
    col[c[L]].insert(L);
    while (l[ord[i]] == L) {
      ll id = ord[i];
      ans[id] = r[id] - l[id] - bit.sum(r[id] - 1);
      ++i;
    }
  }
  rep(j, Q) { cout << ans[j] << endl; }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
constexpr int Bsize = 1000;

int N, Q, C[500000], L[500000], R[500000], res[500000], b[500000], c[500000],
    tree[1 << 20];
vector<int> q[500000];

void add(int x) {
  x += (1 << 19);
  while (x) {
    tree[x]++;
    x /= 2;
  }
}
void erase(int x) {
  if (x == -1)
    return;
  x += (1 << 19);
  while (x) {
    tree[x]--;
    x /= 2;
  }
}

int calc(int l, int r) {
  int s = 0;
  l += (1 << 19);
  r += (1 << 19);
  while (l < r) {
    if (l & 1)
      s += tree[l];
    if (~r & 1)
      s += tree[r];
    l = (l + 1) / 2;
    r = (r - 1) / 2;
  }
  if (l == r)
    s += tree[l];
  return s;
}

int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> C[i];
    C[i]--;
  }
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
    L[i]--;
    R[i]--;
    q[R[i]].push_back(i);
  }
  for (int i = 0; i < N; i++) {
    c[i] = -1;
  }
  for (int i = 0; i < N; i++) {
    b[i] = c[C[i]];
    c[C[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    add(i);
    erase(b[i]);
    for (int j : q[i]) {
      res[j] = calc(L[j], R[j]);
    }
  }
  for (int i = 0; i < Q; i++) {
    cout << res[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
constexpr int Bsize = 1000;

int N, Q, C[500000], L[500000], R[500000], res[500000], b[500000], f[500000],
    c[500000];
vector<int> q[500000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> C[i];
    C[i]--;
  }
  for (int i = 0; i < N; i++) {
    c[i] = -1;
  }
  for (int i = 0; i < N; i++) {
    b[i] = c[C[i]];
    c[C[i]] = i;
  }
  for (int i = 0; i < N; i++) {
    c[i] = N;
  }
  for (int i = N - 1; i >= 0; i--) {
    f[i] = c[C[i]];
    c[C[i]] = i;
  }
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
    L[i]--;
    R[i]--;
    q[R[i]].push_back(i);
  }

  for (int i = Bsize; i < N; i += Bsize) {
    int t = 0;
    for (int j = i; j < N; j++) {
      if (b[j] < i)
        t++;
      for (int k : q[j]) {
        if (res[k] == 0 && L[k] <= i) {
          res[k] = t;
          for (int l = i - 1; l >= L[k]; l--) {
            if (j < f[l])
              res[k]++;
          }
        }
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    if (res[i] == 0) {
      for (int j = L[i]; j <= R[i]; j++) {
        if (b[j] < L[i])
          res[i]++;
      }
    }
    cout << res[i] << endl;
  }
}