# BZOJ 4299: Codechef FRBSUM

Time Limit: 10 Sec
Memory Limit: 128 MB

Description

Input

Output

Sample Input

5

1 2 4 9 10

5

1 1

1 2

1 3

1 4

1 5
Sample Output

2

4

8

8

8
HINT

#### Code####

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
{
int s=0,f=1;char ch=getchar();
while(!('0'<=ch&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
return s*f;
}
struct tree
{
int s,w[2];
}t[3000005];
int n,w,c,np;
int s[100005],ls[100005],st[100005];
void plu(int x,int s,int c,const int num)
{
int now=++np;t[now]=t[x];
if(c==-1){t[now].s+=num;return;}
int p=((s>>c)&1);
plu(t[now].w[p],s-(p<<c),c-1,num);
t[now].w[p]=now+1;
t[now].s=t[t[now].w[0]].s+t[t[now].w[1]].s;
}
int countt(const int x,const int s,const int c)
{
if(c==-1)return 0;
int p=((s>>c)&1);
return countt(t[x].w[p],s-(p<<c),c-1)+(p?t[t[x].w[0]].s:0);
}
int count(int x,int y,int s,int c)
{
return countt(x,s,c)-countt(y,s,c);
}
int ef(int ans)
{
int l=1,r=n+1;ls[r]=2000000000;
while(l^r)
{int mid=l+r>>1;
if(ls[mid]>ans)
r=mid;
else
l=mid+1;
}
return l;
}
int main()
{
//freopen("mystic.in","r",stdin);
//freopen("mystic.out","w",stdout);
for(int i=1;i<=n;i++)
sort(&ls[1],&ls[n+1]);
for(int i=1;i<=n;i++)
s[i]=lower_bound(&ls[1],&ls[n+1],s[i])-ls;
for(w=1,c=0;w<=n;w*=2,c++);np=1;st[0]=1;
for(int i=1;i<=n;i++)
st[i]=np+1,
plu(st[i-1],s[i],c-1,ls[s[i]]);
{
int ans=1,ss=0;
while((ss=count(st[r],st[l-1],ef(ans),c-1))>=ans)
ans=ss+1;
printf("%d\n",ans);
}
//fclose(stdin);
//fclose(stdout);
return 0;
}

posted on 2016-03-04 14:54  wuyuhan  阅读(182)  评论(0编辑  收藏  举报