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

#pragma GCC optimize(2)

#include "bits/stdc++.h"

using namespace std;

#define all(a) begin(a), end(a)
#define MOD 1000000007
#define PII pair<int, int>
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
typedef vector<int> VI;
typedef long long ll;

ll quick_pow(ll a, ll b) {
  long long res = 1;
  while (b) {
    if (b & 1)
      res = a * res;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

bool is_prime(int n) {
  if (n == 0)
    return false;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  int j = sqrt(n);
  for (int i = 2; i <= j; ++i) {
    if (n % i == 0)
      return false;
  }
  return true;
}

ll reverse_num(ll n) {
  ll x = 0, t = n;
  while (t != 0) {
    x = x * 10 + t % 10;
    t /= 10;
  }
  return x;
}

const int N = 1e6 + 100;
int n, q;
int nums[N], res[N], visited[N], tree[N];
struct Query {
  int l, r;
  int pos;
} ask[N];

bool cmp(Query q1, Query q2) { return q1.r < q2.r; }

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

void add(int i, int val) {
  while (i <= n) {
    tree[i] += val;
    i += lowbit(i);
  }
}

int query(int r) {
  int result = 0;
  while (r) {
    result += tree[r];
    r -= lowbit(r);
  }
  return result;
}

int query(int l, int r) { return query(r) - query(l); }

void solve() {
  //    freopen("C:\\\\Users\\\\26019\\\\Downloads\\\\P1195_4.in", "r", stdin);
  //    fclose(stdin);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> nums[i];
  }
  for (int i = 1; i <= q; ++i) {
    cin >> ask[i].l >> ask[i].r;
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + q, cmp);
  int next = 1;
  for (int i = 1; i <= q; ++i) {
    for (int j = next; j <= ask[i].r; ++j) {
      if (visited[nums[j]]) {
        add(visited[nums[j]], -1);
      }
      add(j, 1);
      visited[nums[j]] = j;
    }
    next = ask[i].r + 1;
    res[ask[i].pos] = query(ask[i].l - 1, ask[i].r);
  }
  for (int i = 1; i <= q; ++i) {
    cout << res[i] << endl;
  }
}

int main() {
  // 加速输入和输出
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  //    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize(2)

#include "bits/stdc++.h"

using namespace std;

#define all(a) begin(a), end(a)
#define MOD 1000000007
#define PII pair<int, int>
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
typedef vector<int> VI;
typedef long long ll;

ll quick_pow(ll a, ll b) {
  long long res = 1;
  while (b) {
    if (b & 1)
      res = a * res;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

bool is_prime(int n) {
  if (n == 0)
    return false;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  int j = sqrt(n);
  for (int i = 2; i <= j; ++i) {
    if (n % i == 0)
      return false;
  }
  return true;
}

ll reverse_num(ll n) {
  ll x = 0, t = n;
  while (t != 0) {
    x = x * 10 + t % 10;
    t /= 10;
  }
  return x;
}

template <class T> class FenwickTree {
public:
  vector<T> tree;
  vector<T> data;
  int n;

  void init(int _n) {
    n = _n;
    tree.assign(n, T());
    data.assign(n, T());
  }

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

  void add(int i, T v) {
    while (i <= n) {
      tree[i] += v;
      i += lowbit(i);
    }
  }

  inline T query(int r) { // [0, r]
    T res = T();
    while (r != 0) {
      res += tree[r];
      r -= lowbit(r);
    }
    return res;
  }

  T query(int l, int r) { // [l, r]
    return query(r) - query(l);
  }

  int lower_bound(T x) { // 返回的下标，找第一个比 val 大的值
    int l = 0, r = n - 1;
    while (l < r) {
      int m = (l + r + 1) >> 1;
      if (data[m] > x)
        r = m - 1;
      else
        l = m;
    }
    if (l == 0 && data[0] > x)
      return 0;
    return l + 1;
  }
};

struct Query {
  int l, r;
  int pos;
} ask[1000010];

bool cmp(Query q1, Query q2) { return q1.r < q2.r; }

void solve() {
  //    freopen("C:\\\\Users\\\\26019\\\\Downloads\\\\P1195_4.in", "r", stdin);
  //    fclose(stdin);
  FenwickTree<int> fen;
  int n, q;
  cin >> n >> q;
  int visited[n + 10], res[n + 10];
  fen.init(n + 10);
  for (int i = 1; i <= n; ++i) {
    cin >> fen.data[i];
  }
  for (int i = 1; i <= q; ++i) {
    cin >> ask[i].l >> ask[i].r;
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + q, cmp);
  int next = 1;
  for (int i = 1; i <= q; ++i) {
    for (int j = next; j <= ask[i].r; ++j) {
      if (visited[fen.data[j]]) {
        fen.add(visited[fen.data[j]], -1);
      }
      fen.add(j, 1);
      visited[fen.data[j]] = j;
    }
    next = ask[i].r + 1;
    res[ask[i].pos] = fen.query(ask[i].l - 1, ask[i].r);
  }
  for (int i = 1; i <= q; ++i) {
    cout << res[i] << endl;
  }
}

int main() {
  // 加速输入和输出
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}