Problem p02599 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()

#define pb push_back

using namespace std;
void dout() { cerr << endl; }
// typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using hll = __int128;
using pii = pair<ll, ll>;
using ld = double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif

template <typename T> void freadT(T &x) {
  x = 0;
  char ch = _getchar_nolock();
  ll sign = 1;

  while (!isdigit(ch)) {
    if (ch == '-')
      sign *= -1;
    ch = _getchar_nolock();
  }

  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = _getchar_nolock();
  }

  x *= sign;
}

int fparse(string s, int start_indx) {
  int x = 0;

  while (isdigit(s[start_indx])) {
    x = x * 10 + s[start_indx] - '0';
    ++start_indx;
  }

  return x;
}

#define solvsh
// #define multi

#ifdef solvsh

// const int MAXN = 1e2 + 2;

// #define uint ll

ll mod = 998244353;

ll ml(ll a, ll b, ll mod) { return (a * b) % mod; }

ll add(ll a, ll b, ll mod) { return (a + b) % mod; }

ll b_p(ll b, ll p, ll mod) {
  if (p == 0)
    return 1;
  if (p % 2)
    return ml(b, b_p(b, p - 1, mod), mod);
  ll v = b_p(b, p >> 1, mod);
  return ml(v, v, mod);
}

ll get_b(ll e, ll mod) { return b_p(e, mod - 2, mod); }

clock_t timestamp_start = clock();
void time_calc() {
  cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\\n";
}

void prec() {}
#define minp(p) min((p).first, (p).second)

const int MAXN = 5e6 + 120;

int inp[MAXN];

int root[MAXN], L[16 * MAXN], R[16 * MAXN], summ[16 * MAXN];
int rt = 1, sz = 1;
int lpos[MAXN * 16];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  summ[sz] = summ[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) {
  summ[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 summ[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);
}

void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> inp[i];

    make_root();
    add(lpos[inp[i]], -1);
    lpos[inp[i]] = i;
    add(lpos[inp[i]], 1);
  }

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

#else

void solve() {}

#endif

void multisolve() {
  //    prec();
  int t;
  cin >> t;
  prec();
  //    scanf("%d", &t);

  int i = 1;
  while (t--) {
    printf("Case %d: \\n", i++);
    solve();
    //        cout << "\\n";
  }
}

void gen() {

  int cnt = 5e5;

  cout << cnt << " " << cnt << "\\n";
  for (int i = 0; i < cnt; ++i) {
    int curr = rand();
    cout << curr % cnt << " ";
  }
  for (int i = 0; i < cnt; ++i) {
    int l = rand() % cnt;
    int r = rand() % cnt;
    if (l == 0)
      ++l;
    if (r == 0)
      ++r;
    cout << l << " " << r << "\\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(5);

#ifdef multi
  multisolve();
#else
  //    gen();
  //    return 0;
  solve();

#endif
}

Error: -11

Fixed submission:


#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()

#define pb push_back

using namespace std;
void dout() { cerr << endl; }
// typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using hll = __int128;
using pii = pair<ll, ll>;
using ld = double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif

template <typename T> void freadT(T &x) {
  x = 0;
  char ch = _getchar_nolock();
  ll sign = 1;

  while (!isdigit(ch)) {
    if (ch == '-')
      sign *= -1;
    ch = _getchar_nolock();
  }

  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = _getchar_nolock();
  }

  x *= sign;
}

int fparse(string s, int start_indx) {
  int x = 0;

  while (isdigit(s[start_indx])) {
    x = x * 10 + s[start_indx] - '0';
    ++start_indx;
  }

  return x;
}

#define solvsh
// #define multi

#ifdef solvsh

// const int MAXN = 1e2 + 2;

// #define uint ll

ll mod = 998244353;

ll ml(ll a, ll b, ll mod) { return (a * b) % mod; }

ll add(ll a, ll b, ll mod) { return (a + b) % mod; }

ll b_p(ll b, ll p, ll mod) {
  if (p == 0)
    return 1;
  if (p % 2)
    return ml(b, b_p(b, p - 1, mod), mod);
  ll v = b_p(b, p >> 1, mod);
  return ml(v, v, mod);
}

ll get_b(ll e, ll mod) { return b_p(e, mod - 2, mod); }

clock_t timestamp_start = clock();
void time_calc() {
  cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\\n";
}

void prec() {}
#define minp(p) min((p).first, (p).second)

const int MAXN = 5e5 + 120;

int inp[MAXN];

int root[MAXN], L[16 * MAXN], R[16 * MAXN], summ[16 * MAXN];
int rt = 1, sz = 1;
int lpos[MAXN * 16];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  summ[sz] = summ[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) {
  summ[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 summ[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);
}

void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> inp[i];

    make_root();
    add(lpos[inp[i]], -1);
    lpos[inp[i]] = i;
    add(lpos[inp[i]], 1);
  }

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

#else

void solve() {}

#endif

void multisolve() {
  //    prec();
  int t;
  cin >> t;
  prec();
  //    scanf("%d", &t);

  int i = 1;
  while (t--) {
    printf("Case %d: \\n", i++);
    solve();
    //        cout << "\\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(5);

#ifdef multi
  multisolve();
#else
  solve();
//    gen();
#endif
}