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

// #pragma GCC optimize("O3")

#include <bits/stdc++.h>
// #include <ctime>

using namespace std;

#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 500100, MAXK = 20;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld PI = asin(1) * 2;

void setIO() { FAST_IO; }

void setIO(string s) {
  setIO();
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

int n;
ll a[MAXN];
ll k;

ll f(ll x) {
  ll ret = 0;
  FOR(i, 1, n) { ret += ((ll)((a[i] + x - 1) / x) - 1); }
  return ret;
}

int main() {
  setIO();
  // setIO("lmio_1991_3e1_seka_01");

  cin >> n >> k;
  FOR(i, 1, n) cin >> a[i];

  ll le = 1, ri = 1e9;
  while (le < ri) {
    ll mid = (le + ri) / 2;
    // cout << " mid=" << mid << "  f = " << f(mid) << endl;
    if (f(mid) > k)
      le = mid + 1;
    else
      ri = mid;
  }

  ll ans = le;
  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

// #pragma GCC optimize("O3")

#include <bits/stdc++.h>
// #include <ctime>

using namespace std;

#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 500100, MAXK = 20;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld PI = asin(1) * 2;

void setIO() { FAST_IO; }

void setIO(string s) {
  setIO();
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

int n;
ll a[MAXN];
ll k;

ll f(ll x) {
  ll ret = 0;
  FOR(i, 1, n) { ret += ((ll)((a[i] + x - 1) / x) - 1); }
  return ret;
}

int main() {
  setIO();
  // setIO("lmio_1991_3e1_seka_01");

  cin >> n >> k;
  FOR(i, 1, n) cin >> a[i];

  ll le = 0.0, ri = 1e9;
  while (le < ri) {
    ll mid = (le + ri) / 2;
    // cout << " mid=" << mid << "  f = " << f(mid) << endl;
    if (f(mid) > k)
      le = mid + 1;
    else
      ri = mid;
  }

  ll ans = le;
  cout << ans << "\\n";

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

/*Always success does not build on success,success build on failure and
 * hardwork*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vec vector<long long>
#define map map<long long, long long>
#define scn(t) scanf("%d", &t);
#define lscn(t) scanf("%lld", &t);
#define mod 1000000007
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define endl "\\n"
using namespace std;
const long long N = 2e5 + 5;
long long n, k, arr[N];
bool check(long long mid) {
  long long ans = 0;

  for (long long i = 0; i < n; i++) {
    ans = ans + arr[i] / mid;

    if (arr[i] % mid == 0)
      ans--;
  }

  if (ans <= k)
    return 1;
  else
    return 0;
}
int main() {
  lscn(n);
  lscn(k);

  long long low = 1, high = INT_MAX, mid;

  for (long long i = 0; i < n; i++) {
    lscn(arr[i]);
  }

  while (low < high) {
    mid = (low + high) / 2;

    if (check(mid))
      high = mid;
    else
      low = mid + 1;

    //   cout<<mid<<endl;
  }

  if (check(low))
    cout << low << endl;
  else
    cout << low + 1 << endl;
}

Error: 0

Fixed submission:

/*Always success does not build on success,success build on failure and
 * hardwork*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vec vector<long long>
#define map map<long long, long long>
#define scn(t) scanf("%d", &t);
#define lscn(t) scanf("%lld", &t);
#define mod 1000000007
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define endl "\\n"
using namespace std;
const long long N = 2e5 + 5;
long long n, k, arr[N];
bool check(long long mid) {
  long long ans = 0;

  for (long long i = 0; i < n; i++) {
    ans = ans + arr[i] / mid;

    if (arr[i] % mid == 0)
      ans--;
  }

  if (ans <= k)
    return 1;
  else
    return 0;
}
int main() {
  lscn(n);
  lscn(k);

  long long low = 0, high = INT_MAX, mid;

  for (long long i = 0; i < n; i++) {
    lscn(arr[i]);
  }

  while (low < high) {
    mid = (low + high) / 2;

    if (check(mid))
      high = mid;
    else
      low = mid + 1;

    //   cout<<mid<<endl;
  }

  if (check(low))
    cout << low << endl;
  else
    cout << low + 1 << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

bool judge(ll x) {
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > x) {
      ll cnt = a[i] / x;
      if (a[i] % x == 0) {
        ans += cnt - 1;
      } else {
        ans += cnt;
      }
    }
  }
  // cout << x << " " << ans << endl;
  return ans <= k;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll l = 1, r = 1e9 + 7;
  ll ans = 0;
  while (l <= r) {
    ll m = (l + r) / 2;
    // cout << "l " << l << " r= " << r << endl;
    if (judge(m)) {
      //  cout << m << endl;
      r = m - 1;
      ans = m;
    } else {
      l = m + 1;
    }
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

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

bool judge(ll x) {
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > x) {
      int cnt = a[i] / x;
      if (a[i] % x == 0) {
        ans += cnt - 1;
      } else {
        ans += cnt;
      }
    }
  }
  // cout << x << " " << ans << endl;
  return ans <= k;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll l = 0, r = 1e9 + 7;
  ll ans = 0;
  while (l <= r) {
    ll m = (l + r) / 2;
    // cout << "l " << l << " r= " << r << endl;
    if (judge(m)) {
      //  cout << m << endl;
      r = m - 1;
      ans = m;
    } else {
      l = m + 1;
    }
  }
  printf("%lld\\n", ans);
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  ll left = 0, right = A[N - 1] + 1;
  ll centre;
  vector<ll> ans;
  while (right - left > 1) {
    centre = (left + right) / 2;
    ll cnt = 0;
    for (int i = 0; i < N; i++)
      cnt += ceil((double)A[i] / (double)centre) - 1;
    if (cnt > K)
      left = centre;
    else {
      ans.push_back(centre);
      right = centre;
    }
  }
  cout << ans.back() << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  ll left = 1, right = A[N - 1] + 1;
  ll centre;
  vector<ll> ans;
  while (right - left > 1) {
    centre = (left + right) / 2;
    ll cnt = 0;
    for (int i = 0; i < N; i++)
      cnt += ceil((double)A[i] / (double)centre) - 1;
    if (cnt > K)
      left = centre;
    else {
      ans.push_back(centre);
      right = centre;
    }
  }
  cout << ans.back() << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define input_from_file freopen("input.txt", "r", stdin);
using namespace std::chrono;

int main() {
  // input_from_file
  ll n, k;
  cin >> n >> k;
  vector<ll> vec(n, 0);
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  sort(vec.begin(), vec.end());
  ll l = 1, r = 1e10, mid, ans = vec[n - 1], sum = 0;
  while (l <= r) {
    mid = (l + r) / 2;
    sum = 0;
    for (int i = 0; i < n; i++) {
      ll y = ceil(1.0 * vec[i] / mid) - 1;
      sum += y;
      // cout<<"sum"<<i<<"="<<sum<<"\\n";
    }
    if (sum > k)
      l = mid + 1;
    else if (sum <= k) {
      ans = min(ans, mid);
      r = mid - 1;
    }
    // cout<<"l="<<l<<" ,r="<<r<<" ,mid="<<mid<<" ,ans="<<ans<<endl;
    // break;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define input_from_file freopen("input.txt", "r", stdin);
using namespace std::chrono;

int main() {
  // input_from_file
  ll n, k;
  cin >> n >> k;
  vector<ll> vec(n, 0);
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  sort(vec.begin(), vec.end());
  ll l = 0, r = 1e9, mid, ans = vec[n - 1], sum = 0, x;
  vector<ll> power(1, 0);
  power.pb(1);
  for (int i = 1; i < 40; i++)
    power.pb(power[i] * 2);
  while (l <= r) {
    mid = (l + r) / 2;
    sum = 0;
    bool flag = 1;
    for (int i = 0; i < n; i++) {
      ll y = vec[i] / mid;
      if (y > 35) {
        flag = 0;
        break;
      }
      x = power[y];
      sum += x;
      // cout<<"sum"<<i<<"="<<sum<<"\\n";
    }
    if (sum > k || flag == 0)
      l = mid + 1;
    else if (sum <= k) {
      ans = min(ans, mid);
      r = mid - 1;
    }
    // cout<<"l="<<l<<" ,r="<<r<<" ,mid="<<mid<<" ,ans="<<ans<<endl;
    // break;
  }
  cout << ans;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 50;
int n, m, a[maxn], t[maxn], v[maxn], last[maxn], ans[maxn];
struct Qusetion {
  int l, r, num;
} q[maxn];
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f *= -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + (c ^ 48), c = getchar();
  return x * f;
}
bool cmp(Qusetion x, Qusetion y) { return x.r < y.r; }
inline int lowbit(int x) { return x & (-x); }
void update(int x, int k) {
  while (x <= n)
    t[x] += k, x += lowbit(x);
}
int query(int x) {
  int sum = 0;
  while (x)
    sum += t[x], x -= lowbit(x);
  return sum;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= m; i++)
    q[i].l = read(), q[i].r = read(), q[i].num = i;
  sort(q + 1, q + 1 + m, cmp);
  for (int i = 1; i <= m; i++) {
    for (int j = q[i - 1].r + 1; j <= q[i].r; j++) {
      if (last[a[j]])
        update(last[a[j]], -1);
      last[a[j]] = j;
      update(j, 1);
    }
    ans[q[i].num] = query(q[i].r) - query(q[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 5e5 + 50, maxm = 50 * maxn;
int n, q, l, r, cnt, rt[maxn], ls[maxm], rs[maxm], s[maxm], t[maxm];
int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + (c ^ 48), c = getchar();
  return x;
}
void add(int &x, int p, int y, int l = 1, int r = n) {
  x = ++cnt, ls[x] = ls[y], rs[x] = rs[y];
  if (l == r) {
    s[x] = s[y] + 1, t[x] = 1;
    return;
  }
  int mid = (l + r) / 2;
  if (p <= mid)
    add(ls[x], p, ls[y], l, mid);
  else
    add(rs[x], p, rs[y], mid + 1, r);
  s[x] = s[ls[x]] + s[rs[x]], t[x] = t[ls[x]] + t[rs[x]];
}
int query(int x, int y, int l = 1, int r = n) {
  if (!y)
    return 0;
  if (!x)
    return t[y];
  if (l == r)
    return (s[y] - s[x] > 0);
  int mid = (l + r) / 2;
  return query(ls[x], ls[y], l, mid) + query(rs[x], rs[y], mid + 1, r);
}
int main() {
  n = read(), q = read();
  for (int i = 1; i <= n; i++)
    add(rt[i], read(), rt[i - 1]);
  //	for(int i = 1; i <= n; i ++) cout << rt[i] << ' ';
  //	cout << endl;
  while (q--) {
    l = read(), r = read();
    printf("%d\\n", query(rt[l - 1], rt[r]));
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 5e5 + 11;
int cnt[N];
int a[N];
int ans[N];
int n, Q, block;
int sum = 0;

struct query {
  int l, r, id;
  bool operator<(const query &lhs) const {
    int block_a = l / block;
    int block_b = lhs.l / block;
    return block_a == block_b ? r < lhs.r : block_a < block_b;
  }
};

query q[N];

void add(int x) {
  x = a[x];
  cnt[x]++;
  sum += (cnt[x] == 1);
}

void remove(int x) {
  x = a[x];
  cnt[x]--;
  sum -= (cnt[x] == 0);
}

void MO() {
  block = 1000;
  sort(q, q + Q);
  int l = 0, r = -1;
  for (int i = 0; i < Q; i++) {
    while (l > q[i].l)
      add(--l);
    while (r < q[i].r)
      add(++r);
    while (r > q[i].r)
      remove(r--);
    while (l < q[i].l)
      remove(l++);
    ans[q[i].id] = sum;
  }
}

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

  cin >> n >> Q;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < Q; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].l--, q[i].r--, q[i].id = i;
  }
  MO();
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

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

const int N = 5e5 + 11;
unordered_map<int, int> cnt;
int n, Q, block, a[N], ans[N];
int sum = 0;

struct query {
  int l, r, id;
  bool operator<(const query &lhs) const {
    int block_a = l / block;
    int block_b = lhs.l / block;
    return block_a == block_b ? r < lhs.r : block_a < block_b;
  }
};

query q[N];

void add(int x) {
  x = a[x];
  cnt[x]++;
  sum += (cnt[x] == 1);
}

void remove(int x) {
  x = a[x];
  cnt[x]--;
  sum -= (cnt[x] == 0);
}

void MO() {
  block = sqrt(n) + 1;
  sort(q, q + Q);
  int l = 0, r = -1;
  for (int i = 0; i < Q; i++) {
    while (l > q[i].l)
      add(--l);
    while (r < q[i].r)
      add(++r);
    while (r > q[i].r)
      remove(r--);
    while (l < q[i].l)
      remove(l++);
    ans[q[i].id] = sum;
  }
}

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

  cin >> n >> Q;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < Q; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].l--, q[i].r--, q[i].id = i;
  }
  MO();
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

// BITの実装
template <typename T> struct BIT {
  int n;
  vector<T> bit;
  BIT(int n = 0) : n(n), bit(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      bit[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += bit[i];
    }
    return x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }

  // クエリを始点の大きさをもとに並べる---
  vector<vector<P>> query(n);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    // 番号も保持しておく
    query[l].emplace_back(r, i);
  }

  // 矢印の配列をつくる------------------
  vector<vector<int>> arrow(n);
  // 既に現れた数字を格納する
  vector<int> app(n, -1);

  rep(i, n) {
    int num = app[c[i]];
    if (num != -1) {
      arrow[num].emplace_back(i);
    }
    app[c[i]] = i;
  }

  // X軸に関して走査を行う-----------------
  vector<int> ans(q);
  BIT<int> bit(n);

  // x軸の大きい順に調べる
  for (int x = n - 1; x >= 0; --x) {
    // 矢印の座標と一致したら、bitのy座標番目を+1
    for (int y : arrow[x])
      bit.add(y, 1);
    // クエリを調べる
    for (P qu : query[x]) {
      int r = qu.first, i = qu.second;
      // bitのrまでの数字の合計が重複分
      ans[i] = (r - x + 1) - bit.sum(r);
    }
  }
  rep(i, q) cout << ans[i] << endl;

  return 0;
}

Error: -11

Fixed submission:

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

// BITの実装
template <typename T> struct BIT {
  int n;
  vector<T> bit;
  BIT(int n = 0) : n(n), bit(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      bit[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += bit[i];
    }
    return x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }

  // クエリを始点の大きさをもとに並べる---
  vector<vector<P>> query(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    // 番号も保持しておく
    query[l].emplace_back(r, i);
  }

  // 矢印の配列をつくる------------------
  vector<vector<int>> arrow(n);
  // 既に現れた数字を格納する
  vector<int> app(n, -1);

  rep(i, n) {
    int num = app[c[i]];
    if (num != -1) {
      arrow[num].push_back(i);
    }
    app[c[i]] = i;
  }

  // X軸に関して走査を行う-----------------
  vector<int> ans(q);
  BIT<int> bit(n);

  // x軸の大きい順に調べる
  for (int x = n - 1; x >= 0; --x) {
    // 矢印の座標と一致したら、bitのy座標番目を+1
    for (int y : arrow[x])
      bit.add(y, 1);
    // クエリを調べる
    for (P qu : query[x]) {
      int r = qu.first, i = qu.second;
      // bitのrまでの数字の合計が重複分
      ans[i] = (r - x + 1) - bit.sum(r);
    }
  }
  rep(i, q) cout << ans[i] << endl;

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 500001;

int A[MAXN], B[MAXN];
vector<int> T[4 * MAXN];

void build(int i, int l, int r) {
  if (l == r)
    T[i].push_back(B[r]);
  else {
    int m = (l + r) / 2;
    build(2 * i, l, m);
    build(2 * i + 1, m + 1, r);
    merge(T[2 * i].begin(), T[2 * i].end(), T[2 * i + 1].begin(),
          T[2 * i + 1].end(), back_inserter(T[i]));
  }
}

int ask(int i, int l, int r, int ql, int qr) {
  if (r < ql || qr < l)
    return 0;
  if (ql <= l && r <= qr)
    return lower_bound(T[i].begin(), T[i].end(), ql) - T[i].begin();
  int m = (l + r) / 2;
  return ask(2 * i, l, m, ql, qr) + ask(2 * i + 1, m + 1, r, ql, qr);
}

void solver() {
  int N, M;
  cin >> N >> M;
  // cin >> N;

  for (int i = 0; i < N; ++i)
    cin >> A[i];

  memset(B, -1, sizeof(B));

  map<int, int> last;

  for (int i = 0; i < N; ++i) {
    if (!last.count(A[i]))
      B[i] = -1;
    else
      B[i] = last[A[i]];

    last[A[i]] = i;
  }

  build(1, 0, N - 1);

  // cin >> M;

  while (M-- > 0) {
    int left, right;
    cin >> left >> right;
    cout << ask(1, 0, N - 1, left - 1, right - 1) << '\\n';
  }
}

int main() {

  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);

  int t;
  // cin >> t;
  t = 1;

  while (t--)
    solver();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200010;

int A[MAXN], B[MAXN];
vector<int> T[4 * MAXN];

void build(int i, int l, int r) {
  if (l == r)
    T[i].push_back(B[r]);
  else {
    int m = (l + r) / 2;
    build(2 * i, l, m);
    build(2 * i + 1, m + 1, r);
    merge(T[2 * i].begin(), T[2 * i].end(), T[2 * i + 1].begin(),
          T[2 * i + 1].end(), back_inserter(T[i]));
  }
}

int ask(int i, int l, int r, int ql, int qr) {
  if (r < ql || qr < l)
    return 0;
  if (ql <= l && r <= qr)
    return lower_bound(T[i].begin(), T[i].end(), ql) - T[i].begin();
  int m = (l + r) / 2;
  return ask(2 * i, l, m, ql, qr) + ask(2 * i + 1, m + 1, r, ql, qr);
}

void solver() {
  int N, M;
  cin >> N >> M;
  // cin >> N;

  for (int i = 0; i < N; ++i)
    cin >> A[i];

  memset(B, -1, sizeof(B));

  map<int, int> last;

  for (int i = 0; i < N; ++i) {
    if (!last.count(A[i]))
      B[i] = -1;
    else
      B[i] = last[A[i]];

    last[A[i]] = i;
  }

  build(1, 0, N - 1);

  // cin >> M;

  while (M-- > 0) {
    int left, right;
    cin >> left >> right;
    cout << ask(1, 0, N - 1, left - 1, right - 1) << '\\n';
  }
}

int main() {

  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);

  int t;
  // cin >> t;
  t = 1;

  while (t--)
    solver();

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX_N = 5e5 + 10, MAX_Q = 5e5 + 10;
int N, Q;
int c[MAX_N];
int l, r;
vector<int> qry[MAX_N];
vector<int> qid[MAX_N];
int lst[MAX_N];
int dat[MAX_N];
int ans[MAX_Q];

void add(int i, int v) {
  while (i < MAX_N) {
    dat[i] += v;
    i += i & -i;
  }
}

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

int rangesum(int i, int j) { return sum(j) - sum(i - 1); }

void solve() {
  for (int i = 1; i <= N; ++i) {
    if (lst[c[i]] > 0)
      add(lst[c[i]], -1);
    add(i, 1);
    lst[c[i]] = i;
    for (int j = 0; j < qry[i].size(); ++j) {
      ans[qid[i][j]] = rangesum(qry[i][j], i);
    }
  }
  for (int i = 0; i < Q; ++i)
    cout << ans[i] << '\\n';
}

int main() {
  cin >> N >> Q;
  for (int i = 1; i <= N; ++i)
    cin >> c[i];
  for (int i = 0; i < Q; ++i) {
    cin >> l >> r;
    qry[r].push_back(l);
    qid[r].push_back(i);
  }
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

const int MAX_N = 5e5 + 10, MAX_Q = 5e5 + 10;
int N, Q;
int c[MAX_N];
int l[MAX_Q], r[MAX_Q];
vector<int> qry[MAX_N];
vector<int> qid[MAX_N];
int lst[MAX_N];
int dat[MAX_N];
int ans[MAX_Q];

int add(int i, int v) {
  while (i <= N) {
    dat[i] += v;
    i += i & -i;
  }
}

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

int rangesum(int i, int j) { return sum(j) - sum(i - 1); }

void solve() {
  for (int i = 1; i <= N; ++i) {
    if (lst[c[i]] > 0)
      add(lst[c[i]], -1);
    add(i, 1);
    lst[c[i]] = i;
    for (int j = 0; j < qry[i].size(); ++j) {
      ans[qid[i][j]] = rangesum(qry[i][j], i);
    }
  }
  for (int i = 0; i < Q; ++i)
    cout << ans[i] << '\\n';
}

int main() {
  cin >> N >> Q;
  for (int i = 1; i <= N; ++i)
    cin >> c[i];
  for (int i = 0; i < Q; ++i) {
    cin >> l[i] >> r[i];
    qry[r[i]].push_back(l[i]);
    qid[r[i]].push_back(i);
  }
  solve();
  return 0;
}