prithivMLmods/Coder-Stat huggingface dataset:
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
// Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 1e6 + 1;
ll root[N], L[16 * N], R[16 * N], sum[16 * N];
ll rt = 1, sz = 1;
ll lpos[N];
ll copy(ll v, ll &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}
void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}
void add(ll pos, ll x, ll v = root[rt - 1], ll l = 0, ll r = N) {
  sum[v] += x;
  if (r - l == 1)
    return;
  ll m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

ll get(ll a, ll b, ll v, ll l = 0, ll r = N) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  ll m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  rt = sz = 1;
  ll n, q;
  cin >> n >> q;
  for (ll i = 1; i <= n; i++) {
    ll t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << '\n';
  }
}


Fixed submission:
// Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 15e5 + 1;
ll root[N], L[16 * N], R[16 * N], sum[16 * N];
ll rt = 1, sz = 1;
ll lpos[N];
ll copy(ll v, ll &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}
void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}
void add(ll pos, ll x, ll v = root[rt - 1], ll l = 0, ll r = N) {
  sum[v] += x;
  if (r - l == 1)
    return;
  ll m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

ll get(ll a, ll b, ll v, ll l = 0, ll r = N) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  ll m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  rt = sz = 1;
  ll n, q;
  cin >> n >> q;
  for (ll i = 1; i <= n; i++) {
    ll t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << '\n';
  }
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
// Program to compute sum of ranges for different range
// queries
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll block, n, m;
ll a[500001];
struct Query {
  ll L, R, i;
};
Query q[500001];

bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;
  return x.R < y.R;
}
void queryResults() {
  block = (ll)sqrt(n);
  sort(q, q + m, compare);
  ll currL = 0, currR = 0;
  ll currSum = 0;
  ll ans = 0;
  ll aa[m];
  ll f[500001] = {0};
  for (ll i = 0; i < m; i++) {
    ll L = q[i].L, R = q[i].R;
    while (currL < L) {
      f[a[currL]]--;
      if (f[a[currL]] == 0)
        ans--;
      currL++;
    }
    while (currL > L) {
      if (f[a[currL - 1]] == 0)
        ans++;
      f[a[currL - 1]]++;
      currL--;
    }
    while (currR <= R) {
      if (f[a[currR]] == 0)
        ans++;
      f[a[currR]]++;
      currR++;
    }
    while (currR > R + 1) {
      f[a[currR - 1]]--;
      if (f[a[currR - 1]] == 0)
        ans--;
      currR--;
    }
    aa[q[i].i] = ans;
  }
  for (ll i = 0; i < m; i++)
    cout << aa[i] << endl;
}

// Driver program
int main() {
  ll i, j, k;
  cin >> n >> m;

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

  for (i = 0; i < m; i++) {
    cin >> j >> k;
    q[i].L = j - 1;
    q[i].R = k - 1;
    q[i].i = i;
  }
  queryResults();
}

Fixed submission:
// Program to compute sum of ranges for different range
// queries
#include <bits/stdc++.h>
using namespace std;
#define ll int
ll block, n, m;
ll a[500001];
struct Query {
  ll L, R, i;
};
Query q[500001];

bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;
  return x.R < y.R;
}
void queryResults() {
  block = (ll)sqrt(n);
  sort(q, q + m, compare);
  ll currL = 0, currR = 0;
  ll currSum = 0;
  ll ans = 0;
  ll aa[m];
  ll f[500001] = {0};
  for (ll i = 0; i < m; i++) {
    ll L = q[i].L, R = q[i].R;
    while (currL < L) {
      f[a[currL]]--;
      if (f[a[currL]] == 0)
        ans--;
      currL++;
    }
    while (currL > L) {
      if (f[a[currL - 1]] == 0)
        ans++;
      f[a[currL - 1]]++;
      currL--;
    }
    while (currR <= R) {
      if (f[a[currR]] == 0)
        ans++;
      f[a[currR]]++;
      currR++;
    }
    while (currR > R + 1) {
      f[a[currR - 1]]--;
      if (f[a[currR - 1]] == 0)
        ans--;
      currR--;
    }
    aa[q[i].i] = ans;
  }
  for (ll i = 0; i < m; i++)
    cout << aa[i] << endl;
}

