CF BUDDY
← Problems·

1406D · Three Sequences

2200 · constructive algorithms, data structures, greedy

Problem: You are given a sequence of nn integers a1,a2,,ana_1, a_2, \ldots, a_n.

You have to construct two sequences of integers bb and cc with length nn that satisfy:

  • for every ii (1in1\leq i\leq n) bi+ci=aib_i+c_i=a_i
  • bb is non-decreasing, which means that for every 1<in1<i\leq n, bibi1b_i\geq b_{i-1} must hold
  • cc is non-increasing, which means that for every 1<in1<i\leq n, cici1c_i\leq c_{i-1} must hold

You have to minimize max(bi,ci)\max(b_i,c_i). In other words, you have to minimize the maximum number in sequences bb and cc.

Also there will be qq changes, the ii-th change is described by three integers l,r,xl,r,x. You should add xx to al,al+1,,ara_l,a_{l+1}, \ldots, a_r.

You have to find the minimum possible value of max(bi,ci)\max(b_i,c_i) for the initial sequence and for sequence after each change.

Input Format: The first line contains an integer nn (1n1051\leq n\leq 10^5).

The secound line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1in1\leq i\leq n, 109ai109-10^9\leq a_i\leq 10^9).

The third line contains an integer qq (1q1051\leq q\leq 10^5).

Each of the next qq lines contains three integers l,r,xl,r,x (1lrn,109x1091\leq l\leq r\leq n,-10^9\leq x\leq 10^9), desribing the next change.

Output Format: Print q+1q+1 lines.

On the ii-th (1iq+11 \leq i \leq q+1) line, print the answer to the problem for the sequence after i1i-1 changes.

Note: In the first test:

  • The initial sequence a=(2,1,7,3)a = (2, -1, 7, 3). Two sequences b=(3,3,5,5),c=(5,2,2,2)b=(-3,-3,5,5),c=(5,2,2,-2) is a possible choice.
  • After the first change a=(2,4,4,0)a = (2, -4, 4, 0). Two sequences b=(3,3,5,5),c=(5,1,1,5)b=(-3,-3,5,5),c=(5,-1,-1,-5) is a possible choice.
  • After the second change a=(2,4,6,2)a = (2, -4, 6, 2). Two sequences b=(4,4,6,6),c=(6,0,0,4)b=(-4,-4,6,6),c=(6,0,0,-4) is a possible choice.

Sample Cases

Case 1

Input

4
2 -1 7 3
2
2 4 -3
3 4 2

Output

5
5
6

Case 2

Input

6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3

Output

3
3
3
1

Case 3

Input

1
0
2
1 1 -1
1 1 -1

Output

0
0
-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback