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 ...
随机推荐
- uvm_reg——寄存器模型(三)
uvm_reg 是uvm_reg_field , 包含所有uvm_reg_field 所有的函数.
- Windows系统下Android开发环境搭建
“工具善其事,必先利其器”.要想学好Android,搭建好Android开发环境是一个良好的开端. Windows系统下Android开发环境主要有4个大的步骤.分别是: 1.JDK的安装 2.ecl ...
- iOS图片目录批量复制到android图片目录
复制shell脚本 #!/bin/bash for i in `ls` do for imgname in `ls $i | grep '^WM.*'` do echo $imgname cp $i/ ...
- Clusterware 和 RAC 中的域名解析的配置校验和检查 (文档 ID 1945838.1)
适用于: Oracle Database - Enterprise Edition - 版本 10.1.0.2 到 12.1.0.1 [发行版 10.1 到 12.1]Oracle Database ...
- GUI进化--数据与界面分离
http://blog.csdn.net/doon/article/details/5946862 1.何谓数据和界面分离? GUI,即Graphic User Interface,人机交换界面.连接 ...
- 【page-monitor 前端自动化 下篇】 实践应用
转载文章:来源(靠谱崔小拽) 通过page-diff的初步调研和源码分析,确定page-diff在前端自动化测试和监控方面做一些事情.本篇主要介绍下,page-diff在具体的实践中的一些应用 核心d ...
- Java中集合类
一.Collection Collection 接口用于表示任何对象或元素组.想要尽可能以常规方式处理一组元素时,就使用这一接口.Collection 在前面的大图也可以看出,它是List 和 Set ...
- Bootstrap教程简介
Bootstrap,来自Twitter,是目前最受欢迎的前端框架. Bootstrap是基于HTML. CSS. JAVASCRIPT的,它简洁灵活,使得Web开发更加便捷. 为什么要使用Bootst ...
- shell 管道导致的变量重置问题
测试脚本: #!/bin/sh flag= func() { flag= } main() { func | echo "flag=$flag" } 输出显示的flag=0! 参考 ...
- Javaweb开发之路
本文作者:DavidLin 欢迎转载,但请保留文章原始出处→_→ 本文地址:http://www.cnblogs.com/univeryinli/p/programming-skill-yinli.h ...