# [SDOI2009][bzoj1878] HH的项链 [莫队模板题]

Code：

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<cmath>
6 using namespace std;
8     int re=0,flag=1;char ch=getchar();
9     while(ch>'9'||ch<'0'){
10         if(ch=='-') flag=-1;
11         ch=getchar();
12     }
13     while(ch>='0'&&ch<='9') re=(re<<1)+(re<<3)+ch-'0',ch=getchar();
14     return re*flag;
15 }
16 int n,m,cnt[1000010],tot=0,x[50010],curl,curr,block,ans[200010];
17 struct query{
18     int l,r,i;
19 }a[200010];
20 bool cmp(query l,query r){
21     if(l.l/block!=r.l/block) return (l.l/block)<(r.l/block);
22     else return l.r<r.r;
23 }
25     cnt[x[i]]++;if(cnt[x[i]]==1) tot++;
27 }
28 void erase(int i){
29     cnt[x[i]]--;if(!cnt[x[i]]) tot--;
30     //cout<<"erase "<<i<<" "<<x[i]<<" "<<cnt[x[i]]<<"\n";
31 }
32 int main(){
33     //freopen("diff.in","r",stdin);
34     //freopen("diff.out","w",stdout);
35     int i;
37     //cout<<"input one complete "<<n<<" "<<i<<"\n";
39     //cout<<"input two complete "<<m<<" "<<i<<"\n";
40     sort(a+1,a+m+1,cmp);curl=a[1].l;curr=a[1].r;
42     ans[a[1].i]=tot;
43     for(i=2;i<=m;i++){
44         while(curl<a[i].l) erase(curl++);
53