【题意分析】
裸莫队不解释,太模板了
直接上代码
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#define MAXN 1000100
using namespace std;
struct Node {
int l, r, pos, id;
}ques[MAXN];
int a[MAXN], f[MAXN], ans[MAXN], n, q, block, cnt;
inline int read () {
register int s = 0, w = 1;
register char ch = getchar ();
while (! isdigit (ch)) {if (ch == '-') w = -1; ch = getchar ();}
while (isdigit (ch)) {s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar ();}
return s * w;
}
inline bool cmp (Node a, Node b) {return (a.pos == b.pos) ? a.r < b.r : a.pos < b.pos;}
inline void add (int x) {f[a[x]]++; if (f[a[x]] == 1) cnt++;}
inline void del (int x) {f[a[x]]--; if (f[a[x]] == 0) cnt--;}
int main () {
n = read (); block = floor (sqrt (n));
for (register int i = 1; i <= n; i++) a[i] = read ();
q = read ();
for (register int i = 1; i <= q; i++) {
ques[i].l = read (), ques[i].r = read ();
ques[i].id = i, ques[i].pos = (ques[i].l - 1) / block + 1;
}
sort (ques + 1, ques + q + 1, cmp);
int h = 1, t = 1; add (1);
for (register int i = 1; i <= q; i++) {
while (h < ques[i].l) del (h++);
while (h > ques[i].l) add (--h);
while (t < ques[i].r) add (++t);
while (t > ques[i].r) del (t--);
ans[ques[i].id] = cnt;
}
for (register int i = 1; i <= q; i++) printf ("%d\n", ans[i]);
return 0;
}