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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;

ll N, M;
const ll SIZE = 5000005;
ll c[SIZE];
ll A[SIZE];
ll Next[SIZE];
ll res[5000005];
ll show[5000005];
bool fir[SIZE];

struct Q {
  ll l, r;
  ll pos;
} q[5000005];

ll lowbit(ll k) { return k & (-k); }

void modify(ll n, ll v) {
  while (n <= N) {
    c[n] += v;
    n += lowbit(n);
  }
}

ll sum(ll n) {
  ll ans = 0;
  while (n > 0) {
    ans += c[n];
    n -= lowbit(n);
  }
  return ans;
}

ll cmp(Q a, Q b) { return a.l < b.l; }

int main() {
  cin >> N >> M;
  for (ll i = 1; i <= N; i++)
    cin >> A[i];
  for (ll i = N; i >= 1; i--) {
    if (!show[A[i]]) {
      show[A[i]] = i;
      fir[i] = true;
    } else {
      Next[i] = show[A[i]];
      fir[Next[i]] = false;
      fir[i] = true;
      show[A[i]] = i;
    }
  }
  for (ll i = 1; i <= M; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].pos = i;
  }
  sort(q + 1, q + 1 + M, cmp);

  for (ll i = 1; i <= N; i++)
    if (fir[i]) {
      modify(i, 1);
    }
  ll qtemp = q[1].l;
  ll ptr = 1;
  for (ll i = 1; i <= M; i++) {
    for (; ptr < q[i].l; ptr++) {
      if (fir[ptr]) {
        modify(ptr, -1);
        fir[ptr] = false;
        if (Next[ptr]) {
          fir[Next[ptr]] = true;
          modify(Next[ptr], 1);
        }
      }
    }
    ptr = q[i].l;
    qtemp = q[i].l;
    res[q[i].pos] = sum(q[i].r) - sum(q[i].l - 1);
  }
  for (ll i = 1; i <= M; i++)
    printf("%lld\\n", res[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;

ll N, M;
const ll SIZE = 500005;
ll c[SIZE];
ll A[SIZE];
ll Next[SIZE];
ll res[200005];
ll show[1000005];
bool fir[SIZE];

struct Q {
  ll l, r;
  ll pos;
} q[200005];

ll lowbit(ll k) { return k & (-k); }

void modify(ll n, ll v) {
  while (n <= N) {
    c[n] += v;
    n += lowbit(n);
  }
}

ll sum(ll n) {
  ll ans = 0;
  while (n > 0) {
    ans += c[n];
    n -= lowbit(n);
  }
  return ans;
}

ll cmp(Q a, Q b) { return a.l < b.l; }

int main() {
  scanf("%d %d", &N, &M);
  for (ll i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  for (ll i = N; i >= 1; i--) {
    if (!show[A[i]]) {
      show[A[i]] = i;
      fir[i] = true;
    } else {
      Next[i] = show[A[i]];
      fir[Next[i]] = false;
      fir[i] = true;
      show[A[i]] = i;
    }
  }
  for (ll i = 1; i <= M; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].pos = i;
  }
  sort(q + 1, q + 1 + M, cmp);

  for (ll i = 1; i <= N; i++)
    if (fir[i]) {
      modify(i, 1);
    }
  ll qtemp = q[1].l;
  ll ptr = 1;
  for (ll i = 1; i <= M; i++) {
    for (; ptr < q[i].l; ptr++) {
      if (fir[ptr]) {
        modify(ptr, -1);
        fir[ptr] = false;
        if (Next[ptr]) {
          fir[Next[ptr]] = true;
          modify(Next[ptr], 1);
        }
      }
    }
    ptr = q[i].l;
    qtemp = q[i].l;
    res[q[i].pos] = sum(q[i].r) - sum(q[i].l - 1);
  }
  for (ll i = 1; i <= M; i++)
    printf("%d\\n", res[i]);
  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() {

  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: 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() {

  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;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

const int N = 5e5 + 10;
int n, m, B, re, col[N], cnt[N], ans[N];
void add(int x) { re += !(cnt[x]++); }

void del(int x) { re -= !(--cnt[x]); }

struct Query {
  int l, r, id;
  bool operator<(Query &b) { return l / B == b.l / B ? r < b.r : l < b.l; }
} q[N];

int main() {
  int i, l = 1, r = 0;

  cin >> n >> m;

  B = n / sqrt(m);

  for (i = 1; i <= n; ++i) {
    cin >> col[i];
  }

  for (i = 0; i < m; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }

  sort(q, q + m);

  for (i = 0; i < m; ++i) {
    if (q[i].l == q[i].r) {
      ans[q[i].id] = 1;
      continue;
    }
    while (l > q[i].l)
      add(col[--l]);
    while (r < q[i].r)
      add(col[++r]);
    while (l < q[i].l)
      del(col[l++]);
    while (r > q[i].r)
      del(col[r--]);
    ans[q[i].id] = re;
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

const int N = 50010;
int n, m, B, re, col[N], cnt[N], ans[N];
void add(int x) { re += !(cnt[x]++); }

void del(int x) { re -= !(--cnt[x]); }

struct Query {
  int l, r, id;
  bool operator<(Query &b) { return l / B == b.l / B ? r < b.r : l < b.l; }
} q[N];

int main() {
  int i, l = 1, r = 0;

  cin >> n >> m;

  B = n / sqrt(m);

  for (i = 1; i <= n; ++i) {
    cin >> col[i];
  }

  for (i = 0; i < m; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }

  sort(q, q + m);

  for (i = 0; i < m; ++i) {
    if (q[i].l == q[i].r) {
      ans[q[i].id] = 1;
      continue;
    }
    while (l > q[i].l)
      add(col[--l]);
    while (r < q[i].r)
      add(col[++r]);
    while (l < q[i].l)
      del(col[l++]);
    while (r > q[i].r)
      del(col[r--]);
    ans[q[i].id] = re;
  }

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

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

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

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

  int n, q;
  cin >> n >> q;
  VI c(n);
  rep(i, n) cin >> c[i];
  vector<pair<int, int>> lr(q);
  rep(i, q) cin >> lr[i].first >> lr[i].second;
  rep(i, q) lr[i].first--;

  const int sq = 700;
  vector<tuple<int, int, int>> lri(q);
  rep(i, q) lri[i] = make_tuple(lr[i].first / sq, lr[i].second, i);
  sort(lri.begin(), lri.end());

  VI ans(q);

  VI cnt(n + 1);
  int prev = -1;
  int lx, rx;
  lx = rx = get<0>(lri[0]);
  int sum = 0;
  VI changed;
  for (auto [ls, r, i] : lri) {

    // ここのコメントを外すとhandmade03-05, random06がWAになる

    // if (ls != prev) {
    //   for (auto cx : changed) cnt[cx] = 0;
    //   changed.clear();
    //   lx = rx = r;
    //   prev = ls;
    // }

    int l = lr[i].first;
    assert(r == lr[i].second); // assert(rx <= r);
    int cx;
    while (lx > l)
      if (cnt[cx = c[--lx]]++ == 0)
        sum += 1; // changed.push_back(cx);
    while (rx < r)
      if (cnt[cx = c[rx++]]++ == 0)
        sum += 1; // changed.push_back(cx);
    while (lx < l)
      if (--cnt[c[lx++]] == 0)
        sum -= 1;
    while (rx > r)
      if (--cnt[c[--rx]] == 0)
        sum -= 1;
    ans[i] = sum;
  }
  rep(i, q) cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

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

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

  int n, q;
  cin >> n >> q;
  VI c(n);
  rep(i, n) cin >> c[i];
  vector<pair<int, int>> lr(q);
  rep(i, q) cin >> lr[i].first >> lr[i].second;
  rep(i, q) lr[i].first--;

  const int sq = 700;
  vector<tuple<int, int, int>> lri(q);
  rep(i, q) lri[i] = make_tuple(lr[i].first / sq, lr[i].second, i);
  sort(lri.begin(), lri.end());

  VI ans(q);

  VI cnt(n + 1);
  int prev = -1;
  int lx, rx;
  lx = rx = get<0>(lri[0]);
  int sum = 0;
  VI changed;
  for (auto [ls, r, i] : lri) {

    // ここのコメントを外すとhandmade03-05, random06がWAになる

    // if (ls != prev) {
    //   for (auto cx : changed) cnt[cx] = 0;
    //   changed.clear();
    //   lx = rx = r;
    //   prev = ls;
    // }

    int l = lr[i].first;
    assert(r == lr[i].second); // assert(rx <= r);
    int cx;
    while (lx > l)
      if (cnt[cx = c[--lx]]++ == 0)
        sum += 1, changed.push_back(cx);
    while (rx < r)
      if (cnt[cx = c[rx++]]++ == 0)
        sum += 1, changed.push_back(cx);
    while (lx < l)
      if (--cnt[c[lx++]] == 0)
        sum -= 1;
    while (rx > r)
      if (--cnt[c[--rx]] == 0)
        sum -= 1;
    ans[i] = sum;
  }
  rep(i, q) cout << ans[i] << "\\n";
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int e = 5e5 + 6;
const int e1 = 5e5 + 2;

struct kien {
  ll i, j, id, type;
};
kien a[e + e1];
ll n, q;
ll Bit[e];
ll res[e1];

bool lf(kien u, kien v) {
  return (u.j < v.j || (u.j == v.j && u.type < v.type));
}

void bit(ll vitri, ll val) {
  for (int i = vitri; i <= n; i += i & -i)
    Bit[i] += val;
}

ll getbit(ll vitri) {
  ll ans = 0;
  for (int i = vitri; i > 0; i -= i & -i) {
    ans += Bit[i];
  }
  return ans;
}

int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].i;
    a[i].j = i;
    a[i].type = -1;
  }
  map<ll, ll> last;
  for (int i = 1; i <= q; i++) {
    cin >> a[i + n].i >> a[i + n].j;
    a[i + n].id = i;
  }
  sort(a + 1, a + n + q + 1, lf);
  for (int i = 1; i <= q + n; i++) {
    if (a[i].type == -1) {
      bit(a[i].j, 1);
      if (last[a[i].i] > 0) {
        bit(last[a[i].i], -1);
      }
      last[a[i].i] = a[i].j;
    } else {
      res[a[i].id] = getbit(a[i].j) - getbit(a[i].i - 1);
    }
  }
  for (int i = 1; i <= q; i++)
    cout << res[i] << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int e = 5e5 + 6;
const int e1 = 5e5 + 2;

struct kien {
  ll i, j, id, type;
};
kien a[e + e1];
ll n, q;
ll Bit[e];
ll res[e1];

bool lf(kien u, kien v) {
  return (u.j < v.j || (u.j == v.j && u.type < v.type));
}

ll bit(ll vitri, ll val) {
  for (int i = vitri; i <= n; i += i & -i)
    Bit[i] += val;
}

ll getbit(ll vitri) {
  ll ans = 0;
  for (int i = vitri; i > 0; i -= i & -i) {
    ans += Bit[i];
  }
  return ans;
}

int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].i;
    a[i].j = i;
    a[i].type = -1;
  }
  map<ll, ll> last;
  for (int i = 1; i <= q; i++) {
    cin >> a[i + n].i >> a[i + n].j;
    a[i + n].id = i;
  }
  sort(a + 1, a + n + q + 1, lf);
  for (int i = 1; i <= q + n; i++) {
    if (a[i].type == -1) {
      bit(a[i].j, 1);
      if (last[a[i].i] > 0) {
        bit(last[a[i].i], -1);
      }
      last[a[i].i] = a[i].j;
    } else {
      res[a[i].id] = getbit(a[i].j) - getbit(a[i].i - 1);
    }
  }
  for (int i = 1; i <= q; i++)
    cout << res[i] << "\\n";
}
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 = 1e6 + 7;
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() {

  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() {

  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;
typedef pair<int, int> pii;
const int MAXN = 500005;
vector<pii> que[MAXN];
int n, a[MAXN], last[MAXN], bit[MAXN], ans[MAXN];

void update(int now, int val) {
  for (int i = now; i <= n; i += (i & (-i)))
    bit[i] += val;
}

int query(int now) {
  int ret = 0;
  for (int i = now; i > 0; i -= (i & (-i)))
    ret += bit[i];
  return ret;
}

int main() {
  int q, l, r;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    cin >> l >> r;
    que[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (last[a[i]] != 0)
      update(last[a[i]], -1);
    last[a[i]] = i;
    update(i, 1);
    for (auto isi : que[i]) {
      ans[isi.second] = query(i) - query(isi.first - 1);
    }
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
inline int readInt() {
  int x = 0;
  char ch = getchar_unlocked();
  while (ch < '0' || ch > '9')
    ch = getchar_unlocked();
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar_unlocked();
  }
  return x;
}

struct segment {
  int L, R, val;
  segment *ki, *ka;
  void build(int x, int y) {
    L = x;
    R = y;
    val = 0;
    if (x == y)
      return;
    ki = new segment;
    ka = new segment;
    int mid = (L + R) / 2;
    ki->build(L, mid);
    ka->build(mid + 1, R);
  }
  int query(int x) {
    if (L == R)
      return val;
    int mid = (L + R) / 2;
    if (x <= mid)
      return val + ki->query(x);
    else
      return val + ka->query(x);
  }
};

segment *update(segment *a, int x, int y) {
  if (a->L > y || a->R < x)
    return a;
  segment *ret;
  ret = new segment;
  ret->L = a->L;
  ret->R = a->R;
  if (a->L >= x && a->R <= y) {
    ret->ki = a->ki;
    ret->ka = a->ka;
    ret->val = a->val + 1;
    return ret;
  }
  ret->ki = update(a->ki, x, y);
  ret->ka = update(a->ka, x, y);
  ret->val = a->val;
  return ret;
}

int a[MAXN], b[MAXN];
segment *root[MAXN];

int cost(int l, int r) { return root[r]->query(l); }

int main() {
  int n, q, l, r;
  n = readInt();
  q = readInt();
  for (int i = 1; i <= n; i++)
    a[i] = readInt();
  root[0] = new segment;
  root[0]->build(1, n);
  for (int i = 1; i <= n; i++) {
    root[i] = update(root[i - 1], b[a[i]] + 1, i);
    b[a[i]] = i;
  }
  while (q--) {
    l = readInt();
    r = readInt();
    printf("%d\\n", cost(l, r));
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 1000119
int num[maxn], tree[maxn], booll[maxn], nnn[maxn], N, ww;
;
struct tt {
  int l, r;
  int pos;
};
tt ask[maxn];
bool cmp(tt x, tt y) { return x.r < y.r; }
int lowbit(int n) { return n & (-n); }
void add(int n, int now) {
  while (n <= N) {
    tree[n] += now;
    n += lowbit(n);
  }
}
int sum(int n) {
  int ans = 0;
  while (n != 0) {
    ans += tree[n];
    n -= lowbit(n);
  }
  return ans;
}
int main() {
  scanf("%d%d", &N, &ww);
  for (int i = 1; i <= N; i++)
    scanf("%d", &num[i]);
  for (int i = 1; i <= ww; i++) {
    scanf("%d%d", &ask[i].l, &ask[i].r);
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + ww, cmp);
  int next = 1;
  for (int i = 1; i <= ww; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (booll[num[j]])
        add(booll[num[j]], -1);
      add(j, 1);
      booll[num[j]] = j;
    }
    next = ask[i].r + 1;
    nnn[ask[i].pos] = sum(ask[i].r) - sum(ask[i].l - 1);
  }
  for (int i = 1; i <= ww; i++)
    cout << nnn[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 500010;
int pos[N], sz;
int cnt[N];
int a[N];
int ans[N];
ll res;
struct node {
  int l, r, k;
  bool operator<(const node &o) const {
    if (pos[l] == pos[o.l])
      return r < o.r;
    return pos[l] < pos[o.l];
  }
} q[N];
int n, m;
void add(int k) {
  cnt[a[k]]++;
  if (cnt[a[k]] == 1)
    res++;
}
void sub(int k) {
  cnt[a[k]]--;
  if (cnt[a[k]] == 0)
    res--;
}
int main() {
  scanf("%d%d", &n, &m);
  sz = sqrt(n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    pos[i] = i / sz;
  }
  for (int i = 0; i < m; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    q[i] = {l, r, i};
  }
  sort(q, q + m);
  int l = 1, r = 0;
  for (int i = 0; i < m; i++) {
    while (l < q[i].l)
      sub(l++);
    while (l > q[i].l)
      add(--l);
    while (r < q[i].r)
      add(++r);
    while (r > q[i].r)
      sub(r--);
    ans[q[i].k] = res;
  }
  for (int i = 0; i < m; 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 pair<ll, ll> P;
ll bi[514514];
void add(ll n, ll i, ll x) {
  for (; i <= n; i += i & -i)
    bi[i] += x;
}
ll sum(ll i) {
  ll num = 0;
  for (; i > 0; i -= i & -i)
    num += bi[i];
  return num;
}
ll c[514514], g[514514], ans[514514];
int main() {
  ll n, q;
  cin >> n >> q;
  vector<vector<ll>> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    v[r].push_back(l);
    w[r].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (g[c[i]])
      add(n, g[c[i]], -1);
    add(n, i, 1);
    g[c[i]] = i;
    ll cnt = 0;
    for (ll p : v[i]) {
      ans[w[i][cnt]] = sum(i) - sum(p - 1);
      cnt++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll bi[514514];
void add(ll n, ll i, ll x) {
  for (; i <= n; i += i & -i)
    bi[i] += x;
}
ll sum(ll i) {
  ll num = 0;
  for (; i > 0; i -= i & -i)
    num += bi[i];
  return num;
}
ll c[514514], g[514514], ans[514514];
int main() {
  ll n, q;
  cin >> n >> q;
  vector<vector<ll>> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    v[r].push_back(l);
    w[r].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (g[c[i]])
      add(n, g[c[i]], -1);
    add(n, i, 1);
    g[c[i]] = i;
    for (ll p : v[i]) {
      ans[w[i][p]] = sum(i) - sum(p - 1);
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}