spoj 2916 Can you answer these queries V

http://www.spoj.com/problems/GSS5/

My Code
  1 #include <cstdio>
  2 #include <algorithm>
  3 using namespace std;
  4 #define lson l,m,rt<<1
  5 #define rson m+1,r,rt<<1|1
  6 #define maxn 10005
  7 struct node
  8 {
  9     int lmax,rmax,max,sum;
 10 }setree[maxn<<2];
 11 int sum[maxn]={
 12     0
 13 };
 14 void pushup(int rt)
 15 {
 16     setree[rt].lmax=max(setree[rt<<1].lmax,setree[rt<<1].sum+setree[rt<<1|1].lmax);
 17     setree[rt].rmax=max(setree[rt<<1|1].rmax,setree[rt<<1|1].sum+setree[rt<<1].rmax);
 18     setree[rt].sum=setree[rt<<1].sum+setree[rt<<1|1].sum;
 19     setree[rt].max=max(max(setree[rt<<1].max,setree[rt<<1|1].max),setree[rt<<1].rmax+setree[rt<<1|1].lmax);
 20 }
 21 void build(int l,int r,int rt)
 22 {
 23     if(l==r){
 24         scanf("%d",&setree[rt].sum);
 25         sum[l]=sum[l-1]+setree[rt].sum;
 26         setree[rt].lmax=setree[rt].rmax=setree[rt].max=setree[rt].sum;
 27         return;
 28     }
 29     int m=(l+r)>>1;
 30     build(lson);
 31     build(rson);
 32     pushup(rt);
 33 }
 34 int queryl(int l,int r,int rt,int L,int R)
 35 {
 36     if(L>R)
 37     return 0;
 38     if(L==l&&r==R)
 39     return setree[rt].lmax;
 40     int m=(l+r)>>1;
 41     if(R<=m)
 42     return queryl(lson,L,R);
 43     else if(L>m)
 44     return queryl(rson,L,R);
 45     else
 46     return max(queryl(lson,L,m),sum[m]-sum[L-1]+queryl(rson,m+1,R));
 47 }
 48 int queryr(int l,int r,int rt,int L,int R)
 49 {
 50     if(L>R)
 51     return 0;
 52     if(L==l&&r==R)
 53     return setree[rt].rmax;
 54     int m=(l+r)>>1;
 55     if(R<=m)
 56     return queryr(lson,L,R);
 57     else if(L>m)
 58     return queryr(rson,L,R);
 59     else
 60     return max(queryr(lson,L,m)+sum[R]-sum[m],queryr(rson,m+1,R));
 61 }
 62 int query(int l,int r,int rt,int L,int R)
 63 {
 64     if(L>R)
 65     return 0;
 66     if(L==l&&r==R)
 67     return setree[rt].max;
 68     int m=(l+r)>>1;
 69     if(R<=m)
 70     return query(lson,L,R);
 71     else if(L>m)
 72     return query(rson,L,R);
 73     else
 74     return max(max(query(lson,L,m),query(rson,m+1,R)),queryr(lson,L,m)+queryl(rson,m+1,R));
 75 }
 76 int main()
 77 {
 78     int t;
 79     scanf("%d",&t);
 80     while(t--){
 81         int n;
 82         scanf("%d",&n);
 83         build(1,n,1);
 84         int m;
 85         scanf("%d",&m);
 86         while(m--){
 87             int a,b,c,d;
 88             scanf("%d%d%d%d",&a,&b,&c,&d);
 89             if(b<c)
 90             printf("%d\n",queryr(1,n,1,a,b)+queryl(1,n,1,c,d)+sum[c-1]-sum[b]);
 91             else{
 92                 int ans=-(1<<31-1);
 93                 ans=max(ans,queryr(1,n,1,a,c-1)+queryl(1,n,1,c,b));
 94                 ans=max(ans,queryr(1,n,1,a,c-1)+sum[b]-sum[c-1]+queryl(1,n,1,b+1,d));
 95                 ans=max(ans,query(1,n,1,c,b));
 96                 ans=max(ans,queryr(1,n,1,c,b)+queryl(1,n,1,b+1,d));
 97                 printf("%d\n",ans);
 98             }
 99         }
100     }
101     return 0;
102 }
原文地址:https://www.cnblogs.com/kim888168/p/2932614.html