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的更多相关文章

  1. SPOJ GSS5 Can you answer these queries V

    Time Limit: 132MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  2. SPOJ GSS5 Can you answer these queries V ——线段树

    [题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...

  3. 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 ...

  4. 【SPOJ GSS】数据结构题选做

    SPOJ GSS1 题意:给一个序列以及一些询问,每个是问\([l,r]\)中最大连续子序列和是多少. 思路:这个问题是以下问题的基础. 我们考虑用线段树来解决这个问题. 首先我们来想想如果要求出最大 ...

  5. 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[ ...

  6. SPOJ GSS1 & GSS3&挂了的GSS5

    线段树然后yy一下,搞一搞. GSS1: 题意:求最大区间和. #include <cstdio> #include <algorithm> using namespace s ...

  7. SPOJ 2916 GSS5 - Can you answer these queries V

    传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...

  8. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  9. 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 ...

随机推荐

  1. Joomla - 模块系统(新建模块、模块类别、自定义模块)

    Joomla - 模块系统,模块配合模板的布局设置.菜单分配.权限分配能创建出一个内容丰富且易于管理的高度自定义前端页面架构 一.新建模块 进入后台,点击顶栏菜单 扩展管理 -> 模块管理 ,进 ...

  2. c语言学习笔记 - 文件操作

    #include <stdio.h>#include <time.h> int main(void){ time_t t;               //类似于size_t那 ...

  3. To Learn more

    Openssl Heartbleed Html5 +ajax+ openlayers+qunee+google maps darkleech

  4. PAT甲级——A1094 The Largest Generation

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  5. PKU--1976 A Mini Locomotive (01背包)

    题目http://poj.org/problem?id=1976 分析:给n个数,求连续3段和的最大值. 这个题目的思考方式很像背包问题. dp[i][j]表示前i个数字,放在j段的最大值. 如果选了 ...

  6. 关于obj文件的理解

    编译器先编译.cpp为obj文件,看看文件内有没有冲突,然后再进行链接,链接头文件引入的lib库等等,然后就生成exe文件了,下面这个图说的很好:

  7. [欧拉路]CF1152E Neko and Flashback

    1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...

  8. HZOI2019 星际旅行 欧拉路

    题目大意:https://www.cnblogs.com/Juve/articles/11207540.html—————————> 题解:网上都是一句话题解:将所有的边拆成两条,问题变成去掉两 ...

  9. html常用标签6-表单标签

    1.表单的初始化标签 <form action="#" method="get"><!--表单的开始--> </form> ...

  10. JavaScript RegExp 对象的三种方法

    JavaScript RegExp 对象有 3 个方法:test().exec() 和 compile().(1) test() 方法用来检测一个字符串是否匹配某个正则表达式,如果匹配成功,返回 tr ...