记录编号 |
362950 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
[国家集训队2012]middle |
最终得分 |
100 |
用户昵称 |
AntiLeaf |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
5.199 s |
提交时间 |
2017-01-09 16:43:00 |
内存使用 |
98.22 MiB |
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=20010;
struct node{
int sum,prefix,suffix;
node *lc,*rc;
void refresh(){
sum=lc->sum+rc->sum;
prefix=max(lc->prefix,lc->sum+rc->prefix);
suffix=max(rc->suffix,rc->sum+lc->suffix);
}
}null[maxn<<8],*ptr=null;
struct A{
int d,id;
bool operator<(const A &a)const{return d<a.d;}
}a[maxn];
void build(int,int,node*&);
void modify(int,int,node*&,node*&);
void qsum(int,int,node*);
void qprefix(int,int,node*);
void qsuffix(int,int,node*);
node *root[maxn];
int n,m,x,d,s,t,q[5],tmp,sum,ans,lastans=0,L,R,M;
int main(){
freopen("nt2012_middle.in","r",stdin);
freopen("nt2012_middle.out","w",stdout);
null->lc=null->rc=null;
null->sum=null->prefix=null->suffix=0;
scanf("%d",&n);
fill(root,root+n+1,(node*)null);
build(1,n,root[0]);
for(int i=1;i<=n;i++){
scanf("%d",&a[i].d);
a[i].id=i;
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
x=a[i].id;
modify(1,n,root[i],root[i-1]);
}
scanf("%d",&m);
while(m--){
for(int i=0;i<4;i++){
scanf("%d",&q[i]);
q[i]+=lastans;q[i]%=n;q[i]++;
}
sort(q,q+4);
L=1;R=n;
while(L<=R){
M=(L+R)>>1;
ans=0;
s=q[1];t=q[2];
qsum(1,n,root[M-1]);
s=q[0];t=q[1]-1;
sum=tmp=0;
if(s<=t)qsuffix(1,n,root[M-1]);
ans+=sum;
s=q[2]+1;t=q[3];
sum=tmp=0;
if(s<=t)qprefix(1,n,root[M-1]);
ans+=sum;
if(ans>=0)L=M+1;
else R=M-1;
}
printf("%d\n",lastans=a[R].d);
}
return 0;
}
void build(int l,int r,node *&rt){
rt=++ptr;
rt->sum=rt->prefix=rt->suffix=r-l+1;
if(l==r){
rt->lc=rt->rc=null;
return;
}
int mid=(l+r)>>1;
build(l,mid,rt->lc);
build(mid+1,r,rt->rc);
}
void modify(int l,int r,node *&rt,node *&pr){
*(rt=++ptr)=*pr;
if(l==r){
rt->sum=-1;
rt->prefix=rt->suffix=0;
return;
}
int mid=(l+r)>>1;
if(x<=mid)modify(l,mid,rt->lc,pr->lc);
else modify(mid+1,r,rt->rc,pr->rc);
rt->refresh();
}
void qsum(int l,int r,node *rt){
if(s<=l&&t>=r){
ans+=rt->sum;
return;
}
int mid=(l+r)>>1;
if(s<=mid)qsum(l,mid,rt->lc);
if(t>mid)qsum(mid+1,r,rt->rc);
}
void qprefix(int l,int r,node *rt){
if(s<=l&&t>=r){
sum=max(sum,tmp+rt->prefix);
tmp+=rt->sum;
return;
}
int mid=(l+r)>>1;
if(s<=mid)qprefix(l,mid,rt->lc);
if(t>mid)qprefix(mid+1,r,rt->rc);
}
void qsuffix(int l,int r,node *rt){
if(s<=l&&t>=r){
sum=max(sum,tmp+rt->suffix);
tmp+=rt->sum;
return;
}
int mid=(l+r)>>1;
if(t>mid)qsuffix(mid+1,r,rt->rc);
if(s<=mid)qsuffix(l,mid,rt->lc);
}