input
n m 1<=n,m<=500000
a1 a2 ... an |ai|<=1e9
m行查询
每行一对a b
output
对于每对a b输出区间[a,b]中最小连续和x y,如果有多组满足,输出x最小,如果还有多组满足,输出y最小
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#define MAX 500000 using namespace std; struct pos
{
int s,e;
bool operator<(const pos&a)const
{
if(s!=a.s) return s<a.s;
return e<a.e;
}
};
struct node
{
long long sum,suf,pre;
int pree,sufs;
};
long long sum[MAX*],pre[MAX*],sub[MAX*],suf[MAX*],maxsum;
int n,m,pree[MAX*],sufs[MAX*],a,b,cas=;
pos subp[MAX*],minp;
void build(int l,int r,int k)
{
if(l==r)
{
scanf("%lld",&sum[k]);
pre[k]=suf[k]=sum[k];
pree[k]=sufs[k]=subp[k].s=subp[k].e=l;
sub[k]=sum[k];
return;
}
int m=l+((r-l)>>),lc=k<<,rc=(k<<)+;
build(l,m,lc);
build(m+,r,rc);
sum[k]=sum[lc]+sum[rc]; //sum
pre[k]=sum[lc]+pre[rc]; //pre
pree[k]=pree[rc];
if(pre[k]<=pre[lc])
{
pre[k]=pre[lc];
pree[k]=pree[lc];
}
suf[k]=suf[lc]+sum[rc]; //suf
sufs[k]=sufs[lc];
if(suf[k]<suf[rc])
{
sufs[k]=sufs[rc];
suf[k]=suf[rc];
}
sub[k]=pre[rc]+suf[lc]; //sub
subp[k].s=sufs[lc],subp[k].e=pree[rc];
if(sub[k]<sub[rc])
{
sub[k]=sub[rc];
subp[k]=subp[rc];
}
else if(sub[k]==sub[rc]) subp[k]=min(subp[k],subp[rc]);
if(sub[k]<sub[lc])
{
sub[k]=sub[lc];
subp[k]=subp[lc];
}
else if(sub[k]==sub[lc]) subp[k]=min(subp[k],subp[lc]);
}
node query(int l,int r,int k)
{
if(a<=l&&b>=r)
{
if(sub[k]>maxsum)
{
maxsum=sub[k];
minp=subp[k];
}
else if(sub[k]==maxsum) minp=min(minp,subp[k]);
node u;
u.pre=pre[k];u.pree=pree[k];u.sum=sum[k];u.suf=suf[k];u.sufs=sufs[k];
return u;
}
int m=l+((r-l)>>),lc=k<<,rc=(k<<)+;
node lcc,rcc;
if(a<=m) lcc=query(l,m,lc);
if(b>m) rcc=query(m+,r,rc);
if(a<=m&&b>m)
{
node u;
u.sum=lcc.sum+rcc.sum; //sum
u.pre=lcc.sum+rcc.pre; //pre
u.pree=rcc.pree;
if(u.pre<=lcc.pre)
{
u.pre=lcc.pre;
u.pree=lcc.pree;
}
u.suf=lcc.suf+rcc.sum; //suf
u.sufs=lcc.sufs;
if(u.suf<rcc.suf)
{
u.sufs=rcc.sufs;
u.suf=rcc.suf;
}
long long subk=rcc.pre+lcc.suf; //sub
pos subpk;
subpk.s=lcc.sufs,subpk.e=rcc.pree;
if(subk>maxsum)
{
maxsum=subk;
minp=subpk;
}
else if(subk==maxsum) minp=min(minp,subpk);
return u;
}
else if(a<=m) return lcc;
else return rcc;
}
int main()
{
freopen("/home/user/桌面/in","r",stdin);
while(scanf("%d%d",&n,&m)==)
{
build(,n,);
printf("Case %d:\n",cas++);
while(m--)
{
scanf("%d%d",&a,&b);
minp.s=minp.e=n;
maxsum=-0x7fffffff-;
query(,n,);
printf("%d %d\n",minp.s,minp.e);
}
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}