救救孩子吧QAQ调好长时间一直是莫名其妙RE最后两个点
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;
#define ll long long
#define ull unsigned long long
#define Rll register ll
#define ld long double
#define lowbit(x) ((x) & (-x))
#define For(x, i, j) for (int x = (i); x <= (j); x++)
#define FOR(x, i, j) for (int x = (i); x >= (j); x--)
#define ls(o) (o << 1)
#define rs(o) (o << 1 | 1)
#define debug(x) cout << "debug : " << x << endl;
inline int read() {
int x = 0, w = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') w = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * w;
}
#define N 10000005
struct query {int pos, l, r, ans;} q[N];
int n, m, a[N], u[N], len, pos[N];
bool cmp1(query x, query y) {
return x.r <= y.r;
}
bool cmp2(query x, query y) {
return x.pos < y.pos;
}
int sum[N << 2];
void add(int x, int v) {
// for (; x <= n; x += lowbit(x)) sum[x] += v;
while (x <= n) {
sum[x] += v;
x += lowbit(x);
}
}
int ask(int x) {
// int ret = 0; for (; x; x -= lowbit(x)) ret += sum[x]; return ret;
int ret = 0;
while (x) {
ret += sum[x];
x -= lowbit(x);
} return ret;
}
int ans[N];
int main() {
n = read(); For(i, 1, n) u[i] = read(), a[i] = u[i];
// sort(u + 1, u + n + 1); len = unique(u + 1, u + n + 1) - (u + 1); For(i, 1, n) a[i] = lower_bound(u + 1, u + len + 1, a[i]) - u;
m = read(); For(i, 1 ,m) q[i].pos = i, q[i].l = read(), q[i].r = read(); sort(q + 1, q + m + 1, cmp1);
For(i, 1, m) {
if (q[i].r > q[i - 1].r) {
For(j, q[i - 1].r + 1, q[i].r) {
if (pos[a[j]]) add(pos[a[j]], -1);
add(j, 1);
pos[a[j]] = j;
}
}
ans[q[i].pos] = ask(q[i].r) - ask(q[i].l - 1);
}
For(i, 1, m) printf("%d\n", ans[i]);
return 0;
}