https://www.spoj.pl/problems/GSS5/
SPOJ Problem Set (classical)2916. Can you answer these queries VProblem code: GSS5 |
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
Added by: | Frank Rafael Arteaga |
Date: | 2008-08-06 |
Time limit: | 1s |
Source limit: | 50000B |
Languages: | All except: C99 strict ERL JS PERL 6 |
Resource: | K.-Y. Chen and K.-M. Chao, On the Range Maximum-Sum Segment Query Problem, 2007. |
hide comments
2010-04-09 11:18:43 Ralf I keep getting timeouts for this problem when I test my solution. But I write an additional random line at the end, I get wrong answer in 0.02s. How could it timeout then? | |
2010-04-09 10:57:58 yuzeming if you get wa try it 1 5 -1 -1 -1 -1 -1 1 1 5 1 5 ans=-1 1 5 -1 -1 1 -1 -1 1 1 5 1 5 ans=1 Last edit: 2010-04-13 08:38:20 |
题意:给定一个序列a[i] 每次询问给出x1<=x2,y1<=y2,在[x1,x2]中找一个起点i,在[y1,y2]中找一个终点j,使得[i,j]的和最大
跟GSS1&GSS3还是相似,只要考虑x2和y1的关系 考虑区间相交或相离就行了...
PS: 注意如果x1=y1 那么count( 1 , 1 , n , x1 , y1 - 1 )就会出现error202...
所以45到49行的程序必须加上,否则后果...