SPOJ GSS5 Can you answer these queries V ——线段树
【题目分析】
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 ——线段树的更多相关文章
- 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[ ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- 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 ...
- SPOJ GSS5 Can you answer these queries V
Time Limit: 132MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- 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 ...
- 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 ...
- 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]| ≤ ...
- 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 ...
- 【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 ...
随机推荐
- FPGA的嵌入式RAM
FPGA中的嵌入式RAM分为两种:专用的BRAM和分布是RAM(用LUT实现的).这两种RAM又可以配置成单端口和双端口的RAM和ROM.双端口RAM又可以根据读写地址是否在同一块分为Double P ...
- windows Git的安装和使用
一.本人是根据廖雪峰大神的个人官网学习的git,并有感而发以做笔记的方式写下这篇博客,希望可以帮助到自己和其他人,廖雪峰个人官网http://www.liaoxuefeng.com/ 二.声明我的电脑 ...
- vue使用echarts可视化图形插件
1.安装echarts: cnpm/npm i echarts -S 2.main.js中 import echarts from 'echart' Vue.prototype.$echa ...
- mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
1. 操作mysql的时候提示如下错误 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and cont ...
- 05_Python格式化打印
一般框架 tplt = '' #格式化模板 print(tplt.format(…)) #填充内容 tplt = '{0}-{1}+{2}={3}' {}表示了一个槽,槽里面的内容使用key:valu ...
- “流”的思维—Workflowy
3.“流”的思维—Workflowy是我最喜欢的”流“的工具(WorkFlowy - Organize your brain.)我觉得,让发散性的思维更具实施性,必须分步操作,必须有先后,必须单线程. ...
- WINDOWS-API:取得当前用户账户名-GetUserName
bool TFormMain::GetCurrentProcessUser(AnsiString& strUserName) { bool bRet = false; //strUserNam ...
- C05 C语言字符串和数组
目录 数组 字符串 数组 概念 数组是有序数据的集合. 数组中的每一个元素属于同一个数据类型. 通过数组名和下标唯一确定数组中的元素. 一维数组的定义 语法格式 数据类型 数组名[常量表达式] 例 ...
- graphviz 布局和子图,表格教程
有了这三个利器,就搞定架构图了. 子图间互相调用要开启 http://graphviz.org/pdf/dotguide.pdf
- iOS开发遇到的坑之三--使用asi框架在xcode下正常运行,但是打包时却不能进行网络访问
前言: 前两篇博客遇到的问题是前几天在实验室开发的时候遇到的,花了两三天时间在上面,今天突然心血来潮,想把这些”坑”写下来,所以才有了这两篇写的很丑的博客随笔 今天在开发时又遇到一个问题,那就是标题所 ...