// Driver program
int main() {
  ll i, j, k;
  cin >> n >> m;

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

  for (i = 0; i < m; i++) {
    cin >> j >> k;
    q[i].L = j - 1;
    q[i].R = k - 1;
    q[i].i = i;
  }
  queryResults();
}
Problem p02599 (C++)

Buggy submission (status: 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{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;
}

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{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;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 6e6 + 10;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x;
}
void write(int x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + 48);
}

struct Node {
  int sum, lc, rc;
};
Node dat[N];
int tot;

int insert(int pos, int v, int rt, int l, int r) {
  int p = ++tot;
  dat[p] = dat[rt];
  if (r - l == 1) {
    dat[p].sum += v;
    return p;
  }
  int m = (l + r) >> 1;
  if (pos < m)
    dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
  else
    dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
  dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
  return p;
}

int query(int left, int rt, int l, int r) {
  if (left <= l)
    return dat[rt].sum;
  int m = (l + r) >> 1, sum = 0;
  if (left < m)
    sum += query(left, dat[rt].lc, l, m);
  sum += query(left, dat[rt].rc, m, r);
  return sum;
}
int last[N], root[N];

int main() {
  int n, q;
  n = read();
  q = read();
  for (int i = 1; i <= n; ++i) {
    int x = read();
    if (last[x]) {
      root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
      root[i] = insert(i, 1, root[i], 1, n + 1);
    } else
      root[i] = insert(i, 1, root[i - 1], 1, n + 1);
    last[x] = i;
  }
  while (q--) {
    int x, y;
    x = read();
    y = read();
    write(query(x, root[y], 1, n + 1));
    putchar('\n');
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 3e7 + 10;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x;
}
void write(int x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + 48);
}

struct Node {
  int sum, lc, rc;
};
Node dat[N];
int tot;

int insert(int pos, int v, int rt, int l, int r) {
  int p = ++tot;
  dat[p] = dat[rt];
  if (r - l == 1) {
    dat[p].sum += v;
    return p;
  }
  int m = (l + r) >> 1;
  if (pos < m)
    dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
  else
    dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
  dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
  return p;
}

int query(int left, int rt, int l, int r) {
  if (left <= l)
    return dat[rt].sum;
  int m = (l + r) >> 1, sum = 0;
  if (left < m)
    sum += query(left, dat[rt].lc, l, m);
  sum += query(left, dat[rt].rc, m, r);
  return sum;
}
int last[N], root[N];

