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

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2000010;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

int lowbit(int x) { return x & (-x); }

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  while (~scanf("%d%d", &n, &q)) {
    memset(v, 0, sizeof(v));
    memset(sum, 0, sizeof(sum));
    memset(ans, 0, sizeof(ans));

    int t = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &v[i]);
    }
    // scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &query[i].l, &query[i].r);
      query[i].id = i;
    }
    sort(query + 1, query + q + 1);

    int pre = 1;
    map<int, int> mymap;
    for (int i = 1; i <= q; i++) {
      for (int j = pre; j <= query[i].r; j++) {
        // 已存在
        if (mymap[v[j]]) {
          change(mymap[v[j]], -1);
        }
        mymap[v[j]] = j;
        change(j, 1);
      }
      pre = query[i].r + 1;
      ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
    }

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


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

int lowbit(int x) { return x & (-x); }

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  while (~scanf("%d", &n)) {
    memset(v, 0, sizeof(v));
    memset(sum, 0, sizeof(sum));
    memset(ans, 0, sizeof(ans));

    int t = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &v[i]);
    }
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &query[i].l, &query[i].r);
      query[i].id = i;
    }
    sort(query + 1, query + q + 1);

    int pre = 1;
    map<int, int> mymap;
    for (int i = 1; i <= q; i++) {
      for (int j = pre; j <= query[i].r; j++) {
        // 已存在
        if (mymap[v[j]]) {
          change(mymap[v[j]], -1);
        }
        mymap[v[j]] = j;
        change(j, 1);
      }
      pre = query[i].r + 1;
      ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
    }

    for (int i = 1; i <= q; i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

// #pragma GCC optimize("Ofast")

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int NN = 1e9 + 7;
const int N = 1e6 + 7;
const int M = 26;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q;
int c[N];
int t[N];
int res[N];
int last[N];
vector<pair<int, int>> qr[N];

void upd(int x, int y) {
  while (x < N) {
    t[x] += y;
    x += x & -x;
  }
}

int get(int x) {
  int res = 0;
  while (x > 0) {
    res += t[x];
    x -= x & -x;
  }
  return res;
}

void solve1() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qr[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (last[c[i]] != 0)
      upd(last[c[i]], -1);
    upd(i, 1);
    last[c[i]] = i;
    for (auto x : qr[i]) {
      res[x.se] = get(i) - get(x.fi - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << res[i] << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen( "input.txt" , "r" , stdin );
  // freopen( "output.txt" , "w" , stdout );

  int cghf = 1; // cin >> cghf;
  while (cghf--) {
    solve1();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// #pragma GCC optimize("Ofast")

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int NN = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 26;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q;
int c[N];
int t[N];
int res[N];
int last[N];
vector<pair<int, int>> qr[N];

void upd(int x, int y) {
  while (x < N) {
    t[x] += y;
    x += x & -x;
  }
}

int get(int x) {
  int res = 0;
  while (x > 0) {
    res += t[x];
    x -= x & -x;
  }
  return res;
}

void solve1() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qr[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (last[c[i]] != 0)
      upd(last[c[i]], -1);
    upd(i, 1);
    last[c[i]] = i;
    for (auto x : qr[i]) {
      res[x.se] = get(i) - get(x.fi - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << res[i] << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen( "input.txt" , "r" , stdin );
  // freopen( "output.txt" , "w" , stdout );

  int cghf = 1; // cin >> cghf;
  while (cghf--) {
    solve1();
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N, Q;
  cin >> N >> Q;
  vector<unsigned long> c(N);
  for (auto &&i : c) {
    cin >> i;
    --i;
  }
  vector<tuple<unsigned long, unsigned long, unsigned long>> queries(Q);
  {
    unsigned long cnt{0};
    for (auto &&[i, l, r] : queries) {
      cin >> l >> r;
      --l;
      i = cnt++;
    }
  }
  constexpr unsigned long B{2000};
  sort(begin(queries), end(queries), [&B](const auto &lhs, const auto &rhs) {
    const auto &[i, li, ri] = lhs;
    const auto &[j, lj, rj] = rhs;
    return li / B == lj / B ? (li / B) & 1 ? ri > rj : ri < rj : li < lj;
  });
  unsigned long nl{0}, nr{0}, now{0};
  vector<unsigned long> cnt(N), ans(Q);
  for (const auto &[i, l, r] : queries) {
    while (nr < r)
      now += !cnt[c[nr++]]++;
    while (nr > r)
      now -= !--cnt[c[--nr]];
    while (nl < l)
      now -= !--cnt[c[nl++]];
    while (nl > l)
      now += !cnt[c[--nl]]++;
    ans[i] = now;
  }
  for (const auto &i : ans)
    cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N, Q;
  cin >> N >> Q;
  vector<unsigned long> c(N);
  for (auto &&i : c) {
    cin >> i;
    --i;
  }
  vector<tuple<unsigned long, unsigned long, unsigned long>> queries(Q);
  {
    unsigned long cnt{0};
    for (auto &&[i, l, r] : queries) {
      cin >> l >> r;
      --l;
      i = cnt++;
    }
  }
  constexpr unsigned long B{3000};
  sort(begin(queries), end(queries), [&B](const auto &lhs, const auto &rhs) {
    const auto &[i, li, ri] = lhs;
    const auto &[j, lj, rj] = rhs;
    return li / B == lj / B ? (li / B) & 1 ? ri > rj : ri < rj : li < lj;
  });
  unsigned long nl{0}, nr{0}, now{0};
  vector<unsigned long> cnt(N), ans(Q);
  for (const auto &[i, l, r] : queries) {
    while (nr < r)
      now += !cnt[c[nr++]]++;
    while (nr > r)
      now -= !--cnt[c[--nr]];
    while (nl < l)
      now -= !--cnt[c[nl++]];
    while (nl > l)
      now += !cnt[c[--nl]]++;
    ans[i] = now;
  }
  for (const auto &i : ans)
    cout << i << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
int c[maxn], vis[maxn], ans[maxn], tree[maxn];
vector<pair<int, int>> v[maxn];
int n, q;
int lowbit(int x) { return x & (-x); }
void add(int x, int k) {
  while (x <= n) {
    tree[x] += k;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x != 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    v[r].push_back(make_pair(i, l));
  }
  for (int i = 1; i <= n; i++) {
    if (vis[c[i]] != 0)
      add(vis[c[i]], -1);
    vis[c[i]] = i;
    add(i, 1);
    for (auto j : v[i])
      ans[j.first] = sum(i) - sum(j.second - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int c[maxn], vis[maxn], ans[maxn], tree[maxn];
vector<pair<int, int>> v[maxn];
int n, q;
int lowbit(int x) { return x & (-x); }
void add(int x, int k) {
  while (x <= n) {
    tree[x] += k;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x != 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    v[r].push_back(make_pair(i, l));
  }
  for (int i = 1; i <= n; i++) {
    if (vis[c[i]] != 0)
      add(vis[c[i]], -1);
    vis[c[i]] = i;
    add(i, 1);
    for (auto j : v[i])
      ans[j.first] = sum(i) - sum(j.second - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 500005;

struct Query {
  int l, r, idx;
};
bool cmp(Query x, Query y) { return x.r < y.r; }
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int arr[], int n, Query queries[], int q) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);
    while (query_counter < q && queries[query_counter].r == i) { // cout<<"a";
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";

  return;
}

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

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

  Query queries[q];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries[i].l = l - 1;
    queries[i].r = r - 1;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(arr, n, queries, q);
}


Error: 0

Fixed submission:

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

#define ll long long int
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fi(i, a, b) for (ll i = b - 1; i >= a; i--)
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define ml map<ll, ll>
#define pll pair<ll, ll>
set<ll> *segment;
void build(ll i, ll s, ll e, ll arr[]) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}
set<ll> query(ll node, ll l, ll r, ll a, ll b) {
  set<ll> left, right, result;
  if (b < l || a > r)
    return result;
  if (a <= l && r <= b)
    return segment[node];

  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());
  return result;
}
void init(ll n) {
  ll h = (ll)ceil(log2(n));
  h = (2 * (pow(2, h))) - 1;
  segment = new set<ll>[h];
}
ll getDistinct(ll l, ll r, ll n) {
  set<ll> ans = query(1, 0, n - 1, l, r);

  return ans.size();
}

int main() {
  ll n, q;
  cin >> n >> q;
  ll arr[n];
  init(n);
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  build(1, 0, n - 1, arr);
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << getDistinct(l - 1, r - 1, n) << "\\n";
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

const int maxi = 5e5 + 10;
const int lm = 5e5 + 10;
;
pair<int, pair<int, int>> pr[maxi];
int tree[maxi];
int p[lm];
int ans[maxi];
void update(int idx, int val) {
  while (idx < maxi) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}

int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

int main() {
  int n, q;
  cin >> n;
  cin >> q;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int x = 1;

  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &pr[i].second.first, &pr[i].first);
    pr[i].second.second = i;
  }
  sort(pr + 1, pr + q + 1);
  memset(tree, 0, sizeof(tree));
  memset(p, -1, sizeof(p));
  for (int i = 1; i <= n; i++) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);

    while (x <= q && pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);

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

Error: 0

Fixed submission:

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

const int maxi = 300000 + 10;
const int lm = 1000000 + 10;
pair<int, pair<int, int>> pr[maxi];
int tree[maxi];
int p[lm];
int ans[maxi];
void update(int idx, int val) {
  while (idx < maxi) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}

int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

int main() {
  int n, q;
  scanf("%d", &n);
  cin >> q;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  int x = 1;

  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &pr[i].second.first, &pr[i].first);
    pr[i].second.second = i;
  }
  sort(pr + 1, pr + q + 1);
  memset(tree, 0, sizeof(tree));
  memset(p, -1, sizeof(p));
  for (int i = 1; i <= n; i++) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);

    while (x <= q && pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);

      x++;
    }
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 500005;

// [1, n]
int bit[N], n, q;

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  rep(i, n) c[i]--;
  vector<pair<P, int>> query(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    query[i] = make_pair(make_pair(r, l), i);
  }
  sort(query.begin(), query.end());

  vector<int> gb(n, -1);
  rep(i, n + 1) bit[i] = 0;
  vector<int> ans(q);

  int qi = 0;
  rep(i, n) {
    if (gb[c[i]] != -1)
      add(gb[c[i]], -1);
    gb[c[i]] = i + 1;
    add(i + 1, 1);
    while (query[qi].first.first == i + 1) {
      int ai = query[qi].second;
      int l = query[qi].first.second;
      int r = query[qi].first.first;
      ans[ai] = sum(r) - sum(l - 1);
      qi++;
    }
  }

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

Error: 0

Fixed submission:

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

const int N = 500005;

// [1, n]
int bit[N], n, q;

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

int add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  rep(i, n) c[i]--;
  vector<pair<P, int>> query(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    query[i] = make_pair(make_pair(r, l), i);
  }
  sort(query.begin(), query.end());

  vector<int> gb(n, -1);
  rep(i, n + 1) bit[i] = 0;
  vector<int> ans(q);

  int qi = 0;
  rep(i, n) {
    if (gb[c[i]] != -1)
      add(gb[c[i]], -1);
    gb[c[i]] = i + 1;
    add(i + 1, 1);
    while (query[qi].first.first == i + 1) {
      int ai = query[qi].second;
      int l = query[qi].first.second;
      int r = query[qi].first.first;
      ans[ai] = sum(r) - sum(l - 1);
      qi++;
    }
  }

  rep(i, q) cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

int lowbit(int x) { return x & (-x); }

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  while (~scanf("%d", &n)) {
    scanf("%d", &q);
    memset(v, 0, sizeof(v));
    memset(sum, 0, sizeof(sum));
    memset(ans, 0, sizeof(ans));

    int t = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &v[i]);
    }
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &query[i].l, &query[i].r);
      query[i].id = i;
    }
    sort(query + 1, query + q + 1);

    int pre = 1;
    map<int, int> mymap;
    for (int i = 1; i <= q; i++) {
      for (int j = pre; j <= query[i].r; j++) {
        // 已存在
        if (mymap[v[j]]) {
          change(mymap[v[j]], -1);
        }
        mymap[v[j]] = j;
        change(j, 1);
      }
      pre = query[i].r + 1;
      ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
    }

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


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

int lowbit(int x) { return x & (-x); }

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  while (~scanf("%d", &n)) {
    scanf("%d", &q);
    memset(v, 0, sizeof(v));
    memset(sum, 0, sizeof(sum));
    memset(ans, 0, sizeof(ans));

    int t = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &v[i]);
    }
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &query[i].l, &query[i].r);
      query[i].id = i;
    }
    sort(query + 1, query + q + 1);

    int pre = 1;
    map<int, int> mymap;
    for (int i = 1; i <= q; i++) {
      for (int j = pre; j <= query[i].r; j++) {
        // 已存在
        if (mymap[v[j]]) {
          change(mymap[v[j]], -1);
        }
        mymap[v[j]] = j;
        change(j, 1);
      }
      pre = query[i].r + 1;
      ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
    }

    for (int i = 1; i <= q; i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 5e+5 + 5;
vector<int> st[4 * N];
int a[N], c[N], b[N], n;

void build(int l = 1, int r = n, int v = 1) {
  if (l == r) {
    st[v].push_back(a[l]);
    return;
  }
  build(l, (l + r) / 2, 2 * v);
  build((l + r) / 2 + 1, r, 2 * v + 1);
  merge(st[2 * v].begin(), st[2 * v].end(), st[2 * v + 1].begin(),
        st[2 * v + 1].end(), back_inserter(st[v]));
}

int querry(int l, int r, int x, int tl = 1, int tr = n, int v = 1) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    int ans = upper_bound(st[v].begin(), st[v].end(), x) - st[v].begin();
    int y = st[v].size();
    ans = y - ans;
    return ans;
  }
  int tm = (tl + tr) / 2;
  auto lft = querry(l, min(tm, r), x, tl, tm, 2 * v);
  auto rgt = querry(max(l, tm + 1), r, x, tm + 1, tr, 2 * v + 1);
  int ans = lft + rgt;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    b[i] = INT_MAX;
    cin >> c[i];
  }
  for (int i = n; i >= 1; i--) {
    a[i] = b[c[i]];
    b[c[i]] = i;
  }
  build();
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << querry(l, r, r) << "\\n";
  }
}


Error: TLE

Fixed submission:

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

const int N = 5e+5 + 5;
set<int> st[4 * N];
int a[N], n;

void build(int l = 1, int r = n, int v = 1) {
  if (l == r) {
    st[v].insert(a[l]);
    return;
  }
  build(l, (l + r) / 2, 2 * v);
  build((l + r) / 2 + 1, r, 2 * v + 1);
  st[v].insert(st[2 * v].begin(), st[2 * v].end());
  st[v].insert(st[2 * v + 1].begin(), st[2 * v + 1].end());
}

set<int> querry(int l, int r, int tl = 1, int tr = n, int v = 1) {
  if (l > r)
    return set<int>();
  if (l == tl && r == tr)
    return st[v];
  int tm = (tl + tr) / 2;
  auto lft = querry(l, min(tm, r), tl, tm, 2 * v);
  auto rgt = querry(max(l, tm + 1), r, tm + 1, tr, 2 * v + 1);
  set<int> ans;
  ans.insert(lft.begin(), lft.end());
  ans.insert(rgt.begin(), rgt.end());
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  build();
  while (q--) {
    int l, r;
    cin >> l >> r;
    auto s = querry(l, r);
    cout << s.size() << "\\n";
  }
}