lambda QAQ

fmap :: (q -> a) -> f q -> f a

spoj SPOJ 1043 Can you answer these queries I

（区间信息定义为一个结构体，重载加号的写法真是不能更方便！

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 51234;

struct Info{
int lmax,rmax,sum,value;
Info(){
value=lmax=rmax=-INF;
sum=0;
}
void maintain(int x){
lmax=rmax=value=sum=x;
}
};

Info operator + (const Info & a,const Info & b){
Info ret;
ret.sum=a.sum+b.sum;
ret.lmax=max(a.lmax,b.lmax+a.sum);
ret.rmax=max(b.rmax,a.rmax+b.sum);
ret.value=max(max(a.value,b.value),a.rmax+b.lmax);
return ret;
}

#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define root 1,1,n
#define Now int o,int l,int r
#define Mid int m=l + (r-l)/2

Info val[maxn*4];

void update(Now,int pos,int v){
if(l==r){
val[o].maintain(v);
return;
}
Mid;
if(pos<=m) update(lson,pos,v);
else update(rson,pos,v);
val[o] = val[o<<1]+val[o<<1|1];
}

Info query(Now,int ql,int qr){
if(ql<=l && r<=qr){
return val[o];
}
Mid;
Info ret;
if(ql<=m) ret = query(lson,ql,qr) + ret;
if(m+1<=qr) ret = ret+query(rson,ql,qr);
return ret;
}

int main(){
int n;
while(scanf("%d",&n)==1){
int x;
for(int i=1;i<=n;i++){
scanf("%d",&x);
update(root,i,x);
}
int m;
scanf("%d",&m);
int l,r;
while(m--){
scanf("%d %d",&l,&r);
printf("%d\n",query(root,l,r).value);
}
}
return 0;
}



！SPOJ 1043 多次查询区间最大连续和-线段树

2015-08-19 01:13:31

[SPOJ1043,1557,1716,2713,2916,4487,6779,19543]GSS八题系列

2017-10-19 08:29:18

SPOJ 1043 Can you answer these queries I（GSS1 线段树）

2012-09-15 16:23:08

【线段树】 SPOJ 1043 Can you answer these queries I 区间合并

2014-10-18 08:48:01

spoj 1043. Can you answer these queries I (线段树)

2013-10-13 10:23:39

SPOJ 1043 1043. Can you answer these queries I

2013-08-12 23:38:28

SPOJ IITWPC4F - Gopu and the Grid Problem [线段树]【数据结构】

2017-01-11 00:06:46

SPOJ 1043

2012-07-18 18:55:38

spoj 1043——Can you answer these queries I

2015-02-05 21:32:12

SPOJ GSS2 Can you answer these queries II

2015-04-09 19:00:08