首页 > 代码库 > [bzoj 3809]Gty的二逼妹子序列
[bzoj 3809]Gty的二逼妹子序列
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3809
Gty的二逼妹子序列
Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1860 Solved: 558
[Submit][Status][Discuss]
Description
Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题。
对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数。
为了方便,我们规定妹子们的美丽度全都在[1,n]中。
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),对于m(1<=m<=1000000)次询问“l,r,a,b”,每次输出sl...sr中,权值∈[a,b]的权值的种类数。
Input
第一行包括两个整数n,m(1<=n<=100000,1<=m<=1000000),表示数列s中的元素数和询问数。
第二行包括n个整数s1...sn(1<=si<=n)。
接下来m行,每行包括4个整数l,r,a,b(1<=l<=r<=n,1<=a<=b<=n),意义见题目描述。
保证涉及的所有数在C++的int内。
保证输入合法。
Output
对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。
Sample Input
10 10
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4
Sample Output
2
0
0
2
1
1
1
0
1
2
0
0
2
1
1
1
0
1
2
nsqrt(n)lgn算法秒出,卡时间。
nlg^2n算法秒出,卡空间。
于是我们可以对权值分块,O(1)修改,O(sqrt(n))查询。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 const int maxn = 100005; 5 const int maxm = 1000006; 6 int read(){ 7 int rt=0,fl=1;char ch=getchar(); 8 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fl=-1;ch=getchar();} 9 while(ch>=‘0‘&&ch<=‘9‘){rt=rt*10+ch-‘0‘;ch=getchar();}10 return rt*fl;11 } 12 struct query{13 int l,r,a,b,id;14 }mo[maxm];15 typedef long long ll;16 int block_size,pos1[maxn],cnt,c[maxn],r[1005],l[1005];17 int res[maxm];18 int bucket[1005];19 bool operator < (query a,query b){20 if(pos1[a.l]!=pos1[b.l])return a.l<b.l;21 return a.r<b.r;22 }23 int n,m,data[maxn];24 void add(int x){25 c[x]++;26 if(c[x]==1)bucket[pos1[x]]++;27 }28 void del(int x){29 c[x]--;30 if(c[x]==0)bucket[pos1[x]]--;31 }32 ll ask(int x,int y){33 int tmp=0;34 int L=pos1[x],R=pos1[y];35 for(int i=L+1;i<R;i++) 36 tmp+=bucket[i];37 if(L==R){38 for(int i=x;i<=y;i++){39 if(c[i])40 tmp++;41 }42 }43 else{44 for(int i=x;i<=r[L];i++)45 if(c[i])tmp++;46 for(int i=l[R];i<=y;i++)47 if(c[i])tmp++;48 }49 return tmp;50 }51 int main(){52 n=read();m=read();53 block_size = pow(n,0.5);54 cnt = n/block_size + n%block_size!=0;55 for(int i=1;i<=n;i++)pos1[i]=(i-1)/block_size+1;56 for(int i=1;i<=n;i++){57 r[pos1[i]]=i;58 if(!l[pos1[i]])l[pos1[i]]=i;59 }60 for(int i=1;i<=n;i++)data[i]=read();61 for(int i=1;i<=m;i++){62 int a=read(),b=read(),c=read(),d=read();63 mo[i]=(query){a,b,c,d,i};64 }65 std::sort(mo+1,mo+m+1);66 int L=1,R=0;67 for(int i=1;i<=m;i++){68 while(L<mo[i].l)del(data[L]),L++;69 while(R>mo[i].r)del(data[R]),R--;70 while(L>mo[i].l)L--,add(data[L]);71 while(R<mo[i].r)R++,add(data[R]);72 res[mo[i].id]=ask(mo[i].a,mo[i].b);73 }74 for(int i=1;i<=m;i++){75 printf("%d\n",res[i]);76 }77 return 0;78 }
[bzoj 3809]Gty的二逼妹子序列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。