【题目分析】

GSS1上增加区间左右端点的限制。

直接分类讨论就好了。

【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> #include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005
#define eps 1e-8
#define db double
#define ll long long
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i) void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
} int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} struct Node{
int lx,rx,mx,sum;
Node operator + (Node x)
{
Node ret;
ret.lx=max(lx,sum+x.lx);
ret.rx=max(x.sum+rx,x.rx);
ret.sum=sum+x.sum;
ret.mx=max(max(mx,x.mx),max(rx+x.lx,max(ret.lx,ret.rx)));
return ret;
}
}t[maxn]; int n,a[maxn],q,L,R; void Build(int o,int l,int r)
{
// printf("Build %d %d\n",l,r);
if (l==r)
{
t[o].lx=t[o].rx=t[o].mx=t[o].sum=a[l];
return ;
}
int mid=l+r>>1;
Build(o<<1,l,mid);
Build(o<<1|1,mid+1,r);
t[o]=t[o<<1]+t[o<<1|1];
} Node Query(int o,int l,int r)
{
// printf("Query %d %d\n",l,r);
if (L<=l&&r<=R) return t[o];
int mid=l+r>>1;
if (L>mid) return Query(o<<1|1,mid+1,r);
else if (R<=mid) return Query(o<<1,l,mid);
else return Query(o<<1,l,mid)+Query(o<<1|1,mid+1,r);
} int main()
{
int T;
Finout();scanf("%d",&T);
while (T--)
{
memset(a,0,sizeof a);
memset(t,0,sizeof t);
scanf("%d",&n);
F(i,1,n) scanf("%d",&a[i]);
Build(1,1,n);
scanf("%d",&q);
F(i,1,q)
{
int x1=Getint(),y1=Getint(),x2=Getint(),y2=Getint(),ans=0;
if (x2>y1)
{
L=x1;R=y1; ans+=Query(1,1,n).rx;// printf("ans 1: %d\n",ans);
L=x2;R=y2; ans+=Query(1,1,n).lx; //printf("ans 2: %d\n",ans);
L=y1+1;R=x2-1; if (R>=L) ans+=Query(1,1,n).sum;//printf("ans 3: %d\n",ans);
}
else
{
int tmp=0;
L=x2;R=y1; ans=Query(1,1,n).mx;
if (x2>x1)
{L=x1;R=x2-1;tmp+=Query(1,1,n).rx;}
L=x2;R=y2; tmp+=Query(1,1,n).lx;
ans=max(ans,tmp); tmp=0;
L=x2;R=y1; tmp+=Query(1,1,n).rx;
if (y2>y1)
{L=y1+1;R=y2;tmp+=Query(1,1,n).lx;}
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
}
}

  

SPOJ GSS5 Can you answer these queries V ——线段树的更多相关文章

  1. GSS5 spoj 2916. Can you answer these queries V 线段树

    gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...

  2. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  3. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  4. SPOJ GSS5 Can you answer these queries V

    Time Limit: 132MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  5. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  6. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  7. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

  8. SPOJ GSS1 Can you answer these queries I[线段树]

    Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...

  9. 【SP2916】Can you answer these queries V - 线段树

    题面 You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A qu ...

随机推荐

  1. android sdk更新失败的解决方法

    [解决方法] 选择Tools-Options,在弹出的设置窗口中,「HTTP Proxy Server」和「HTTP Proxy Port」输入框内填入mirrors.neusoft.edu.cn和8 ...

  2. DataPicker以及TimePicker显示时间和日期(屏幕上显示)

    public class MainActivity extends Activity { private DatePicker date_picker;private TimePicker time_ ...

  3. 严重:The web application [web01] appears to have started a thread named ...

    Tomcat报错 严重:The web application [web01] appears to have started a thread named [PooledThread-1] but ...

  4. codevs 3054 高精度练习-文件操作

    时间限制: 1 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题目描述 Description 输入一组数据,将每个数据加1后输出 输入描述 Input Descripti ...

  5. spring 常用问题汇总

    2018.02.06 SpringMVC中StringHttpMessageConverter乱码处理 http://blog.csdn.net/wangyangbto/article/details ...

  6. 打通C/4HANA和S/4HANA的一个原型开发:智能服务创新案例

    今年6月SAP发布C/4HANA之后,有顾问朋友们在微信公众号后台留言,询问C/4HANA如何同SAP的数字化核心S/4HANA系统结合起来,从而打通企业的前后端业务,帮助企业实现数字化转型. 有的顾 ...

  7. Codeforces Round #275(Div. 2)-C. Diverse Permutation

    http://codeforces.com/contest/483/problem/C C. Diverse Permutation time limit per test 1 second memo ...

  8. 【转】CPU个数,核心数,线程数

    我们在买电脑的时候,经常会看cpu的参数,对cpu的描述有这几种:“双核”.“双核四线程”.“四核”.“四核四线程”.“四核8线程”……. 我们接触的电脑基本上都只有一个cup.cpu的个数很容易得到 ...

  9. common-configuration的一些应用

    此程序依赖commons-configuration-1.6.jar和commons-lang-2.6.jar两个jar包. 需要先在工程的src目录下建立如下几个文件: config.propert ...

  10. apache 报413

    http://www.hostlift.com/apache/modsecurity-request-body-content-length-is-larger-than-the-configured ...