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[j] ; x1 <= i <= y1 , x2 j <= y2 and x1 <= x2 , y1 <= y2 }
分析:
其实画个图分类讨论一下之后,跟gss1基本一样。。。
注意到x1<=x2 , y1<=y2.
所以大致可以分为:
1.y1<x2:
直接计算区间[x1,y1]的右子区间连续最大和,[x2,y2]的左区间连续最大和,如果y1与x2之间有空隙的话,需要加上[y1+1,x2-1]的和。
2.y2>=x2:
考虑x1与x2的关系:
如果x1==x2,最大值可能出现在区间[x1,y1]的最大子段和,[x1,y1]的右区间连续最大和加上[y1+1,y2]的左区间连续最大和。
否则,考虑三个区间[x1,x2-1],[x2,y1],[y1+1,y2],这时考虑方式跟上面差不多,就不写出来了,具体可以看代码。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 100005; int a[MAXN]; struct segTree{
int l,r,lx,rx,mx,sum;
inline int mid(){
return (l+r)>>1;
}
}tree[MAXN<<2]; inline void Union(segTree& now,segTree l,segTree r){
now.lx = max( l.lx , l.sum+max(0,r.lx) );
now.rx = max( r.rx , r.sum+max(0,l.rx) );
now.mx = max( l.rx+r.lx , max(l.mx,r.mx) );
now.sum = l.sum+r.sum;
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
if(l==r){
tree[rt].lx = tree[rt].rx = tree[rt].sum = tree[rt].mx = a[l];
return;
}
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} void modify(int pos,int c,int rt){
if(tree[rt].l==tree[rt].r){
tree[rt].lx = tree[rt].rx = tree[rt].mx = tree[rt].sum = c;
return;
}
int mid = tree[rt].mid();
if(pos<=mid)modify(pos,c,rt<<1);
else modify(pos,c,rt<<1|1);
Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} segTree ask(int l,int r,int rt){
if(l<=tree[rt].l&&r>=tree[rt].r)
return tree[rt];
int mid = tree[rt].mid();
segTree ans;
if(r<=mid) ans = ask(l,r,rt<<1);
else if(l>mid) ans = ask(l,r,rt<<1|1);
else{
segTree a = ask(l,r,rt<<1);
segTree b = ask(l,r,rt<<1|1);
Union( ans,a,b );
}
Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
return ans;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int m,n,x,y,c,d;
int ncase;
RD(ncase);
while(ncase--){
RD(n);
rep1(i,n)
RD(a[i]);
build(1,n,1);
RD(m);
segTree ca,cb,cc;
while(m--){
RD4(x,y,c,d);
int sum = 0;
if(y>=c){
if(x<c){
ca = ask(x,c-1,1);
cb = ask(c,y,1);
sum = max( ca.rx+cb.lx,cb.mx );
if(y<d){
cc = ask(y+1,d,1);
int tmp = max( max(0,ca.rx)+cb.sum+cc.lx , cb.rx+cc.lx );
sum = max( sum,tmp );
}
}else{
ca = ask(c,y,1);
sum = ca.mx;
if(y<d){
cc = ask(y+1,d,1);
sum = max(sum,ca.rx+cc.lx);
}
}
}else{
ca = ask(x,y,1);
cb = ask(c,d,1);
sum = ca.rx+cb.lx;
if(y+1<c){
cc = ask(y+1,c-1,1);
sum += cc.sum;
}
}
printf("%d\n",sum);
}
} return 0;
}
GSS5 spoj 2916. Can you answer these queries V 线段树的更多相关文章
- 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 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 GSS5 Can you answer these queries V ——线段树
[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...
- 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 ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- SPOJ GSS2 Can you answer these queries II ——线段树
[题目分析] 线段树,好强! 首先从左往右依次扫描,线段树维护一下f[].f[i]表示从i到当前位置的和的值. 然后询问按照右端点排序,扫到一个位置,就相当于查询区间历史最值. 关于历史最值问题: 标 ...
随机推荐
- C++ Vector 使用心得 [转]
标准库Vector类型 使用需要的头文件:#include <vector>Vector:Vector 是一个类模板.不是一种数据类型. Vector<int>是一种数据类型. ...
- CSS(04) 定位
布局常用的三种:标准流.定位.浮动: 1.文档流-标准流 窗体自上而下分成一行行(元素在 (X)HTML 中的位置),并在一行行中从左到右排放元素: 2.CSS 定位 Position 属性(绝对定位 ...
- perl学习笔记(2)
1)记得刚开始写perl的时候,对于一个功能,总是拿目前能用的数据类型来解决问题,不想想有没有更好的,能用能解决问题就好,这就导致了后期,要在函数里面添加功能的时候,函数要添加很多参数,一个函数有7. ...
- Unity实现相似于安卓原生项目的点击安卓返回button回到前一页的功能
本章博主和大家一起讨论下Unity怎么实现类似安卓原生项目,点击安卓返回button实现返回到前一个页面的功能. 1.定义一个泛型用于响应安卓的返回button public static List& ...
- C++学习笔记之友元
一.引言 C++控制对类对象私有部分(private)的访问,通常只能通过公有的(public)类方法去访问.但是有时候这种限制太严格,不适合特定的问题,于是C++提供了另外一种形式的访问权限:友元. ...
- Objective-C语法之代码块(block)的使用
代码块本质上是和其它变量相似.不同的是,代码块存储的数据是一个函数体.使用代码块是,你能够像调用其它标准函数一样,传入參数数,并得到返回值. 脱字符(^)是块的语法标记.依照我们熟悉的參数语法规约所定 ...
- C#类的成员初始化顺序
首先我们来看看引用类型的成员初始化过程 我们来看一个例子吧 class Program { static void Main(string[] args) { Driv ...
- Visual Studio Code 1.0.1 for python
1. 安 F1健 ext install python E:\test\.vscode下的三个文件 2.launch.json { "version": "0.1.0&q ...
- 进程间通信和同步:pipe、FIFO、消息队列、信号量、共享内存、信号
一.半双工管道(pipe) 关于管道详细介绍可参考http://www.cnblogs.com/nufangrensheng/p/3560130.html. 1.管道实现父子进程间通信实例: /* p ...
- 标准库 - unicode/utf8/utf8.go 解读
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a B ...