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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define maxn 5000010
struct node {
  int l, r, no;
} c[maxn];
int n, m;
int tree[maxn], front[maxn], last[maxn];
int ans[maxn];
bool cmp(node x, node y) { return x.r < y.r; }
int lowbit(int i) { return i & (-i); }
void update(int i, int x) {
  while (i <= maxn) {
    tree[i] += x;
    i += lowbit(i);
  }
}
int query(int i) {
  int sum = 0;
  while (i > 0) {
    sum += tree[i];
    i -= lowbit(i);
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    front[i] = last[temp] + 1; // front数组为i位置上的数上一次出现的位置+1
    last[temp] = i; // last数组为i位置上的数到目前为止最后一次出现的位置
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &c[i].l, &c[i].r);
    c[i].no = i; // 每次查询的序号
  }
  sort(c + 1, c + 1 + m, cmp); // 按右端点排序
  int k = 1;                   // k初值为1
  for (int i = 1; i <= m; i++) {
    while (k <= c[i].r) {
      k++;
      update(front[k - 1], 1);
      update(k, -1);
    }
    ans[c[i].no] = query(c[i].l); // 查询左端点
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define maxn 5000010
#define maxm 200010
struct node {
  int l, r, no;
} c[maxm];
int n, m;
int tree[maxn], front[maxn], last[maxn];
int ans[maxm];
bool cmp(node x, node y) { return x.r < y.r; }
int lowbit(int i) { return i & (-i); }
void update(int i, int x) {
  while (i <= maxn) {
    tree[i] += x;
    i += lowbit(i);
  }
}
int query(int i) {
  int sum = 0;
  while (i > 0) {
    sum += tree[i];
    i -= lowbit(i);
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    front[i] = last[temp] + 1; // front数组为i位置上的数上一次出现的位置+1
    last[temp] = i; // last数组为i位置上的数到目前为止最后一次出现的位置
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &c[i].l, &c[i].r);
    c[i].no = i; // 每次查询的序号
  }
  sort(c + 1, c + 1 + m, cmp); // 按右端点排序
  int k = 1;                   // k初值为1
  for (int i = 1; i <= m; i++) {
    while (k <= c[i].r) {
      k++;
      update(front[k - 1], 1);
      update(k, -1);
    }
    ans[c[i].no] = query(c[i].l); // 查询左端点
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#define prev fuckkkkk
using namespace std;
using pi = pair<int, int>;
using pii = pair<pi, int>;
vector<pi> v;
vector<pii> w;
int n, sqrtn, m, a[555555], check[555555], ans[555555], ret;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> m;
  sqrtn = sqrt(n);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    v.push_back({x, y});
    w.push_back({{x / sqrtn, y}, i});
  }
  sort(w.begin(), w.end());
  for (int j = 0; j < m; j++) {
    pi prev = {0, 0}, now = v[w[j].second];
    if (j)
      prev = v[w[j - 1].second];
    for (int i = prev.first - 1; i >= now.first; i--)
      if (++check[a[i]] == 1)
        ret++;
    for (int i = prev.second + 1; i <= now.second; i++)
      if (++check[a[i]] == 1)
        ret++;
    for (int i = prev.first; i < now.first; i++)
      if (!--check[a[i]])
        ret--;
    for (int i = prev.second; i > now.second; i--)
      if (!--check[a[i]])
        ret--;
    ans[w[j].second] = ret;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define prev fuckkkkk
using namespace std;
using pi = pair<int, int>;
using pii = pair<pi, int>;
vector<pi> v;
vector<pii> w;
int n, sqrtn, m, a[555555], check[555555], ans[555555], ret;
void add(int x) {
  if (++check[x] == 1)
    ret++;
}
void del(int x) {
  if (!--check[x])
    ret--;
}
void go(int x) {
  pi prev = {0, 0}, now = v[w[x].second];
  if (x)
    prev = v[w[x - 1].second];
  for (int i = prev.first - 1; i >= now.first; i--)
    add(a[i]);
  for (int i = prev.second + 1; i <= now.second; i++)
    add(a[i]);
  for (int i = prev.first; i < now.first; i++)
    del(a[i]);
  for (int i = prev.second; i > now.second; i--)
    del(a[i]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> m;
  sqrtn = sqrt(n);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    v.push_back({x, y});
    w.push_back({{x / sqrtn, y}, i});
  }
  sort(w.begin(), w.end());
  for (int i = 0; i < m; i++) {
    go(i);
    ans[w[i].second] = ret;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << '\\n';
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

/*
  By Anudeep Nekkanti
  Editorial at http://blog.anudeep2011.com/mos-algorithm/
  Question at http://www.spoj.com/problems/DQUERY/
  O( (N+M) * sqrt N )
*/

#include <algorithm>
#include <cstdio>
using namespace std;

#define N 511111
#define A 5111111
#define BLOCK 715 // ~sqrt(N)

int cnt[A], a[N], ans[N], answer = 0;

struct node {
  int L, R, i;
} q[N];

bool cmp(node x, node y) {
  if (x.L / BLOCK != y.L / BLOCK) {
    // different blocks, so sort by block.
    return x.L / BLOCK < y.L / BLOCK;
  }
  // same block, so sort by R value
  return x.R < y.R;
}

void add(int position) {
  cnt[a[position]]++;
  if (cnt[a[position]] == 1) {
    answer++;
  }
}

void remove(int position) {
  cnt[a[position]]--;
  if (cnt[a[position]] == 0) {
    answer--;
  }
}

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

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &q[i].L, &q[i].R);
    q[i].L--;
    q[i].R--;
    q[i].i = i;
  }

  sort(q, q + m, cmp);

  int currentL = 0, currentR = 0;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR <= R) {
      add(currentR);
      currentR++;
    }
    while (currentR > R + 1) {
      remove(currentR - 1);
      currentR--;
    }
    ans[q[i].i] = answer;
  }

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

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5.1e5, maxk = 1e6 + 1;
int root[maxn], L[23 * maxn], R[23 * maxn], sum[23 * 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;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int seg[1000005];
int n;
void upd(int i) {
  ++seg[i += n];
  while (i > 1) {
    i /= 2;
    seg[i] = seg[2 * i] + seg[2 * i + 1];
  }
}
int find(int l, int r) {
  l += n;
  r += n;
  int res = 0;
  while (l < r) {
    if (l % 2)
      res += seg[l++];
    if (r % 2)
      res += seg[--r];
    l /= 2;
    r /= 2;
  }
  return res;
}
map<int, int> last;
vector<pair<int, int>> queries[500001];
int ans[500001];
int main() {
  int q;
  scanf("%d%d", &n, &q);
  int x[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &x[i]);
  for (int i = 0; i < n; i++)
    last[x[i]] = -1;
  for (int i = 0; i < q; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    queries[b].push_back({a, i});
  }
  for (int i = 0; i < n; i++) {
    if (last[x[i]] != -1)
      upd(last[x[i]]);
    last[x[i]] = i;
    for (auto q : queries[i]) {
      ans[q.second] = (i - q.first + 1) - find(q.first, i);
    }
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int seg[400005];
int n;
void upd(int i) {
  ++seg[i += n];
  while (i > 1) {
    i /= 2;
    seg[i] = seg[2 * i] + seg[2 * i + 1];
  }
}
int find(int l, int r) {
  l += n;
  r += n;
  int res = 0;
  while (l < r) {
    if (l % 2)
      res += seg[l++];
    if (r % 2)
      res += seg[--r];
    l /= 2;
    r /= 2;
  }
  return res;
}
map<int, int> last;
vector<pair<int, int>> queries[200001];
int ans[200001];
int main() {
  int q;
  cin >> n >> q;
  int x[n];
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++)
    last[x[i]] = -1;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    queries[b].push_back({a, i});
  }
  for (int i = 0; i < n; i++) {
    if (last[x[i]] != -1)
      upd(last[x[i]]);
    last[x[i]] = i;
    for (auto q : queries[i]) {
      ans[q.second] = (i - q.first + 1) - find(q.first, i);
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 5e5 + 5; //,M=2e5+5;
int n, m;
int c[N];
struct Q {
  int l, r, id;
  bool operator<(Q &x) { return l < x.l; }
} q[N];
int ans[N];
int s[N];
void add(int pos, int x) {
  while (pos <= n) {
    s[pos] += x;
    pos += pos & -pos;
  }
}
int que(int pos) {
  int ans = 0;
  while (pos) {
    ans += s[pos];
    pos -= pos & -pos;
  }
  return ans;
}

int p[N], nxt[N]; // 颜色桶

int main() {
  // ios::sync_with_stdio(false);std::cin.tie(0);cout.tie(0);
  // freopen("a.in","r",stdin);
  // while(~scanf("%d",&n)){
  if (true) {
    // int T;scanf("%d",&T);for(int tttt=1;tttt<=T;tttt++){
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      scanf("%d", c + i);
    }
    for (int i = n; i; i--) {
      nxt[i] = p[c[i]];
      p[c[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
      if (p[i])
        add(p[i], 1);
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &q[i].l, &q[i].r);
      q[i].id = i;
    }
    std::sort(q + 1, q + 1 + m);
    int pos = 1;
    for (int i = 1; i <= m; i++) {
      while (pos < q[i].l) {
        add(pos, -1);
        if (nxt[pos])
          add(nxt[pos], 1);
        pos++;
      }
      ans[q[i].id] = que(q[i].r) - que(q[i].l - 1);
    }
    for (int i = 1; i <= m; i++)
      printf("%d\\n", ans[i]);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
// using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 2e5 + 5; //,M=2e5+5;
int n, k;
int a[N];

bool check(int len) {
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > len)
      cnt += (a[i] - 1) / len; // 8变4要1刀
  }
  return cnt <= k;
}

int main() {
  // ios::sync_with_stdio(false);std::cin.tie(0);cout.tie(0);
  // freopen("a.in","r",stdin);
  // while(~scanf("%d",&n)){
  if (true) {
    // int T;scanf("%d",&T);for(int tttt=1;tttt<=T;tttt++){
    scanf("%d%d", &n, &k);
    int l = 1, r = -1, ans = 0;
    for (int i = 1; i <= n; i++)
      scanf("%d", a + i), r = std::max(r, a[i]);
    while (l <= r) {
      int mid = l + r >> 1;
      if (check(mid))
        r = mid - 1, ans = mid;
      else
        l = mid + 1;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
@author: sharrad99
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (auto &x : c) {
    cin >> x;
    x--;
  }

  vector<tuple<int, int, int>> qq;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    qq.emplace_back(l, r, i);
  }

  int b = ceil(sqrt(n));
  sort(qq.begin(), qq.end(), [&](auto u, auto v) {
    if (get<0>(u) / b != get<0>(v) / b) {
      return u < v;
    }

    return (get<0>(u) / b & 1) ? (get<1>(u) < get<1>(v))
                               : (get<1>(u) > get<1>(v));
  });

  vector<int> cnt(n, 0);
  vector<int> res(q);
  int u = 0, ml = 0, mr = -1;
  for (int i = 0; i < q; i++) {
    while (mr < get<1>(qq[i])) {
      ++cnt[c[++mr]];
      if (cnt[c[mr]] == 1)
        u++;
    }

    while (ml < get<0>(qq[i])) {
      --cnt[c[ml++]];
      if (cnt[c[ml - 1]] == 0)
        u--;
    }

    while (ml > get<0>(qq[i])) {
      ++cnt[c[--ml]];
      if (cnt[c[ml]] == 1)
        u++;
    }

    while (mr > get<1>(qq[i])) {
      --cnt[c[mr--]];
      if (cnt[c[mr + 1]] == 0)
        u--;
    }
    res[get<2>(qq[i])] = u;
  }

  for (auto &x : res)
    cout << x << '\\n';
}

Error: TLE

Fixed submission:

/*
@author: sharrad99
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (auto &x : c)
    cin >> x;

  vector<tuple<int, int, int>> qq;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    qq.emplace_back(l, r, i);
  }

  int b = ceil(sqrt(n));
  sort(qq.begin(), qq.end(), [&](auto u, auto v) {
    if (get<0>(u) / b != get<0>(v) / b) {
      return u < v;
    }

    return (get<0>(u) / b & 1) ? (get<1>(u) < get<1>(v))
                               : (get<1>(u) > get<1>(v));
  });

  map<int, int> m;
  vector<int> res(q);
  int ml = 0, mr = -1;
  for (int i = 0; i < q; i++) {
    while (mr < get<1>(qq[i])) {
      ++m[c[++mr]];
    }

    while (ml < get<0>(qq[i])) {
      --m[c[ml++]];
      if (!m[c[ml - 1]])
        m.erase(c[ml - 1]);
    }

    while (ml > get<0>(qq[i])) {
      ++m[c[--ml]];
    }

    while (mr > get<1>(qq[i])) {
      --m[c[mr--]];
      if (!m[c[mr + 1]])
        m.erase(c[mr + 1]);
    }
    res[get<2>(qq[i])] = m.size();
  }

  for (auto &x : res)
    cout << x << '\\n';
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;

int A[500000], K[500001], k = 1, CL = 0, CR = -1, n, B;

void add(int I) {
  K[A[I]]++;
  if (K[A[I]] == 1) {
    k++;
  }
}

void Remove(int I) {
  K[A[I]]--;
  if (K[A[I]] == 0) {
    k--;
  }
}

int query(int L, int R) {
  while (CL < L) {
    Remove(CL);
    CL++;
  }
  while (CL > L) {
    CL--;
    add(CL);
  }
  while (CR < R) {
    CR++;
    add(CR);
  }
  while (CR > R) {
    Remove(CR);
    CR--;
  }
  return k;
}

bool Bsort(const pair<pair<int, int>, int> &a,
           const pair<pair<int, int>, int> &b) {
  if (a.first.first / B == b.first.first / B) {
    return a.first.second < b.first.second;
  } else {
    return a.first.first / B < b.first.first / B;
  }
}

int main() {
  int m, l, r, L, R, p, I;
  cin >> n >> m;
  B = sqrt(n);
  map<int, int> M;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (M[A[i]] == 0) {
      M[A[i]] = k;
      k++;
      K[A[i]] = 0;
    }
    A[i] = M[A[i]];
  }
  k = 0;
  int U[m];
  vector<pair<pair<int, int>, int>> V;
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    l--;
    r--;
    V.push_back({{l, r}, i});
  }
  sort(V.begin(), V.end(), Bsort);
  for (int i = 0; i < m; i++) {
    L = V[i].first.first;
    R = V[i].first.second;
    I = V[i].second;
    U[I] = query(L, R);
  }
  for (int i = 0; i < m; i++) {
    cout << U[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int A[500000], K[500001], k = 1, CL = 0, CR = -1, n, B;

void add(int I) {
  K[A[I]]++;
  if (K[A[I]] == 1) {
    k++;
  }
}

void Remove(int I) {
  K[A[I]]--;
  if (K[A[I]] == 0) {
    k--;
  }
}

int query(int L, int R) {
  while (CL < L) {
    Remove(CL);
    CL++;
  }
  while (CL > L) {
    CL--;
    add(CL);
  }
  while (CR < R) {
    CR++;
    add(CR);
  }
  while (CR > R) {
    Remove(CR);
    CR--;
  }
  return k;
}

bool Bsort(const pair<pair<int, int>, int> &a,
           const pair<pair<int, int>, int> &b) {
  if (a.first.first / B == b.first.first / B) {
    return a.first.second < b.first.second;
  } else {
    return a.first.first / B < b.first.first / B;
  }
}

int main() {
  int m, l, r, L, R, p, I;
  cin >> n >> m;
  B = sqrt(n);
  map<int, int> M;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (M[A[i]] == 0) {
      M[A[i]] = k;
      k++;
      K[A[i]] = 0;
    }
    A[i] = M[A[i]];
  }
  k = 0;
  int U[m];
  vector<pair<pair<int, int>, int>> V;
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    l--;
    r--;
    V.push_back({{l, r}, i});
  }
  sort(V.begin(), V.end(), Bsort);
  for (int i = 0; i < m; i++) {
    L = V[i].first.first;
    R = V[i].first.second;
    I = V[i].second;
    U[I] = query(L, R);
  }
  for (int i = 0; i < m; i++) {
    cout << U[i] << "\\n";
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
typedef long long ll;
int a[maxn], rt[maxn];
int lc[maxn * 40], rc[maxn * 40], tol;
ll sum[maxn * 40];
map<int, int> vis;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v, lc[tol] = lc[u], rc[tol] = rc[u];
  u = tol++;
  if (x == y)
    return;
  int m = (x + y) >> 1;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
ll query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr)
    return sum[u];
  int m = (x + y) >> 1;
  ll ans = 0;
  if (ql <= m)
    ans += query(lc[u], x, m, ql, qr);
  if (qr > m)
    ans += query(rc[u], m + 1, y, ql, qr);
  return ans;
}
int main() {
  int n, q;
  init();
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(a[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[a[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define T                                                                      \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 10;
const int MAXN = 2e5 + 10;
int n, k;
int a[MAXN];

inline bool check(int m) {
  int ans = 0;
  rep(i, 1, n) {
    if (a[i] != m)
      ans += a[i] / m;
    if (ans > k)
      return 0;
  }
  return ans <= k;
}

int main() {
  scanf("%d%d", &n, &k);
  int l = 1;
  int r = -1;
  rep(i, 1, n) {
    scanf("%d", a + i);
    if (i == 1) {
      r = a[i];
    } else {

      r = max(r, a[i]);
    }
  }

  //    rep(i,1,r) {
  //        printf("%d: %d\\n",i,check(i));
  //    }

  //    if (k == 0) {
  //        printf("%d\\n",r);
  //        return 0;
  //    }
  int m;
  int ans = 1;

  while (l < r) {

    m = (l + r) / 2;
    if (check(m)) {
      r = m;
      ans = m;
    } else
      l = m + 1;
  }
  printf("%d\\n", r);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define maxi 500000 + 10
#define 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;
  scanf("%d", &n);
  int q;
  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]);
}

Error: TLE

Fixed submission:

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

#define vl vector<ll>
#define vi vector<int>

#define ff first
#define ss second

#define pb push_back
#define MOD 1000000007
// const int nax = 1e6+5;
#define inf 4e18

#define display(v)                                                             \\
  for (int i = 0; i < v.size(); ++i)                                           \\
  cout << v[i] << " "
#define all(v) v.begin(), v.end()
ll sqr(ll x) { return x * x; }
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c >= MOD)
    c -= MOD;
  return c;
}
inline ll mul(ll a, ll b) { return (a * 1ll * b) % MOD; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += MOD;
  return c;
}
const int nax = 200 * 1000 + 10;
int n, q, sqn;
struct que {
  int l, r;
  int ind;
};
que Q[500002];
int ans[500002];
int A[500002], cont[500002];
int L, R, answer;
bool f(que A, que B) {
  if (A.l / sqn < B.l / sqn)
    return true;
  if (A.l / sqn > B.l / sqn)
    return false;
  return A.r < B.r;
}

void remove(int i) {
  cont[A[i]]--;
  if (cont[A[i]] == 0)
    answer--;
}

void add(int i) {
  cont[A[i]]++;
  if (cont[A[i]] == 1)
    answer++;
}
int query(int i) {

  while (L < Q[i].l) {
    remove(L);
    L++;
  }
  while (L > Q[i].l) {
    L--;
    add(L);
  }
  while (R < Q[i].r) {
    R++;
    add(R);
  }
  while (R > Q[i].r) {
    remove(R);
    R--;
  }
  return answer;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> q;
  sqn = sqrt(n);
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  for (int i = 1; i <= q; ++i) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].ind = i;
  }
  sort(Q + 1, Q + q + 1, f);

  for (int i = 1; i <= q; i++)
    ans[Q[i].ind] = query(i);
  for (int i = 1; i <= q; i++)
    cout << ans[i] << "\\n";

  return 0;
}

// Химаншу