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. Jmeter接口测试(第二篇)

    一.新建项目 1.运行Jmeter.bat打开Jmeter 2.添加线程组(测试计划->添加->Thread(users)->线程组) 3.添加HTTP请求(线程组->添加-& ...

  2. js 实现横向滚动轮播并中间暂停下

    效果: html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. VS2005下安装boost

    本文参照http://dxwang.blog.51cto.com/384651/711798 (一)boost的安装和编译 1:下载boost版本,目前最新的版本为1-47-0    下载地址为htt ...

  4. 新增的Java MapReduce API

    http://book.51cto.com/art/201106/269647.htm Hadoop的版本0.20.0包含有一个新的 Java MapReduce API,有时也称为"上下文 ...

  5. 跟我一起在ubuntu中安装docker

    卸载旧版本 $ sudo apt-get remove docker docker-engine docker.io 查看ubuntu版本 设置安装源 通过如下步骤,设置安装源仓库,这里我们使用阿里源 ...

  6. 廖雪峰Java10加密与安全-5签名算法-2DSA签名算法

    DSA DSA:Digital Signature Algorithm,使用EIGamal数字签名算法,和RSA数字签名相比,DSA更快. DSA只能配合SHA使用: SHA1withDSA SHA2 ...

  7. ROS urdf和xacro文件详解

    视觉标签:visual <visual> <origin xyz="0.0 0.0 0.0" /> <geometry> <box siz ...

  8. BZOJ 2683: 简单题(CDQ 分治)

    题面 Time Limit: 50 Sec  Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: ...

  9. Java面试总结-基础篇1

    java多线程-- 自旋锁,偏向锁 好处:可以举Servlet和CGI的对比用户线程和守护线程的区别:用户线程结束后JVM会退出,然后守护线程才会终止(比如垃圾回收线程),如何在java中创建守护线程 ...

  10. mysql大数据表优化

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...