SPOJ GSS5
GSS5 - Can you answer these queries V
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| <= 10000 , 1 <= N <= 10000 ). A query is defined as follows: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 <= j <= y2 and x1 <= x2 , y1 <= y2 }. Given M queries (1 <= M <= 10000), your program must output the results of these queries.
Input
The first line of the input consist of the number of tests cases <= 5. Each case consist of the integer N and the sequence A. Then the integer M. M lines follow, contains 4 numbers x1, y1, x2 y2.
Output
Your program should output the results of the M queries for each test case, one query per line.
Example
Input:
2
6 3 -2 1 -4 5 2
2
1 1 2 3
1 3 2 5
1 1
1
1 1 1 1 Output:
2
3
1 【题解】
维护前缀/后缀最大,区间最大,区间总和 区间[x1, y1] [x2, y2]分情况查询
两个区间没有交(y1 < x2), 则答案为[x1, y1].right + [x2, y2].left + (y1 +1 <= x2 - 1 ? [y1 + 1, x2 - 1].sum : 0)
两个区间有交(y1 >= x2), 则分种情况讨论:
左端点 右端点
1、左区间非公共部分 公共部分
2、 公共部分 公共部分
3、左区间非公共部分 右区间非公共部分
4、 公共部分 右区间非公共部分
其实这四种情况可以并为三种:
公共部分最大连续区间
左区间右边最大连续后缀, 右区间非公共部分最大连续前缀
左区间非公共部分最大连续后缀, 右区间最大连续前缀
考虑一下边界,看好怎么+1 -1 合适即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
//改longlong
inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} inline void swap(long long &a, long long &b)
{
long long tmp = a;a = b;b = tmp;
} const long long MAXN = + ;
const long long INF = 0x3f3f3f3f3f3f3f3f; long long n,m,num[MAXN],left[MAXN],right[MAXN],ma[MAXN],sum[MAXN]; void build(long long o = , long long l = , long long r = n)
{
if(l == r)
{
left[o] = right[o] = ma[o] = sum[o] = num[l];
return;
}
long long mid = (l + r) >> ;
build(o << , l, mid);
build(o << | , mid + , r); left[o] = max(left[o << ], sum[o << ] + left[o << | ]);
right[o] = max(right[o << | ], sum[o << | ] + right[o <<]);
ma[o] = max(left[o], max(right[o], left[o << | ] + right[o << ]));
ma[o] = max(ma[o], max(ma[o << ], ma[o << | ]));
sum[o] = sum[o << ] + sum[o << | ];
} struct Node
{
long long left, right, ma, sum;
Node(){left = right = ma = -INF;sum = ;}
Node(long long _left, long long _right, long long _ma, long long _sum){left = _left, right = _right, ma = _ma, sum = _sum;}
}; Node ask(long long ll, long long rr, long long o = , long long l = , long long r = n)
{
if(ll <= l && rr >= r)return Node(left[o], right[o], ma[o], sum[o]);
int mid = (l + r) >> ;
int flag1 = , flag2 = ;
Node re, ans1, ans2;
if(mid >= ll) ans1 = ask(ll, rr, o << , l, mid), flag1 = ;
if(mid < rr) ans2 = ask(ll, rr, o << | , mid + , r), flag2 = ; re.sum = ans1.sum + ans2.sum;
if(flag1)re.left = max(ans1.left, ans1.sum + ans2.left);
else re.left = ans2.left;
if(flag2)re.right = max(ans2.right, ans2.sum + ans1.right);
else re.right = ans1.right;
re.ma = max(re.left, max(re.right, max(ans1.right + ans2.left, max(ans1.ma, ans2.ma)))); return re;
} long long solution(long long x1, long long y1, long long x2, long long y2)
{
register Node tmp1, tmp2, tmp3;
long long ans = -INF;
if(y1 < x2)
{
tmp1 = ask(x1, y1);
tmp2 = ask(x2, y2);
if(y1 + <= x2 - )tmp3 = ask(y1 + , x2 - );
return tmp1.right + tmp2.left + tmp3.sum;
}
else
{
tmp1 = ask(x2, y1);
ans = tmp1.ma; tmp2 = ask(x2, y2);
if(x1 <= x2 - )tmp3 = ask(x1, x2 - );
else tmp3.right = ;
ans = max(ans, tmp2.left + tmp3.right); tmp2 = ask(x1, y1);
if(y1 + <= y2)tmp3 = ask(y1 + , y2);
else tmp3.left = ;
ans = max(ans, tmp2.right + tmp3.left); return ans;
}
} int main()
{
long long t;read(t);
for(;t;--t)
{
read(n);
for(register long long i = ;i <= n;++ i)read(num[i]);
memset(ma, -0x3f, sizeof(ma));
memset(left, -0x3f, sizeof(left));
memset(right, -0x3f, sizeof(right));
memset(sum, , sizeof(sum));
build();
read(m);
for(register long long i = ;i <= m;++ i)
{
long long x1, x2, y1, y2;
read(x1), read(y1), read(x2), read(y2);
printf("%lld\n", solution(x1, y1, x2, y2));
}
}
return ;
}
SPOJ GSS5
注册不了SPJ, 跟标称大数据/小数据(测边界情况)对拍,拍了近半个小时,无错
SPOJ GSS5的更多相关文章
- SPOJ GSS5 Can you answer these queries V
Time Limit: 132MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- SPOJ GSS5 Can you answer these queries V ——线段树
[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...
- Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)
recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...
- 【SPOJ GSS】数据结构题选做
SPOJ GSS1 题意:给一个序列以及一些询问,每个是问\([l,r]\)中最大连续子序列和是多少. 思路:这个问题是以下问题的基础. 我们考虑用线段树来解决这个问题. 首先我们来想想如果要求出最大 ...
- 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 GSS1 & GSS3&挂了的GSS5
线段树然后yy一下,搞一搞. GSS1: 题意:求最大区间和. #include <cstdio> #include <algorithm> using namespace s ...
- SPOJ 2916 GSS5 - Can you answer these queries V
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
随机推荐
- MySQL 中LIMIT的使用详解
在使用数据库过程中,常会遇到查询或者导出某个数据表或者查询集的前几条或者后几条记录,LIMIT可以很好的满足需求. LIMIT基本语法: 如果只给定一个参数,表示记录数. mysql; ) 相当于 m ...
- 转:linux下的c/c++开发
源地址:http://zhidao.baidu.com/question/131261452.html 我就是做LINUX下的C开发的. 准确的说,LINUX下C才是主要的开发语言,但是写应用程序还是 ...
- 洛谷P1081——开车旅行
传送门:QAQQAQ 题意注意点: 1.是从前往后走,不能回头 2.小A小B轮流开,先小A开,而小A是到第二近的点(这点调试的时候查了好久) 3.若绝对值差相同海拔低的更近,而第一个询问若比值相同是海 ...
- Axure中表格使用的技巧
对于新手来说,用Axure做一个表格是一件麻烦的事情.本文教你如何快速学会Axure表格的基础应用. (1)Axure制作基本表格的使用 可以使用“线框图”中的“表格”控件来制作一些简单的表格,同时A ...
- SpringBoot使用注解方式整合Redis
1.首先导入使用Maven导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- occ+vtk显示igs模型
使用Opencascade读取igs文件内模型,使用vtk进行显示. 本案例环境:Opencascade6.6.0 + vtk-5.10 + VS2005(win32) 使用CMake管理工程. C ...
- There is no public key available for the following key IDs:3B4FE6ACC0B21F32
ubuntu 运行完sudo apt-get update之后,提示 W: There is no public key available for the following key IDs: 3B ...
- HZOI20190814 B 不等式
不等式 题目大意:求解满足$L \leqslant(S×x)mod M\leqslant R$的x最小正整数解,无解输出-1 几种部分分: $L==R$,就是$ex_gcd$; 解在$1e6$以内:搜 ...
- 【Redis缓存机制】1.Redis介绍和使用场景
(1)持久化数据库的缺点平常我们使用的关系型数据库有Mysql.Oracle以及SqlServer等,在开发的过程中,数据通常都是通过Web提供的数据库驱动来链接数据库进行增删改查. 那么,我们日常使 ...
- WIX、Squarespace、WordPress 三者的优劣分别是什么?
层出不穷的智能建站,模板建站,源码建站,云建站,仿站,各种建站概念都抛洒于红海之中.到底什么样的网站适合自己,什么样的网站值得我们去消费,什么样的网站能长久,是个非常值得思考的问题. 网站建设技术非常 ...