alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 500003;
struct query {
  int l, r, id;
  query() {}
  query(int ll, int rr, int d) { l = ll, r = rr, id = d; }
  bool operator<(const query &x) { return r < x.r; }
};
int n, k, Q, c[N], ans[N], s[N];
query q[N];

void add(int i, int a) {
  while (i <= n) {
    s[i] += a;
    i += i & -i;
  }
}
int getsum(int i) {
  int ret = 0;
  while (i) {
    ret += s[i];
    i -= i & -i;
  }
  return ret;
}

int main() {
  cin >> n >> Q;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  for (int i = 0, l, r; i < Q; ++i) {
    scanf("%d %d", &l, &r);
    q[i] = query(l, r, i);
  }
  unordered_map<int, int> mp;
  sort(q, q + Q);
  for (int i = 0, pre = 1; i < Q; ++i) {
    for (int j = pre; j <= q[i].r; ++j) {
      add(j, 1);
      auto ptr = mp.find(c[j]);
      if (ptr != mp.end())
        add(mp[c[j]], -1);
      mp[c[j]] = j;
    }
    ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l - 1);
    pre = q[i].r + 1;
  }
  for (int i = 0; i < Q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 200003;
struct query {
  int l, r, id;
  query() {}
  query(int ll, int rr, int d) { l = ll, r = rr, id = d; }
  bool operator<(const query &x) { return r < x.r; }
};
int n, k, Q, c[N], ans[N], s[N];
query q[N];

void add(int i, int a) {
  while (i <= n) {
    s[i] += a;
    i += i & -i;
  }
}
int getsum(int i) {
  int ret = 0;
  while (i) {
    ret += s[i];
    i -= i & -i;
  }
  return ret;
}

int main() {
  cin >> n >> Q;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  for (int i = 0, l, r; i < Q; ++i) {
    scanf("%d %d", &l, &r);
    q[i] = query(l, r, i);
  }
  unordered_map<int, int> mp;
  sort(q, q + Q);
  for (int i = 0, pre = 1; i < Q; ++i) {
    for (int j = pre; j <= q[i].r; ++j) {
      add(j, 1);
      auto ptr = mp.find(c[j]);
      if (ptr != mp.end())
        add(mp[c[j]], -1);
      mp[c[j]] = j;
    }
    ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l - 1);
    pre = q[i].r + 1;
  }
  for (int i = 0; i < Q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

const int N = 5e5 + 5;
const long long k = sqrt(N);

struct Query {
  long long l;
  long long r;
  long long id;
} Q[N];

long long a[N], f[N];
long long res = 0, ans[N];

void add(long long x) {
  res -= f[a[x]] > 0;
  f[a[x]]++;
  res += f[a[x]] > 0;
}

void remove(long long x) {
  res -= f[a[x]] > 0;
  f[a[x]]--;
  res += f[a[x]] > 0;
}

int main() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (long long i = 0; i < m; i++) {
    scanf("%lld %lld", &Q[i].l, &Q[i].r);
    --Q[i].l, --Q[i].r;
    Q[i].id = i;
  }
  sort(Q, Q + m, [&](Query x, Query y) {
    long long k_x = x.l / k, k_y = y.l / k;
    if (k_x == k_y)
      return x.r < y.r;
    return k_x < k_y;
  });
  long long l = 0, r = -1;
  for (long long i = 0; i < m; i++) {
    while (l > Q[i].l)
      add(--l);
    while (r < Q[i].r)
      add(++r);
    while (l < Q[i].l)
      remove(l++);
    while (r > Q[i].r)
      remove(r--);
    ans[Q[i].id] = res;
  }
  for (long long i = 0; i < m; i++) {
    printf("%lld\\n", ans[i]);
  }
}


Error: WA

Fixed submission:

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

const long long N = 2e5 + 5;
const long long k = sqrt(N - 5);

struct Query {
  long long l, r;
  long long id;
} Q[N];

long long a[N], f[(int)1e6 + 5];
long long res = 0, ans[N];

void add(long long x) {
  res -= f[a[x]] * 1LL * f[a[x]] * a[x];
  f[a[x]]++;
  res += f[a[x]] * 1LL * f[a[x]] * a[x];
}

void remove(long long x) {
  res -= f[a[x]] * 1LL * f[a[x]] * a[x];
  f[a[x]]--;
  res += f[a[x]] * 1LL * f[a[x]] * a[x];
}

int main() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (long long i = 0; i < m; i++) {
    scanf("%lld %lld", &Q[i].l, &Q[i].r);
    --Q[i].l, --Q[i].r;
    Q[i].id = i;
  }
  sort(Q, Q + m, [&](Query x, Query y) {
    long long k_x = x.l / k, k_y = y.l / k;
    if (k_x == k_y)
      return x.r < y.r;
    return k_x < k_y;
  });
  long long l = 0, r = -1;
  for (long long i = 0; i < m; i++) {
    while (l > Q[i].l)
      add(--l);
    while (r < Q[i].r)
      add(++r);
    while (l < Q[i].l)
      remove(l++);
    while (r > Q[i].r)
      remove(r--);
    ans[Q[i].id] = res;
  }
  for (long long i = 0; i < m; i++) {
    printf("%lld\\n", ans[i]);
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 10;

int n, q;
int a[N], res[N], bit[N];
vector<int> pos[N];
vector<pair<int, int>> query[N];

void update(int x, int p) {
  for (; x <= n; x += x & -x)
    bit[x] += p;
}
int get(int x) {
  int ret = 0;
  for (; x; x -= x & -x)
    ret += bit[x];
  return ret;
}

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (pos[a[i]].empty())
      update(i, 1);
    pos[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++)
    reverse(pos[i].begin(), pos[i].end());
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    query[l].push_back(make_pair(r, i));
  }
  for (int i = 1; i <= n; i++) {
    for (pair<int, int> p : query[i])
      res[p.second] = get(p.first);
    update(pos[a[i]].back(), -1);
    pos[a[i]].pop_back();
    if (pos[a[i]].size())
      update(pos[a[i]].back(), 1);
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", res[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;
const int BLOCK = sqrt(N);

int n, q;
int a[N], cnt[N], res[N];
struct pack {
  int l, r, id;
} p[N];

void add(int x, int &ret) {
  cnt[a[x - 1]]++;
  ret += cnt[a[x - 1]] == 1;
}

void sub(int x, int &ret) {
  cnt[a[x - 1]]--;
  ret -= cnt[a[x - 1]] == 0;
}

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &p[i].l, &p[i].r);
    p[i].id = i;
  }
  sort(p, p + q, [](const pack &itl, const pack &itr) {
    return itl.l / BLOCK == itr.l / BLOCK ? itl.r < itr.r : itl.l < itr.r;
  });
  int prel = 0, prer = 0, ret = 0;
  for (int i = 0; i < q; i++) {
    for (int j = p[i].l; j <= prel - 1; j++)
      add(j, ret);
    for (int j = prel; j <= p[i].l - 1; j++)
      sub(j, ret);
    for (int j = p[i].r + 1; j <= prer; j++)
      sub(j, ret);
    for (int j = prer + 1; j <= p[i].r; j++)
      add(j, ret);
    prel = p[i].l;
    prer = p[i].r;
    res[p[i].id] = ret;
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", res[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

typedef struct node {
  int s, e, val, pos;
  bool operator<(const node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  node(){};
  node(int ss, int ee, int v, int p) { s = ss, e = ee, val = v, pos = p; }
} node;

int occs[(int)1e6 + 5], tree[500005], n, ans[500005];

void update(int x, int val) {
  while (x <= n) {
    tree[x] += val;
    x += x & -x;
  }
}

int query(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
#endif

  int q, s, e;
  vector<node> v;
  scanf("%d", &n);
  scanf("%d", &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s);
    v.push_back(node(i, i, s, -1));
  }

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &e);
    v.push_back(node(s, e, 0, i));
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    if (v[i].val == 0) {
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    } else {
      if (occs[v[i].val] != 0)
        update(occs[v[i].val], -1);
      update(v[i].s, 1);
      occs[v[i].val] = v[i].s;
    }
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <map>

using std::map;

const int MAXN = 500000, MAXLGN = 4;

int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
  int val, L, R, size;
} buff[2 * MAXN * MAXLGN];

