Loi_Vampire's Blog

自己选择的路,就算跪着也要走完

02/1
19:52
数据结构 线段树

GSS3 - Can you answer these queries III 线段树-最大连续子段和

You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.

Input

The first line of input contains an integer N. The following line contains N integers, representing the sequence A1..AN. The third line contains an integer M. The next M lines contain the operations in following form: 0 x y: modify Ax into y (|y|<=10000). 1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.

Output

For each query, print an integer as the problem required.

Example

Input:
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3

Output:
6
4
-3

 

还是最大连续子段和,只是加了修改操作,同GSS1

 

01/25
11:31
数据结构 线段树

SPOJ GSS1 - Can you answer these queries I 线段树-最大连续子段和

You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows: Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }. Given M queries, your program must output the results of these queries.

Input

The first line of the input file contains the integer N. In the second line, N numbers follow. The third line contains the integer M. M lines follow, where line i contains 2 numbers xi and yi.

Output

Your program should output the results of the M queries, one query per line.

Example

Input:
3
-1 2 3
1
1 2
Output:
2

题意:给定一个序列,每次询问一个区间的最大连续子段和。

线段树,维护左起最大连续子段和,右起最大连续子段和,最大连续子段和,以及区间和。

在询问时,一共有三种情况: Ⅰ 最大连续子段和在左区间,直接递归查询 Ⅱ 最大连续子段和在右区间,同上 Ⅲ 最大连续子段和跨越左右区间,那么,对左区间最大连续子段和,右区间最大连续子段和,和左区间右起最大连续子段和 右区间左起最大连续子段和的和 取max即可