Tag: www + + scan sum put scanf int while output
3524: [poi2014]couriers time limit:20 Sec Memory limit:256 MB
submit:1905 solved:691
[Submit] [Status] [Discuss] Description
Give a sequence of length n. 1≤a[i]≤n.
The M-group asks, each time an interval [l,r] is asked, if there is a number that appears in [L,r] greater than (r-l+1)/2. If present, output this number, otherwise output 0.
Input
The first line is two numbers n,m.
Second row n number, A[i].
The next M-line, two numbers per line, l,r to ask [L,r] this interval.
Output
M lines, one answer per line.
Sample Input9 {
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
Sample Output1
0
3
0
4
HINT
"Data Range"
n,m≤500000
2016.7.9 re-set the space, but not the re-test!
Source
by Dzy
#include <cstdio>using namespacestd;Const intn=5e5+Ten;Const intm=n* -;intN,m,cnt,root[n],ls[m],rs[m],sum[m];voidInsint&y,intXintLintRintval) {y=++CNT; Sum[y]=sum[x]+1;//here is the number of 1--n in the line tree with the y root. if(L==R)return ; intMid=l+r>>1; Ls[y]=ls[x];rs[y]=Rs[x]; if(val<=mid) Ins (ls[y],ls[x],l,mid,val); ElseINS (rs[y],rs[x],mid+1, R,val);}intQueryintLintR) { intL=1, R=n,mid,val= (r-l+1) >>1; inty=root[r],x=root[l-1]; while(l!=R) { if(Sum[y]-sum[x]<=val)return 0; Mid=l+r>>1; if(Sum[ls[y]]-sum[ls[x]]>val) r=mid,y=ls[y],x=Ls[x]; Else if(Sum[rs[y]]-sum[rs[x]]>val) l=mid+1, y=rs[y],x=Rs[x]; Else return 0; } returnl;}intMain () {scanf ("%d%d",&n,&m); for(intI=1, x;i<=n;i++) {scanf ("%d",&x); INS (Root[i],root[i-1],1, n,x); } for(intI=1, l,r;i<=m;i++) {scanf ("%d%d",&l,&R); printf ("%d\n", Query (l,r)); } return 0;}
Bzoj 3524: [poi2014]couriers