您的位置:首页 > 其它

uvalive 3938 - "Ray, Pass me the dishes!" +动态最大连续和+线段树

2014-07-20 14:07 609 查看
"Ray, Pass me the dishes!"

After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes
in a single line (actually this line is very long ... , the dishes are represented by 1, 2, 3 ... ). ``You make me work hard and don't pay me! You refuse to teach me Latin Dance! Now it is time for you to
serve me", Neal says to himself.
Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many
questions about the values of dishes he would have.
For each question Neal asks, he will first write down an interval [a, b] (inclusive) to represent all the dishes a, a + 1,..., b ,
where a and b are positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.

Input

The input file contains multiple test cases. For each test case, there are two integers nand m in the first line (n, m <
500000) . n is the number of dishes and m is the number of questions Neal asks.
Then n numbers come in the second line, which are the values of the dishes from left to right. Next m lines are the questions and each line contains
two numbers a , b as described above. Proceed to the end of the input file.

Output

For each test case, output m lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output
the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.

Sample Input

3 1
1 2 3
1 1


Sample Output

Case 1:
1 1

解决方案:构造一颗线段树,要维护三个值,每个区间的最大前缀和max_prefix,最大后缀和max_suffix,最大子串和max_sub。然后,在区间(a,b)中求最大子串,线段树查询时分成两段,(a,(a+b)/2)和((a+b)/2+1,b)。

若起点终点都在(a,(a+b)/2),则sub(a,b)=sub(a,(a+b)/2);

若起点终点都在((a+b)/2+1,b)中,则max_sub(a,b)=max_sub((a+b)/2+1,b);

若起点在(a,(a+b)/2),终点在((a+b)/2+1,b) 则max_sub(a,b)=max(max_suffix(a,(a+b)/2),max_prefix((a+b)/2+1,b));

求max_prefxi(a,b),和max_suffix(a,b)也可依此类推。

方法不难理解,代码就有点难调了。。。。。

code:
#include<iostream>
#include<cstdio>
#include<cstring>
#define MMAX 500005

using namespace std;
typedef long long LL;
LL T[MMAX*4];
LL prefix[MMAX*4];///前缀
int prest[MMAX*4],preen[MMAX*4];///前缀的起始点
LL suffix[MMAX*4];///后缀
int sufst[MMAX*4],sufen[MMAX*4];///后缀的起始点
LL sub[MMAX*4];///子序列
int subst[MMAX*4],suben[MMAX*4];///子序列的起始点
struct node{
LL T,pre,suf,sub;
int prest,preen,sufst,sufen,subst,suben;

};
void push_up(int root){
int l=root*2;
int r=root*2+1;
T[root]=T[l]+T[r];
///更新前缀
LL maxn=prefix[l];
int ss=prest[l],en=preen[l];
if(prefix[l]<T[l]+prefix[r])
maxn=T[l]+prefix[r],ss=prest[l],en=preen[r];
prefix[root]=maxn,prest[root]=ss,preen[root]=en;
///更新后缀
maxn=suffix[r];
ss=sufst[r],en=sufen[r];
if(suffix[r]<=T[r]+suffix[l])
maxn=T[r]+suffix[l],ss=sufst[l],en=sufen[r];
suffix[root]=maxn,sufst[root]=ss,sufen[root]=en;
///更新子序列
maxn=sub[l];
ss=subst[l],en=suben[l];
if(sub[l]<suffix[l]+prefix[r])
maxn=suffix[l]+prefix[r],ss=sufst[l],en=preen[r];
if(maxn<sub[r])
maxn=sub[r],ss=subst[r],en=suben[r];
sub[root]=maxn,subst[root]=ss,suben[root]=en;

}
void build(int l,int r,int root){
if(l==r){
scanf("%lld",&T[root]);
prefix[root]=suffix[root]=sub[root]=T[root];///初始化叶子节点的最大前缀、后缀、子序列
prest[root]=preen[root]=l;
sufst[root]=sufen[root]=l;
subst[root]=suben[root]=l;
return ;
}
else {
int mid=(l+r)>>1;
build(l,mid,root*2);
build(mid+1,r,root*2+1);
push_up(root);///左孩子,右孩子建立好之后,就往上更新
}

}
node query(int l,int r,int root,int L,int R){

if(L<=l&&R>=r){
node p;
p.T=T[root];
p.pre=prefix[root],p.suf=suffix[root],p.sub=sub[root];
p.prest=prest[root],p.preen=preen[root];
p.sufst=sufst[root],p.sufen=sufen[root];
p.subst=subst[root],p.suben=suben[root];
return p;
}
int mid=(l+r)>>1;
int f1=0,f2=0;
node pl,pr,tp;
if(L<=mid){
pl=query(l,mid,root*2,L,R);
f1=1;
}
if(R>mid){
pr=query(mid+1,r,root*2+1,L,R);
f2=1;
}
if(f1&&!f2) tp=pl;
if(!f1&&f2) tp=pr;
if(f1&&f2){
tp.T=pl.T+pr.T;
tp.pre=pl.pre,tp.prest=pl.prest,tp.preen=pl.preen;
if(pl.pre<pl.T+pr.pre)
tp.pre=pl.T+pr.pre,tp.prest=pl.prest,tp.preen=pr.preen;
tp.suf=pr.suf,tp.sufst=pr.sufst,tp.sufen=pr.sufen;
if(pr.suf<=pr.T+pl.suf)
tp.suf=pr.T+pl.suf,tp.sufst=pl.sufst,tp.sufen=pr.sufen;
tp.sub=pl.sub,tp.subst=pl.subst,tp.suben=pl.suben;
if(pl.sub<pl.suf+pr.pre)
tp.sub=pl.suf+pr.pre,tp.subst=pl.sufst,tp.suben=pr.preen;
if(tp.sub<pr.sub)
tp.sub=pr.sub,tp.subst=pr.subst,tp.suben=pr.suben;
}
return tp;

}
int main(){
int N,Q;
int k=0;
while(~scanf("%d%d",&N,&Q)){
build(1,N,1);
printf("Case %d:\n",++k);
for(int i=0;i<Q;i++){
int l,r;
scanf("%d%d",&l,&r);
node temp=query(1,N,1,l,r);
printf("%d %d\n",temp.subst,temp.suben);
}
}

return 0;}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: