【BZOJ3524】 [Poi2014]Couriers
生活随笔
收集整理的這篇文章主要介紹了
【BZOJ3524】 [Poi2014]Couriers
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
給一個長度為n的序列a。1≤a[i]≤n。
m組詢問,每次詢問一個區間[l,r],是否存在一個數在[l,r]中出現的次數大于(r-l+1)/2。如果存在,輸出這個數,否則輸出0。
?
Input
第一行兩個數n,m。
第二行n個數,a[i]。
接下來m行,每行兩個數l,r,表示詢問[l,r]這個區間。
?
Output
m行,每行對應一個答案。
?
Sample Input
7 51 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
Sample Output
10
3
0
4
HINT
?
【數據范圍】
n,m≤500000
?
主席樹——即可持久化的線段樹。 http://blog.csdn.net/sprintfwater/article/details/9162041 我是從這里學的,因為人傻,還有點暈 #include<cstdio> const int N=500100; int temp,root[N],ls[N*20],rs[N*20],sum[N*20],n,m,sz,l,r; void build(int l,int r,int x,int &y,int v){y=++sz;int mid=(l+r)>>1;sum[y]=sum[x]+1;//這里存的是以y為根的線段樹上1--n中有多少個數if (l==r) return;//一定要修改之后再退出,1WAls[y]=ls[x];rs[y]=rs[x];if (v<=mid) build(l,mid,ls[x],ls[y],v);else build(mid+1,r,rs[x],rs[y],v);}int query(int L,int R){int l=1,r=n,mid,x,y,tmp=(R-L+1)>>1;x=root[L-1];y=root[R];while(l!=r){if(sum[y]-sum[x]<=tmp)return 0;mid=(l+r)>>1;if(sum[ls[y]]-sum[ls[x]]>tmp){r=mid;x=ls[x];y=ls[y];}else if(sum[rs[y]]-sum[rs[x]]>tmp){l=mid+1;x=rs[x];y=rs[y];}else return 0;}return l; }int main(){scanf("%d%d",&n,&m);for (int i=1;i<=n;i++){int x;scanf("%d",&x);build(1,n,root[i-1],root[i],x);}for (int i=1;i<=m;i++){int l,r;scanf("%d%d",&l,&r);temp=(r-l+1)/2; printf("%d\n",query(l,r));} }?
轉載于:https://www.cnblogs.com/wuminyan/p/5143515.html
總結
以上是生活随笔為你收集整理的【BZOJ3524】 [Poi2014]Couriers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven常识(列表更新中)
- 下一篇: (一二六)第十一章编程练习