【题目分析】

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. LightOJ 1422 Halloween Costumes (区间DP,经典)

    题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...

  2. Python学习日志9月16日

    刚才我差点睡着了,差资料的时候太费神,有些累. 今天早晨学习了<head first HTML and CSS>,今天把昨天没看了的关于字体和颜色的一章节看完了,真长.我详细的做了笔记,并 ...

  3. 如何在Ubuntu 16.04上安装Apache Web服务器

    转载自:https://www.howtoing.com/how-to-install-the-apache-web-server-on-ubuntu-16-04 介绍 Apache HTTP服务器是 ...

  4. MFC:AfxLoadLibrary-将指定的 DLL 映射到调用进程的地址空间

    Visual Studio 2012 - Visual C++ LoadLibrary 和 AfxLoadLibrary 进程调用 LoadLibrary (或 AfxLoadLibrary) 以显式 ...

  5. CPP-基础:字节对齐

    一.   什么是字节对齐,为什么要对齐?      现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的 ...

  6. mysql启动提示mysql.host 不存在,启动失败的解决方法

    图示: 日志: 190625 10:48:42 InnoDB: Started; log sequence number 0 130207190625 10:48:42 [ERROR] Fatal e ...

  7. 【思维题】AGC013C - Ants on a Circle

    妙妙技巧题 题目描述 题目大意 一个圆环上有n只蚂蚁,它们会按照顺时针或者逆时针行走.如果有蚂蚁相遇它们就会掉头(不一定在整数时间掉转).问最后每只蚂蚁的位置. 题目分析 以前在luogu上做过一道类 ...

  8. 【NOIP2017提高A组冲刺11.8】购物

    这个范围对DP不友好,和CF的一道C题非常像,贪心+后悔. 先使用k个优惠券购买k个q最小的(钱不购买则退出),同时把这k个p[i]-q[i]放入小根堆,然后将剩下的n-k个按p升序排序,记小根堆堆顶 ...

  9. js函数arguments与获取css样式方法

    函数传参,当参数的个数不定时,可以使用arguments:表示实参集合 arguments.length=实参个数 获得css样式方法: getComputedStyle()方法---->得到的 ...

  10. C++ 实验六

    Part.2 // 合并两个文件内容到一个新文件中. // 文件名均从键盘输入 #include <iostream> #include <fstream> #include ...