int build(int lo, int hi) {
  if (lo > hi)
    return 0;

  int idx = ++cant;

  int mid = (lo + hi) / 2;
  buff[idx] = (node){mid, build(lo, mid - 1), build(mid + 1, hi), 0};
  return idx;
}

int update(int x, int val, int amount) {

  if (x == 0)
    return 0;

  int idx = ++cant;

  int L = buff[x].L;
  int R = buff[x].R;
  if (val < buff[x].val)
    L = update(L, val, amount);
  if (val > buff[x].val)
    R = update(R, val, amount);

  buff[idx] = (node){buff[x].val, L, R, buff[x].size + amount};

  return idx;
}

int query(int x, int val) {
  if (val < buff[x].val)
    return query(buff[x].L, val) + buff[x].size - buff[buff[x].L].size;

  if (val > buff[x].val)
    return query(buff[x].R, val);

  return buff[x].size - buff[buff[x].L].size;
}

int main() {

  scanf("%d", &N);
  scanf("%d", &Q);
  tree[0] = build(1, N);
  for (int i = 1; i <= N; i++) {
    int x, posx;
    scanf("%d", &x);
    posx = pos[x];
    if (posx != 0)
      tree[i] = update(update(tree[i - 1], posx, -1), i, +1);
    else
      tree[i] = update(tree[i - 1], i, +1);
    pos[x] = i;
  }

  while (Q--) {
    int lo, hi;
    scanf("%d %d", &lo, &hi);
    printf("%d\\n", query(tree[hi], lo));
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 750
#define mxn 5000001
using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;

  return a.r < b.r;
}

void add(int pos) {
  fre[ar[pos]]++;
  if (fre[ar[pos]] == 1)
    cnt++;
}

void remove(int pos) {
  fre[ar[pos]]--;
  if (fre[ar[pos]] == 0)
    cnt--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  for (int i = 0; i < q; i++) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i, Q[i].l--, Q[i].r--;
  }

  sort(Q, Q + q, comp);

  int ML = 0, MR = -1;
  for (int i = 0; i < q; i++) {
    int L = Q[i].l;
    int R = Q[i].r;

    while (ML > L)
      ML--, add(ML);

    while (MR < R)
      MR++, add(MR);

    while (ML < L)
      remove(ML), ML++;

    while (MR > R)
      remove(MR), MR--;

    ans[Q[i].i] = cnt;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 700
#define mxn 5000001
using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;

  return a.r < b.r;
}

void add(int pos) {
  fre[ar[pos]]++;
  if (fre[ar[pos]] == 1)
    cnt++;
}

void remove(int pos) {
  fre[ar[pos]]--;
  if (fre[ar[pos]] == 0)
    cnt--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  for (int i = 0; i < q; i++) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i, Q[i].l--, Q[i].r--;
  }

  sort(Q, Q + q, comp);

  int ML = 0, MR = -1;
  for (int i = 0; i < q; i++) {
    int L = Q[i].l;
    int R = Q[i].r;

    while (ML > L)
      ML--, add(ML);

    while (MR < R)
      MR++, add(MR);

    while (ML < L)
      remove(ML), ML++;

    while (MR > R)
      remove(MR), MR--;

    ans[Q[i].i] = cnt;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << '\\n';
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
const int Max = 5e5 + 5;
int K, ans[Max], arra[Max], sum, cnt, mp[Max];
struct Query {
  int index, l, r;
};

bool cmp(Query a, Query b) {
  if (a.l / K != b.l / K) {
    return a.l / K < b.l / K;
  } else if ((a.l / K) & 1)
    return a.r < b.r;
  return a.r > b.r;
}
Query query[Max];
void add(int index) {
  if (!mp[index]) {
    cnt++;
  }
  mp[index]++;
}
void del(int index) {
  mp[index]--;
  if (!mp[index]) {
    cnt--;
  }
}

void MO(int n, int q) {
  K = sqrt(n);
  sort(query, query + q, cmp);
  int st = 0, en = -1;
  cnt = 0;
  for (int i = 0; i < q; i++) {
    int l = query[i].l - 1;
    int r = query[i].r - 1;
    int idx = query[i].index;

    while (st > l) {
      st--;
      add(arra[st]);
    }
    while (st < l) {
      del(arra[st]);
      st++;
    }
    while (en < r) {
      en++;
      add(arra[en]);
    }
    while (en > r) {
      del(arra[en]);
      en--;
    }
    ans[idx] = cnt;
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}

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

  for (int i = 0; i < q; i++) {
    cin >> query[i].l >> query[i].r;
    query[i].index = i;
  }
  MO(n, q);
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5 + 5;
int K, ans[Max], arra[Max], sum, cnt, mp[Max];
struct Query {
  int index, l, r;
};

bool cmp(Query a, Query b) {
  if (a.l / K != b.l / K) {
    return a.l / K < b.l / K;
  } else if ((a.l / K) & 1)
    return a.r < b.r;
  return a.r > b.r;
}
Query query[Max];
void add(int index) {
  if (!mp[index]) {
    cnt++;
  }
  mp[index]++;
}
void del(int index) {
  mp[index]--;
  if (!mp[index]) {
    cnt--;
  }
}

void MO(int n, int q) {
  K = sqrt(n);
  sort(query, query + q, cmp);
  int st = 0, en = -1;
  cnt = 0;
  for (int i = 0; i < q; i++) {
    int l = query[i].l - 1;
    int r = query[i].r - 1;
    int idx = query[i].index;

    while (st > l) {
      st--;
      add(arra[st]);
    }
    while (st < l) {
      del(arra[st]);
      st++;
    }
    while (en < r) {
      en++;
      add(arra[en]);
    }
    while (en > r) {
      del(arra[en]);
      en--;
    }
    ans[idx] = cnt;
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}

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

  for (int i = 0; i < q; i++) {
    cin >> query[i].l >> query[i].r;
    query[i].index = i;
  }
  MO(n, q);
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAXN = 5e5 + 5;
const int blockSize = 707;

int a[MAXN];
int freq[MAXN];
int answer;
int ans[MAXN];

void add(int v) {
  freq[v]++;
  if (freq[v] == 1)
    answer++;
}

void rem(int v) {
  freq[v]--;
  if (freq[v] == 0)
    answer--;
}

struct Query {
  int l, r, id;

  bool operator<(Query const &q) const {
    if (l / blockSize != q.l / blockSize) {
      return l / blockSize < q.l / blockSize;
    }
    return r < q.r;
  }
};

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);

  vector<Query> queries(q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &queries[i].l, &queries[i].r);
    queries[i].l--;
    queries[i].r--;
    queries[i].id = i;
  }
  sort(queries.begin(), queries.end());

  int l = 1, r = 0;
  for (int i = 0; i < q; i++) {
    Query &query = queries[i];
    while (r < query.r)
      add(a[++r]);
    while (r > query.r)
      rem(a[r--]);
    while (l < query.l)
      rem(a[l++]);
    while (l > query.l)
      add(a[--l]);
    ans[query.id] = answer;
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }
}

Error: TLE

Fixed submission:

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

#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define mset(a, b) memset(a, b, sizeof(a))
#define DBG(x) cout << "[" << #x << "]: " << x << endl
using ll = long long;
using pii = pair<int, int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#ifndef __linux
#define getchar_unlocked _getchar_nolock
#endif

template <class Ty> Ty randint(Ty a, Ty b) {
  return uniform_int_distribution<Ty>(a, b)(rng);
}
template <class num> inline void rd(num &x) {
  char c, neg = 0;
  while (isspace(c = getchar_unlocked()))
    ;
  if (!isdigit(c))
    neg = (c == '-'), x = 0;
  else
    x = c - '0';
  while (isdigit(c = getchar_unlocked()))
    x = (x << 3) + (x << 1) + c - '0';
  x = neg ? -x : x;
}
template <class... Args> inline void rd(Args &...args) { (rd(args), ...); }

const int MAXN = 5e5 + 5, INF = 0x3f3f3f3f;

set<int> seg[4 * MAXN];

void build(int p, int i, int j) {
  if (i == j) {
    int x;
    rd(x);
    seg[p].insert(x);
  } else {
    int m = (i + j) / 2;
    build(p + p, i, m);
    build(p + p + 1, m + 1, j);
    seg[p] = seg[p + p];
    seg[p].insert(seg[p + p + 1].begin(), seg[p + p + 1].end());
  }
}

set<int> query(int p, int i, int j, int l, int r) {
  if (i > j or i > r or j < l)
    return {};
  if (i >= l && j <= r)
    return seg[p];
  int m = (i + j) / 2;
  set<int> L = query(p + p, i, m, l, r);
  set<int> R = query(p + p + 1, m + 1, j, l, r);
  L.insert(R.begin(), R.end());
  return L;
}

int main() {
  int n, q;
  rd(n, q);
  build(1, 1, n);

  while (q--) {
    int l, r;
    rd(l, r);
    printf("%ld\\n", query(1, 1, n, l, r).size());
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")

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

const int MN = 500012;

int n, q;
int a[MN];
int fr[MN];
pair<int, pair<int, int>> queries[MN];
int cur = 0;
int res[MN];

inline void add(int c) {
  fr[c]++;
  if (fr[c] == 1)
    cur++;
}

inline void remove(int c) {
  fr[c]--;
  if (fr[c] == 0)
    cur--;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries[i] = make_pair(i, make_pair(l, r));
  }
  int sz = (int)sqrt(n);
  sort(queries, queries + q, [sz](auto &a, auto &b) {
    if (a.second.first / sz != b.second.first / sz)
      return a.second.first < b.second.first;
    return a.second.second < b.second.second;
  });
  int l = queries[0].second.first, r = l;
  add(a[l]);
  for (int i = 0; i < q; i++) {
    auto [tl, tr] = queries[i].second;
    while (l > tl)
      add(a[--l]);
    while (r < tr)
      add(a[++r]);
    while (l < tl)
      remove(a[l++]);
    while (r > tr)
      remove(a[r--]);
    res[queries[i].first] = cur;
  }
  for (int i = 0; i < q; i++)
    cout << res[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

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

const int MN = 500012;

int n, q;
int a[MN];
int fr[MN];
vector<pair<int, pair<int, int>>> queries;
int cur = 0;
int res[MN];

void add(int c) {
  fr[c]++;
  if (fr[c] == 1)
    cur++;
}

void remove(int c) {
  fr[c]--;
  if (fr[c] == 0)
    cur--;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries.push_back(make_pair(i, make_pair(l, r)));
  }
  int sz = (int)sqrt(n);
  sort(queries.begin(), queries.end(), [sz](auto &a, auto &b) {
    if (a.second.first / sz != b.second.first / sz)
      return a.second.first < b.second.first;
    return a.second.second > b.second.second;
  });
  int l = queries[0].second.first, r = l;
  add(a[l]);
  for (int i = 0; i < q; i++) {
    auto [tl, tr] = queries[i].second;
    while (l > tl)
      add(a[--l]);
    while (r < tr)
      add(a[++r]);
    while (l < tl)
      remove(a[l++]);
    while (r > tr)
      remove(a[r--]);
    res[queries[i].first] = cur;
  }
  for (int i = 0; i < q; i++)
    cout << res[i] << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, q, c[500005], l[500005], r[500005], p[500005], bit[500005], ans[500005];
vector<int> v;

bool cmp(int x, int y) { return r[x] <= r[y]; }
void update(int x, int d) {
  while (x <= n) {
    bit[x] += d;
    x += x & (-x);
  }
}
int query(int x) {
  int ret = 0;
  while (x) {
    ret += bit[x];
    x -= x & (-x);
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
    v.push_back(i);
  }
  int pos = 0;
  sort(v.begin(), v.end(), cmp);
  for (auto i : v) {
    for (int j = pos + 1; j <= r[i]; j++) {
      if (p[c[j]])
        update(p[c[j]], -1);
      update(j, 1);
      p[c[j]] = j;
    }
    pos = r[i];
    ans[i] = query(r[i]) - query(l[i] - 1);
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <set>
using namespace std;

int n, q, c[500005], x, y;
set<int> st[500005 << 2];

void build(int x, int l, int r) {
  if (l == r) {
    st[x] = {c[l]};
    return;
  }
  int mid = (l + r) / 2;
  build(x << 1, l, mid);
  st[x].insert(st[x << 1].rbegin(), st[x << 1].rend());
  build(x << 1, mid + 1, r);
  st[x].insert(st[x << 1 | 1].rbegin(), st[x << 1 | 1].rend());
}
set<int> query(int x, int l, int r, int ql, int qr) {
  if (l == r)
    return {c[l]};
  set<int> ret = {};
  int mid = (l + r) / 2;
  if (ql <= mid) {
    set<int> tmp = query(x << 1, l, mid, ql, qr);
    ret.insert(tmp.rbegin(), tmp.rend());
  }
  if (mid < qr) {
    set<int> tmp = query(x << 1 | 1, mid + 1, r, ql, qr);
    ret.insert(tmp.rbegin(), tmp.rend());
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  build(1, 1, n);
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    cout << query(1, 1, n, x, y).size() << "\\n";
  }
}