GSS2 - Can you answer these queries II


Being a completist and a simplest, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always fails in contests.

When having a contest, Yang Zhe looks at the score of every problems first. For the problems of the same score, Yang Zhe will do only one of them. If he's lucky enough, he can get all the scores wanted.

Amber is going to hold a contest in SPOJ. She has made a list of N candidate problems, which fit Yang Zhe very well. So Yang Zhe can solve any problem he want. Amber lined up the problems, began to select. She will select a subsequence of the list as the final problems. Being A girl of great compassion, she'd like to select such a subsequence (can be empty) that Yang Zhe will get the maximal score over all the possible subsequences.

Amber found the subsequence easily after a few minutes. To make things harder, Amber decided that, Yang Zhe can take this contest only if Yang Zhe can answer her Q questions. The question is: if the final problems are limited to be a subsequence of list[X..Y] (1 <= X <= Y <= N), what's the maximal possible score Yang Zhe can get?

As we know, Yang Zhe is a bit idiot (so why did he solve the problem with a negative score?), he got Wrong Answer again... Tell him the correct answer!

Input

  • Line 1: integer N (1 <= N <= 100000);
  • Line 2: N integers denoting the score of each problem, each of them is a integer in range [-100000, 100000];
  • Line 3: integer Q (1 <= Q <= 100000);
  • Line 3+i (1 <= i <= Q): two integers X and Y denoting the ith question.

Output

  • Line i: a single integer, the answer to the ith question.

Example

Input:
9
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9

Output:
4
5
3

Warning: large input/output data, be careful with certain languages

hide comments
c_jain: 2017-12-13 13:37:05

thanks Karol for Clarification :)

jatinjulu: 2017-10-10 18:43:32

What a horrible way to write a question!!

weramajstor: 2017-08-02 13:40:11

SAMOBOR,FUCK YEAH!!!!

the_unbeatable: 2017-06-23 13:04:02

@1729.000 she can only choose a subsequence, that is, a contiguous sequence, you can't just skip some of the elements

krist7599555: 2017-03-31 08:48:05

the best problem
great concept :)

aexpo: 2016-12-16 16:11:30

Tricky test case:
10
-1 -2 -3 4 4 5 6 -6 -6 100
7
1 10
1 4
1 5
2 6
4 9
3 9
2 8

aexpo: 2016-12-16 16:07:01

One of the best questions on segment trees!
Hint:
1.Using set to find the sum of 2 slices(each number taken once) may give you TLE
2.Lazy propogation is a must here to do within time limit
3.add an extra field (best lazy) in addition to lazy to each node inorder to handle multiple updates on the same node.
4.For further hints refer to Brian Bi's answer on GSS2 on qoura

nurlansofiyev: 2016-09-15 18:31:07

About IO : For the problems of the same score, Yang Zhe will do only one of them !

Zhouxing Shi: 2013-04-22 23:52:45

subsequence?

Ella: 2013-03-06 03:33:27

Yes,I agree with you,Walrus.I think that amber choose a substring.It just means that if you want to get data from i to j,you cannot jump the over the negative score between i and j.


Added by:Fudan University Problem Setters
Date:2007-05-16
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:Description, standard program and test data by Yang Zhe