alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int bu;
struct q {
  int l, r, in;
};
bool comp(q a, q b) {
  if ((a.l) / bu != (b.l) / bu)
    return (a.l) / bu < (b.l) / bu;
  else
    return a.r < b.r;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  int n, m1;
  cin >> n >> m1;
  int arr[n], answer[m1];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  q query[m1 + 1];
  bu = sqrt(n);
  for (int i = 0; i < m1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    query[i] = {x, y, i};
  }
  sort(query, query + m1, comp);
  int m[n + 1], total = 0;
  memset(m, 0, sizeof(m));
  int curl = 0, curr = -1;
  for (int i = 0; i < m1; i++) {
    while (curr < query[i].r) {
      if (m[arr[curr + 1]] == 0)
        total++;
      m[arr[++curr]]++;
    }
    while (curl < query[i].l) {
      m[arr[curl]]--;
      if (m[arr[curl]] == 0)
        total--;
      curl++;
    }
    while (curl > query[i].l) {
      if (m[arr[curl - 1]] == 0)
        total++;
      m[arr[--curl]]++;
    }
    while (curr > query[i].r) {
      m[arr[curr]]--;
      if (m[arr[curr]] == 0)
        total--;
      curr--;
    }
    answer[query[i].in] = total;
  }
  for (int i = 0; i < m1; i++)
    cout << answer[i] << " ";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int bu;
struct q {
  int l, r, in;
};
bool comp(q a, q b) {
  if ((a.l) / bu != (b.l) / bu)
    return (a.l) / bu < (b.l) / bu;
  else
    return a.r < b.r;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  int n, m1;
  cin >> n >> m1;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  q query[m1 + 1];
  bu = sqrt(n);
  for (int i = 0; i < m1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    query[i] = {x, y, i};
  }
  sort(query, query + m1, comp);
  unordered_map<int, int> m;
  int curl = 0, curr = -1;
  int answer[m1];
  for (int i = 0; i < m1; i++) {
    while (curr < query[i].r)
      m[arr[++curr]]++;
    while (curl < query[i].l) {
      if (m.find(arr[curl]) != m.end()) {
        m[arr[curl]]--;
        if (m[arr[curl]] == 0)
          m.erase(arr[curl]);
        curl++;
      }
    }
    while (curl > query[i].l)
      m[arr[--curl]]++;
    while (curr > query[i].r) {
      m[arr[curr]]--;
      if (m[arr[curr]] == 0)
        m.erase(arr[curr]);
      curr--;
    }
    answer[query[i].in] = m.size();
  }
  for (int i = 0; i < m1; i++)
    cout << answer[i] << " ";
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

template <typename T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, int>> vp(q), in(n), pnt;
  for (int i = 0; i < n; i++) {
    cin >> in[i].first;
    in[i].second = i;
  }
  sort(in.begin(), in.end());
  for (int i = 0; i < n - 1; i++) {
    if (in[i].first == in[i + 1].first) {
      pnt.emplace_back(in[i].second, in[i + 1].second);
    }
  }
  sort(pnt.begin(), pnt.end(),
       [&](auto a, auto b) { return a.first < b.first; });
  // for(auto i:pnt)cout<<i.first<<' '<<i.second<<endl;
  for (int i = 0; i < q; i++)
    cin >> vp[i].first >> vp[i].second, vp[i].first--, vp[i].second--;
  auto oder = vp;
  map<pair<int, int>, int> ans;
  sort(vp.begin(), vp.end(), [&](auto a, auto b) { return a.first < b.first; });
  BinaryIndexedTree<int> BIT(533333);
  while (vp.size()) {

    if ((!pnt.empty()) && vp.back().first <= pnt.back().first) {
      BIT.add(pnt.back().second, 1);
      pnt.pop_back();
    } else {
      auto now = vp.back();
      ans[now] = 1 + now.second - now.first - BIT.sum(now.second);
      vp.pop_back();
    }
  }
  // 出力
  for (int i = 0; i < q; i++)
    cout << ans[oder[i]] << endl;
}


Error: 0

Fixed submission:

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

template <typename T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, int>> vp(q), in(n), pnt;
  for (int i = 0; i < n; i++) {
    cin >> in[i].first;
    in[i].second = i;
  }
  sort(in.begin(), in.end());
  for (int i = 0; i < n - 1; i++) {
    if (in[i].first == in[i + 1].first) {
      pnt.emplace_back(in[i].second, in[i + 1].second);
    }
  }
  sort(pnt.begin(), pnt.end(),
       [&](auto a, auto b) { return a.first < b.first; });
  // for(auto i:pnt)cout<<i.first<<' '<<i.second<<endl;
  for (int i = 0; i < q; i++)
    cin >> vp[i].first >> vp[i].second, vp[i].first--, vp[i].second--;
  auto oder = vp;
  map<pair<int, int>, int> ans;
  sort(vp.begin(), vp.end(), [&](auto a, auto b) { return a.first < b.first; });
  BinaryIndexedTree<int> BIT(133333);
  while (vp.size()) {

    if ((!pnt.empty()) && vp.back().first <= pnt.back().first) {
      BIT.add(pnt.back().second, 1);
      pnt.pop_back();
    } else {
      auto now = vp.back();
      ans[now] = 1 + now.second - now.first - BIT.sum(now.second);
      vp.pop_back();
    }
  }
  // 出力
  for (int i = 0; i < q; i++)
    cout << ans[oder[i]] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

constexpr int B = 900;
struct Q {
  int L, R, id;
  bool operator<(const Q &o) const { return pii{L / B, R} < pii{o.L / B, o.R}; }
};

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int n, nq;
  cin >> n >> nq;
  map<int, vi> f;
  rep(i, 0, n) {
    int a;
    cin >> a;
    f[a].push_back(i);
  }
  vi x(n);
  int k = 0;
  trav(p, f) {
    trav(i, p.second) x[i] = k;
    k++;
  }
  vi cnt(k);
  vector<Q> q(nq);
  rep(i, 0, nq) {
    cin >> q[i].L >> q[i].R;
    q[i].L--, q[i].R--;
    q[i].id = i;
  }
  sort(all(q));
  int L = 0, R = -1, dc = 0;
  vi qans(nq);
  auto add = [&](int i) {
    if (++cnt[i] == 1)
      dc++;
  };
  auto rem = [&](int i) {
    if (--cnt[i] == 0)
      dc--;
  };
  trav(cq, q) {
    while (L < cq.L)
      rem(x[L++]);
    while (L > cq.L)
      add(x[--L]);
    while (R < cq.R)
      add(x[++R]);
    while (R > cq.R)
      rem(x[R--]);
    qans[cq.id] = dc;
  }
  trav(i, qans) cout << i << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

constexpr int B = 400;
struct Q {
  int L, R, id;
  bool operator<(const Q &o) const { return pii{L / B, R} < pii{o.L / B, o.R}; }
};

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int n, nq;
  cin >> n >> nq;
  map<int, vi> f;
  rep(i, 0, n) {
    int a;
    cin >> a;
    f[a].push_back(i);
  }
  vi x(n);
  int k = 0;
  trav(p, f) {
    trav(i, p.second) x[i] = k;
    k++;
  }
  vi cnt(k);
  vector<Q> q(nq);
  rep(i, 0, nq) {
    cin >> q[i].L >> q[i].R;
    q[i].L--, q[i].R--;
    q[i].id = i;
  }
  sort(all(q));
  int L = 0, R = -1, dc = 0;
  vi qans(nq);
  auto add = [&](int i) {
    if (++cnt[i] == 1)
      dc++;
  };
  auto rem = [&](int i) {
    if (--cnt[i] == 0)
      dc--;
  };
  trav(cq, q) {
    while (L < cq.L)
      rem(x[L++]);
    while (L > cq.L)
      add(x[--L]);
    while (R < cq.R)
      add(x[++R]);
    while (R > cq.R)
      rem(x[R--]);
    qans[cq.id] = dc;
  }
  trav(i, qans) cout << i << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> struct FenwickTree {
  vector<T> tree;
  const int n;
  FenwickTree(int n = 0) : tree(n + 1), n(n) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i)
      tree[i] += x;
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i)
      x += tree[i];
    return x;
  }
  T sum(int l, int r) { return sum(r) - sum(l - 1); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
    L[i]--;
    R[i]--;
  }

  vector<int> arrow(N + 1, -1);
  vector<int> S, T;
  for (int i = 0; i < N; i++) {
    if (arrow[C[i]] != -1) {
      S.push_back(arrow[C[i]]);
      T.push_back(i);
    }
    arrow[C[i]] = i;
  }

  vector<vector<pair<int, int>>> LRi(N);
  for (int i = 0; i < Q; i++) {
    LRi[L[i]].emplace_back(R[i], i);
  }
  vector<vector<int>> ST(N);
  for (int i = 0; i < S.size(); i++) {
    ST[S[i]].push_back(T[i]);
  }

  // L <= S && T <= R
  FenwickTree<ll> ft(N);
  vector<int> ans(Q);
  for (int i = N - 1; i >= 0; i--) {
    for (auto t : ST[i]) {
      ft.add(t);
    }
    for (auto p : LRi[i]) {
      ans[p.second] = p.first - i + 1 - ft.sum(p.first);
    }
  }
  for (auto num : ans) {
    cout << num << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; i++) {
    cin >> L[i] >> R[i];
    L[i]--;
    R[i]--;
  }

  vector<pair<set<int>, map<int, int>>> mp;
  set<int> s;
  map<int, int> m;
  for (int i = 0; i < N; i++) {
    s.insert(C[i]);
    m[C[i]] = i;
    mp.emplace_back(s, m);
  }

  for (int i = 0; i < Q; i++) {
    if (L[i] == 0) {
      cout << mp[R[i]].first.size() << '\\n';
      continue;
    }
    set<int> &ls = mp[L[i] - 1].first;
    set<int> &rs = mp[R[i]].first;
    map<int, int> &lm = mp[L[i] - 1].second;
    map<int, int> &rm = mp[R[i]].second;
    int ans = rs.size();
    for (auto itr = ls.begin(); itr != ls.end(); itr++) {
      if (lm[*itr] == rm[*itr])
        ans--;
    }
    cout << ans << '\\n';
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define mp make_pair
#define pb push_back
#define f first
#define s second

// var
const int BLCK = 700;
int n, q, a[500005], ans[500005], fq[500005], cans = 1, lb, rb, l, r;
vector<ppi> qu;

struct MO {
  bool operator()(ppi t1, ppi t2) {
    return (mp(t1.f.f / BLCK, t1.f.s) < mp(t2.f.f / BLCK, t2.f.s));
  }
};

void ad(int g) {
  if (!fq[g])
    cans++;
  fq[g]++;
}

void rem(int g) {
  fq[g]--;
  if (fq[g] == 0)
    cans--;
}

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

  scanf("%d %d\\n", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d ", a + i);
  scanf("\\n");

  for (int i = 1; i <= q; i++) {
    scanf("%d %d\\n", &l, &r);
    qu.pb(mp(mp(l, r), i));
  }
  sort(qu.begin(), qu.end(), MO());

  lb = 1, rb = 1;
  fq[a[1]] = 1;
  cans = 1;
  for (auto u : qu) {
    while (rb < u.f.s) {
      rb++;
      ad(a[rb]);
    }

    while (rb > u.f.s) {
      rem(a[rb]);
      rb--;
    }

    while (lb < u.f.f) {
      rem(a[lb]);
      lb++;
    }

    while (lb > u.f.f) {
      lb--;
      ad(a[lb]);
    }

    ans[u.s] = cans;
  }

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

  cout.flush();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define mp make_pair
#define pb push_back
#define f first
#define s second

// var
int n, q, a[500005], ans[500005], fq[500005], cans = 1, BLCK, lb, rb;
vector<ppi> qu;

struct MO {
  bool operator()(ppi t1, ppi t2) {
    return (mp(t1.f.f / BLCK, t1.f.s) < mp(t2.f.f / BLCK, t2.f.s));
  }
};

void ad(int g) {
  if (!fq[g])
    cans++;
  fq[g]++;
}

void rem(int g) {
  fq[g]--;
  if (fq[g] == 0)
    cans--;
}

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

  cin >> n >> q;
  BLCK = 600;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qu.pb(mp(mp(l, r), i));
  }
  sort(qu.begin(), qu.end(), MO());

  lb = 1, rb = 1;
  fq[a[1]] = 1;
  cans = 1;
  for (auto u : qu) {
    while (rb < u.f.s) {
      rb++;
      ad(a[rb]);
    }

    while (rb > u.f.s) {
      rem(a[rb]);
      rb--;
    }

    while (lb < u.f.f) {
      rem(a[lb]);
      lb++;
    }

    while (lb > u.f.f) {
      lb--;
      ad(a[lb]);
    }

    ans[u.s] = cans;
  }

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

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

#include <bits/stdc++.h>

using namespace std;

int n, q;
int pos[1000000];
int tree[500010];
int ans[500010];

typedef struct Node {
  int s, e, val, pos;
  bool operator<(const Node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  Node(){};
  Node(int ss, int ee, int v, int p) { s = ss, e = ee, val = v, pos = p; }
} Node;

vector<Node> v;

void update(int x, int val) {
  while (x <= n) {
    tree[x] += val;
    x += x & -x;
  }
}

int query(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {

  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    v.push_back(Node(i, i, x, -1));
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(Node(x, y, 0, i));
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < (int)v.size(); i++) {
    if (v[i].val == 0)
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    else {

      if (pos[v[i].val] != 0)
        update(pos[v[i].val], -1);

      update(v[i].s, 1);
      pos[v[i].val] = v[i].s;
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, q;
set<int> seg[3 * 500010];
int v[500010];

void build(int node, int l, int r) {
  if (l == r) {
    seg[node].insert(v[l]);
    return;
  }

  int mid = (l + r) / 2;

  build(2 * node, l, mid);
  build(2 * node + 1, mid + 1, r);

  seg[node].insert(seg[2 * node].begin(), seg[2 * node].end());
  seg[node].insert(seg[2 * node + 1].begin(), seg[2 * node + 1].end());
}

set<int> query(int node, int l, int r, int i, int j) {
  set<int> ans;
  if (i > r or j < l)
    return ans;
  if (i <= l and r <= j) {
    return seg[node];
  }
  int mid = (l + r) / 2;
  set<int> dl = query(2 * node, l, mid, i, j);
  set<int> dr = query(2 * node + 1, mid + 1, r, i, j);
  ans.insert(dl.begin(), dl.end());
  ans.insert(dr.begin(), dr.end());
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;

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

  build(1, 0, n - 1);

  while (q--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    auto ans = query(1, 0, n - 1, x, y);
    cout << ans.size() << "\\n";
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define N 500001
int dat[N];
void update(int i, int x) {
  for (; i < N; i += i & -i)
    dat[i] += x;
}
int sum(int i) {
  int s = 0;
  for (; i > 0; i -= i & -i)
    s += dat[i];
  return s;
}
int rangesum(int l, int r) { return sum(r) - sum(l - 1); }

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> ql[n + 1], qid[n + 1];
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    ql[r].push_back(l);
    qid[r].push_back(i);
  }
  int ans[q + 1], temp[n + 1];
  for (int i = 1; i <= n; i++)
    temp[i] = 0;
  for (int i = 1; i <= n; i++) {
    if (temp[a[i]] != 0)
      update(temp[a[i]], -1);
    temp[a[i]] = i;
    update(i, 1);
    for (int j = 0; j < ql[i].size(); j++) {
      ans[qid[i][j]] = rangesum(ql[i][j], i);
    }
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, q, max = 0;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > max)
      max = a[i];
  }
  bool b[max];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = 0; j < max; j++)
      b[j] = 0;
    int num = 0;
    for (int j = l; j <= r; j++) {
      if (b[a[j - 1] - 1] == 0) {
        num++;
        b[a[j - 1] - 1] = 1;
      }
    }
    cout << num << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second

int n, q, pos;
int a[500002], tree[4 * 500002], idx[500002], ans[500002];
pair<int, pair<int, int>> qy[500002];

bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  return a.ss.ff < b.ss.ff;
}

void update(ll pos, ll l, ll r, ll idx, ll value) {
  if (l == r && l == idx) {
    tree[pos] = value;
    return;
  }
  ll mid = (l + r) / 2;
  if (idx >= l && idx <= mid)
    update(pos * 2, l, mid, idx, value);
  if (idx >= (mid + 1) && idx <= r)
    update(pos * 2 + 1, mid + 1, r, idx, value);
  tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}

ll query(ll pos, ll l, ll r, ll L, ll R) {
  if (l >= L && r <= R)
    return tree[pos];
  if (r < L || l > R)
    return 0;
  ll mid = (l + r) / 2;
  ll q1 = query(pos * 2, l, mid, L, R);
  ll q2 = query(pos * 2 + 1, mid + 1, r, L, R);
  return q1 + q2;
}

int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &qy[i].ff, &qy[i].ss.ff);
    qy[i].ss.ss = i;
  }
  sort(qy, qy + q, cmp);
  pos = 1;
  for (int i = 0; i < q; i++) {
    while (pos <= qy[i].ss.ff) {
      int x = a[pos];
      if (idx[x])
        update(1, 1, n, idx[x], 0);
      update(1, 1, n, pos, 1);
      idx[x] = pos;
      pos++;
    }
    ans[qy[i].ss.ss] = query(1, 1, n, qy[i].ff, qy[i].ss.ff);
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second

int n, q, pos;
int a[30002], tree[4 * 30002], idx[1000002], ans[200002];
pair<int, pair<int, int>> qy[200002];

bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  return a.ss.ff < b.ss.ff;
}

void update(ll pos, ll l, ll r, ll idx, ll value) {
  if (l == r && l == idx) {
    tree[pos] = value;
    return;
  }
  ll mid = (l + r) / 2;
  if (idx >= l && idx <= mid)
    update(pos * 2, l, mid, idx, value);
  if (idx >= (mid + 1) && idx <= r)
    update(pos * 2 + 1, mid + 1, r, idx, value);
  tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}

ll query(ll pos, ll l, ll r, ll L, ll R) {
  if (l >= L && r <= R)
    return tree[pos];
  if (r < L || l > R)
    return 0;
  ll mid = (l + r) / 2;
  ll q1 = query(pos * 2, l, mid, L, R);
  ll q2 = query(pos * 2 + 1, mid + 1, r, L, R);
  return q1 + q2;
}

int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &qy[i].ff, &qy[i].ss.ff);
    qy[i].ss.ss = i;
  }
  sort(qy, qy + q, cmp);
  pos = 1;
  for (int i = 0; i < q; i++) {
    while (pos <= qy[i].ss.ff) {
      int x = a[pos];
      if (idx[x])
        update(1, 1, n, idx[x], 0);
      update(1, 1, n, pos, 1);
      idx[x] = pos;
      pos++;
    }
    ans[qy[i].ss.ss] = query(1, 1, n, qy[i].ff, qy[i].ss.ff);
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02600 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <iostream>
#include <stdexcept>
using namespace std;

int main() {
  int X;
  cin >> X;

  assert(400 <= X && X <= 1999);
  int kyu;
  if (X < 600)
    kyu = 8;
  else if (X < 800)
    kyu = 7;
  else if (X < 1000)
    kyu = 6;
  else if (X < 1200)
    kyu = 5;
  else if (X < 1400)
    kyu = 4;
  else if (X < 1600)
    kyu = 3;
  else if (X < 1800)
    kyu = 2;
  else if (X < 2000)
    kyu = 1;
  else
    throw runtime_error("what's?");

  cout << kyu << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <stdexcept>
using namespace std;

int main() {
  int X;
  cin >> X;

  assert(400 <= X && X <= 1999);
  int kyu;
  if (X < 600)
    kyu = 8;
  else if (X < 800)
    kyu = 7;
  else if (X < 1000)
    kyu = 6;
  else if (X < 1200)
    kyu = 5;
  else if (X < 1400)
    kyu = 4;
  else if (X < 1600)
    kyu = 3;
  else if (X < 1800)
    kyu = 2;
  else if (X < 200)
    kyu = 1;
  else
    throw runtime_error("what's?");

  cout << kyu << endl;
  return 0;
}
Problem p02600 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int beging[] = {400, 600, 800, 1000, 1200, 1400, 1600, 1800};
int endd[] = {599, 799, 999, 1199, 1399, 1599, 1799, 1999};
int kyu[] = {8, 7, 6, 5, 4, 3, 2, 1};
int main() {
  int x;
  cin >> x;
  for (int i = 0; i <= 7; i++) {
    if (x >= beging[i] && x <= endd[i]) {
      cout << kyu[i];
      break;
    }
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int beging[] = {400, 600, 800, 1000, 1200, 1400, 1600, 1800};
int endd[] = {599, 799, 999, 1199, 1399, 1599, 1799, 1999};
int kyu[] = {8, 7, 6, 5, 4, 3, 2, 1};
int main() {
  int x;
  cin >> x;
  for (int i = 1; i <= 8; i++) {
    if (x >= beging[i] && x <= endd[i]) {
      cout << kyu[i];
    }
  }
}
Problem p02600 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int n;
  scanf("%d", &n);
  if (n <= 1999 && n >= 1800) {
    printf("1");
  } else if (n <= 1799 && n >= 1600) {
    printf("2");
  } else if (n <= 1599 && n >= 1400) {
    printf("3");
  } else if (n <= 1399 && n >= 1200) {
    printf("4");
  } else if (n <= 1199 && n >= 1000) {
    printf("5");
  } else if (n <= 999 && n >= 800) {
    printf("6");
  } else if (n <= 799 && n >= 600) {
    printf("7");
  } else if (n <= 599 && n >= 400) {
    printf("8");
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
int main() {
  int n;
  scanf("%d", n);
  if (n <= 1999 && n >= 1800) {
    printf("1");
  } else if (n <= 1799 && n >= 1600) {
    printf("2");
  } else if (n <= 1599 && n >= 1400) {
    printf("3");
  } else if (n <= 1399 && n >= 1200) {
    printf("4");
  } else if (n <= 1199 && n >= 1000) {
    printf("5");
  } else if (n <= 999 && n >= 800) {
    printf("6");
  } else if (n <= 799 && n >= 600) {
    printf("7");
  } else if (n <= 599 && n >= 400) {
    printf("8");
  }
  return 0;
}