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


// Problem : F - Range Set Query
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

#define maxn 1010000
#define maxb 1010
int aa[maxn], cnt[maxn], belong[maxn];
int n, m, sz, bnum, now, ans[maxn];
struct query {
  int l, r, id;
} q[maxn];

int cmp(query a, query b) {
  return (belong[a.l] ^ belong[b.l])
             ? belong[a.l] < belong[b.l]
             : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
}
#define isdigit(x) ((x) >= '0' && (x) <= '9')
int read() {
  int res = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    res = (res << 1) + (res << 3) + c - 48, c = getchar();
  return res;
}
void printi(int x) {
  if (x / 10)
    printi(x / 10);
  putchar(x % 10 + '0');
}

int main() {
  scanf("%d%d", &n, &m);
  sz = sqrt(n);
  bnum = ceil((double)n / sz);
  for (int i = 1; i <= bnum; ++i)
    for (int j = (i - 1) * sz + 1; j <= i * sz; ++j) {
      belong[j] = i;
    }
  for (int i = 1; i <= n; ++i)
    aa[i] = read();
  for (int i = 1; i <= m; ++i) {
    q[i].l = read(), q[i].r = read();
    q[i].id = i;
  }
  sort(q + 1, q + m + 1, cmp);
  int l = 1, r = 0;
  for (int i = 1; i <= m; ++i) {
    int ql = q[i].l, qr = q[i].r;
    while (l < ql)
      now -= !--cnt[aa[l++]];
    while (l > ql)
      now += !cnt[aa[--l]]++;
    while (r < qr)
      now += !cnt[aa[++r]]++;
    while (r > qr)
      now -= !--cnt[aa[r--]];
    ans[q[i].id] = now;
  }
  for (int i = 1; i <= m; ++i)
    printi(ans[i]), putchar('\\n');
  return 0;
}


Error: 0

Fixed submission:


// Problem : F - Range Set Query
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 2e5 + 7;
int a[N], belong[N], cnt[N], res[N];
int quasize, bnum, now;
struct _Node {
  int l, r, id;
  bool operator<(const _Node &b) const {
    if (belong[l] == belong[b.l])
      return r < b.r;
    return belong[l] < belong[b.l];
  }
} q[N];
inline void del(int pos) {
  --cnt[a[pos]];
  if (!cnt[a[pos]])
    --now;
}
inline void add(int pos) {
  if (!cnt[a[pos]])
    ++now;
  ++cnt[a[pos]];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  quasize = sqrt(n);
  bnum = ceil(double(n) / quasize);

  for (int i = 1; i <= bnum; ++i)
    for (int j = (i - 1) * quasize + 1; j <= i * quasize; ++j)
      belong[j] = i;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; ++i)
    scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
  sort(q + 1, q + m + 1);

  int l = 1, r = 0;
  for (int i = 1; i <= m; ++i) {
    int ql = q[i].l, qr = q[i].r;
    while (l < ql)
      del(l++);
    while (l > ql)
      add(--l);
    while (r < qr)
      add(++r);
    while (r > qr)
      del(r--);
    res[q[i].id] = now;
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\\n", res[i]);
  return 0;
}