int main() {
  int n, q;
  n = read();
  q = read();
  for (int i = 1; i <= n; ++i) {
    int x = read();
    if (last[x]) {
      root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
      root[i] = insert(i, 1, root[i], 1, n + 1);
    } else
      root[i] = insert(i, 1, root[i - 1], 1, n + 1);
    last[x] = i;
  }
  while (q--) {
    int x, y;
    x = read();
    y = read();
    write(query(x, root[y], 1, n + 1));
    putchar('\n');
  }
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\n'
#define BLOCK 555
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[500001];
int ar[500001], ans[500001];
int fre[500001];
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::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  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);

  // why ML = 0 , and MR = -1?
  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';
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\n'
#define BLOCK 710
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[500001];
int ar[500001], ans[500001];
int fre[500001];
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::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  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);

  // why ML = 0 , and MR = -1?
  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 (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int blk;
struct node {
  int left, right;
  int index;
};
static bool compare(node &p1, node &p2) {
  if (p1.left / blk == p2.left / blk)
    return p1.right < p2.right;
  return p1.left < p2.left;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  blk = sqrt(n);
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<node> q;
  q.resize(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    node p;
    p.left = l, p.right = r, p.index = i;
    q[i] = p;
  }
  sort(q.begin(), q.end(), compare);
  unordered_map<int, int> freq;
  vector<int> ans(m, 0);
  int ml = 0, mr = -1, count = 0;
  for (int i = 0; i < m; i++) {
    int l = q[i].left - 1, r = q[i].right - 1;
    int ind = q[i].index;
    while (ml > l)
      if (++freq[a[--ml]] == 1)
        count++;
    while (mr < r)
      if (++freq[a[++mr]] == 1)
        count++;
    while (ml < l)
      if (--freq[a[ml++]] == 0)
        count--;
    while (mr > r)
      if (--freq[a[mr--]] == 0)
        count--;
    ans[ind] = count;
  }
  for (auto &i : ans)
    cout << i << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int blk;
struct node {
  int left, right;
  int index;
};
static bool compare(node &p1, node &p2) {
  if (p1.left / blk == p2.left / blk)
    return p1.right < p2.right;
  return p1.left < p2.left;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  blk = sqrt(n);
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<node> q;
  q.resize(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    node p;
    p.left = l, p.right = r, p.index = i;
    q[i] = p;
  }
  sort(q.begin(), q.end(), compare);
  vector<int> freq(500001, 0);
  vector<int> ans(m, 0);
  int ml = 0, mr = -1, count = 0;
  for (int i = 0; i < m; i++) {
    int l = q[i].left - 1, r = q[i].right - 1;
    int ind = q[i].index;
    while (ml > l)
      if (++freq[a[--ml]] == 1)
        count++;
    while (mr < r)
      if (++freq[a[++mr]] == 1)
        count++;
    while (ml < l)
      if (--freq[a[ml++]] == 0)
        count--;
    while (mr > r)
      if (--freq[a[mr--]] == 0)
        count--;
    ans[ind] = count;
  }
  for (auto &i : ans)
    cout << i << endl;
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <map>

using std::map;

const int MAXN = 40000, MAXLGN = 16;

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;
}

Fixed submission:
#include <bits/stdc++.h>
#include <map>

using std::map;

const int MAXN = 500005, MAXLGN = 20;

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 (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02599 (C++)

Buggy submission (status: 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));
}

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";
}


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));
}

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";
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

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

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[500001];
int n, m, a[500001], ans[500001];
int st[25 * 100001], used[5000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

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

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int block_size;
struct Query {
  int l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  block_size = sqrt(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<Query> vq;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vq.push_back((Query){l, r, i});
  }
  vector<int> answers(q);
  int ans = 0;
  unordered_map<int, int> mp;
  auto add = [&](int idx) {
    if (mp[v[idx]] == 0) {
      ans++;
    }
    mp[v[idx]]++;
  };
  auto remove = [&](int idx) {
    if (mp[v[idx]] == 1) {
      ans--;
    }
    mp[v[idx]]--;
  };
  sort(vq.begin(), vq.end());
  int cur_l = 0;
  int cur_r = -1;
  for (Query qu : vq) {
    while (cur_l > qu.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < qu.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < qu.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > qu.r) {
      remove(cur_r);
      cur_r--;
    }
    answers[qu.idx] = ans;
  }
  for (int x : answers) {
    cout << x << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int block_size;
struct Query {
  int l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  block_size = sqrt(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<Query> vq;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vq.push_back((Query){l, r, i});
  }
  vector<int> answers(q);
  int ans = 0;
  vector<int> mp(n + 1, 0);
  auto add = [&](int idx) {
    if (mp[v[idx]] == 0) {
      ans++;
    }
    mp[v[idx]]++;
  };
  auto remove = [&](int idx) {
    if (mp[v[idx]] == 1) {
      ans--;
    }
    mp[v[idx]]--;
  };
  sort(vq.begin(), vq.end());
  int cur_l = 0;
  int cur_r = -1;
  for (Query qu : vq) {
    while (cur_l > qu.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < qu.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < qu.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > qu.r) {
      remove(cur_r);
      cur_r--;
    }
    answers[qu.idx] = ans;
  }
  for (int x : answers) {
    cout << x << "\n";
  }
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int nmax = 5e5 + 3;
int n, q, usu[nmax], sol, v[nmax];
struct Query {
  int l, r, idx;

  inline pair<int, int> toPair() const { return make_pair(l / sqrt(n), r); }
} t[nmax];

int ap[nmax];

inline bool operator<(const Query &a, const Query &b) {
  return a.toPair() < b.toPair();
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> v[i];
  for (int i = 1; i <= q; ++i) {
    cin >> t[i].l >> t[i].r;
    t[i].idx = i;
  }
  sort(t + 1, t + q + 1);

  int st = 0;
  int dr = 0;

  for (int i = 1; i <= q; ++i) {
    // cout << t[i].l << ' ' << t[i].r << '\n';

    if (t[i].l > dr || t[i].r < st) {
      for (int j = st; j <= dr; ++j)
        ap[v[j]] = 0;
      st = t[i].l;
      dr = st;
      sol = 1;
      ++ap[v[st]];
    }

    while (dr < t[i].r) {
      ap[v[++dr]]++;
      if (ap[v[dr]] == 1)
        ++sol;
    }

    while (st > t[i].l) {
      ap[v[--st]]++;
      if (ap[v[st]] == 1)
        ++sol;
    }

    while (st < t[i].l) {
      ap[v[st++]]--;
      if (ap[v[st - 1]] == 0)
        --sol;
    }

    while (dr > t[i].r) {
      ap[v[dr--]]--;
      if (ap[v[dr + 1]] == 0)
        --sol;
    }

    usu[t[i].idx] = sol;
  }

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


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int nmax = 5e5 + 3;
int n, q, usu[nmax], sol, v[nmax];
struct Query {
  int l, r, idx;

  inline pair<int, int> toPair() const {

    int ceva;
    if ((int)(l / sqrt(n)) % 2)
      ceva = -r;
    else
      ceva = r;
    return make_pair(l / sqrt(n), ceva);
  }
} t[nmax];

int ap[nmax];

inline bool operator<(const Query &a, const Query &b) {
  return a.toPair() < b.toPair();
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> v[i];
  for (int i = 1; i <= q; ++i) {
    cin >> t[i].l >> t[i].r;
    t[i].idx = i;
  }
  sort(t + 1, t + q + 1);

  int st = 0;
  int dr = 0;

  for (int i = 1; i <= q; ++i) {
    // cout << t[i].l << ' ' << t[i].r << '\n';

    if (t[i].l > dr || t[i].r < st) {
      for (int j = st; j <= dr; ++j)
        ap[v[j]] = 0;
      st = t[i].l;
      dr = st;
      sol = 1;
      ++ap[v[st]];
    }

    while (dr < t[i].r) {
      ap[v[++dr]]++;
      if (ap[v[dr]] == 1)
        ++sol;
    }

    while (st > t[i].l) {
      ap[v[--st]]++;
      if (ap[v[st]] == 1)
        ++sol;
    }

    while (st < t[i].l) {
      ap[v[st++]]--;
      if (ap[v[st - 1]] == 0)
        --sol;
    }

    while (dr > t[i].r) {
      ap[v[dr--]]--;
      if (ap[v[dr + 1]] == 0)
        --sol;
    }

    usu[t[i].idx] = sol;
  }

  for (int i = 1; i <= q; ++i)
    cout << usu[i] << '\n';
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#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';
}


Fixed submission:
#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';
}
Problem p02599 (C++)

Buggy submission (status: 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 = 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;
}

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 = 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;
}
Problem p02599 (C++)

Buggy submission (status: 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 = 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;
}


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 = 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;
}
Problem p02599 (C++)

Buggy submission (status: 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;
}

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;
}

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;
}

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;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e7, maxk = 1e7;
int root[maxn], L[maxn], R[maxn], sum[maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }

  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 15e5, maxk = 1e7;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }

  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\n";
  }
  return 0;
}