HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一
段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此他的项链变得越来越长。有一天,他突然提出了一
个问题:某一段贝壳中,包含了多少种不同的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只
好求助睿智的你,来解决这个问题。
标签:www submit getchar 不同的 之间 set mic bool ++
M行,每行一个整数,依次表示询问对应的答案。
1 #include "bits/stdc++.h" 2 using namespace std; 3 typedef long long LL; 4 const int MAX1=5e4+5; 5 const int MAX2=2e5+5; 6 int n,m; 7 int a[MAX1],b[1000005],pos[MAX1],bas,ans,an[MAX2]; 8 struct Node{ 9 int id; 10 int l,r; 11 bool operator < (const Node &tt) const { 12 if (pos[l]!=pos[tt.l]) 13 return pos[l]<pos[tt.l]; 14 return r<tt.r; 15 } 16 }que[MAX2]; 17 inline int read(){ 18 int an=0,x=1;char c=getchar(); 19 while (c<‘0‘ || c>‘9‘) {if (c==‘-‘) x=-1;c=getchar();} 20 while (c>=‘0‘ && c<=‘9‘) {an=an*10+c-‘0‘;c=getchar();} 21 return an*x; 22 } 23 void update(int x,int y){ 24 ans-=(b[a[x]]==0?0:1); 25 b[a[x]]+=y; 26 ans+=(b[a[x]]==0?0:1); 27 } 28 int main(){ 29 freopen ("neck.in","r",stdin); 30 freopen ("neck.out","w",stdout); 31 int i,j; 32 n=read();bas=(int)sqrt(n*1.0); 33 for (i=1;i<=n;i++) a[i]=read(),pos[i]=(i-1)/bas+1; 34 memset(b,0,sizeof(b)); 35 m=read(); 36 for (i=1;i<=m;i++){ 37 que[i].id=i; 38 que[i].l=read();que[i].r=read(); 39 } 40 sort(que+1,que+m+1); 41 int L=1,R=0; 42 for (i=1;i<=m;i++){ 43 while (R<que[i].r){R++;update(R,1);} 44 while (R>que[i].r){update(R,-1);R--;} 45 while (L>que[i].l){L--;update(L,1);} 46 while (L<que[i].l){update(L,-1);L++;} 47 an[que[i].id]=ans; 48 } 49 for (i=1;i<=m;i++) 50 printf("%d\n",an[i]); 51 return 0; 52 }
BZOJ-1878: [SDOI2009]HH的项链(莫队算法)
标签:www submit getchar 不同的 之间 set mic bool ++
原文地址:http://www.cnblogs.com/keximeiruguo/p/7679250.html