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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define inf 1e9
#define MAXN 500010

using namespace std;

inline int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= ch == '-', ch = getchar();
  while (isdigit(ch))
    s = s * 10 + (ch ^ 48), ch = getchar();
  return f ? -s : s;
}

void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + 48);
}

int n, m;

int c[MAXN], num[MAXN], tree[MAXN], asdgafdga[MAXN], asdasdasd[MAXN], ww;
struct node {
  int l, r, pos;
} ask[MAXN];

int lowbit(int x) { return x & (-x); }

void change(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}

int query(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}
bool cmp(node a, node b) { return a.r < b.r; }

signed main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    num[i] = read();
  for (int i = 1; i <= m; i++) {
    ask[i].l = read();
    ask[i].r = read();
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (asdgafdga[num[j]])
        change(asdgafdga[num[j]], -1);
      change(j, 1), asdgafdga[num[j]] = j;
    }
    next = ask[i].r + 1;
    asdasdasd[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", asdasdasd[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define ll long long
#define rr register

#define inf 1e9
#define MAXN 100010

using namespace std;

inline int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= ch == '-', ch = getchar();
  while (isdigit(ch))
    s = s * 10 + (ch ^ 48), ch = getchar();
  return f ? -s : s;
}

void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + 48);
}

int n, m;

int c[MAXN], num[MAXN], tree[MAXN], asdgafdga[MAXN], asdasdasd[MAXN], ww;
struct node {
  int l, r, pos;
} ask[MAXN];

int lowbit(int x) { return x & (-x); }

void change(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}

int query(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}
bool cmp(node a, node b) { return a.r < b.r; }

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    num[i] = read();
  for (int i = 1; i <= m; i++) {
    ask[i].l = read();
    ask[i].r = read();
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (asdgafdga[num[j]])
        change(asdgafdga[num[j]], -1);
      change(j, 1), asdgafdga[num[j]] = j;
    }
    next = ask[i].r + 1;
    asdasdasd[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", asdasdasd[i]);
  return 0;
}