luogu#P5069. [Ynoi Easy Round 2015] 纵使日薄西山

[Ynoi Easy Round 2015] 纵使日薄西山

Background

【You seem very happy.】
Because I rarely get a chance to go out alone with you.
Or rather, it seems like we never really had before, right?
【Didn’t we go to lots of places together when we first met?】

Huh? Ah... right.
But, well... it just feels kind of different from now.
Alright, hurry up, let’s go!
【Chtholly...】


Seriously, don’t make a sound like you’re about to cry. You’re an adult, aren’t you?
I’m fine, look.
Eh...
Ah, th-this kid is also fine.

【If anything unexpected happens, I...】
【I thought I would lose you again, this close...】


A red-haired girl.
Is
Watching here.

Problem Description

Chtholly wants you to maintain a positive integer sequence of length nn, a1,a2,,ana_1,a_2,\ldots,a_n, supporting updates that modify the value at some position.

After each update, you are asked: if we repeatedly perform the following operation on the sequence, how many operations are needed to make the sequence become all 00 (after the query, the sequence is the same as before the query; it will not actually become all 00)?

Choose a position where the maximum value in the sequence occurs. If there are multiple maximum values, choose the one with the smallest index. Let the position be xx. Then decrease ax1,ax,ax+1a_{x-1},a_x,a_{x+1} by 11. If there exists a number less than 00 in the sequence, change that number to 00.

Input Format

The first line contains an integer nn.

The next nn lines each contain one integer aia_i.

The next line contains an integer qq.

The next qq lines each contain two integers xi,yix_i,y_i separated by a space, meaning to modify axia_{x_i} to yiy_i.

Output Format

Output qq lines, each containing one integer representing the answer.

4
3
6
6
4
3
4 4
3 5
1 8
10
10
13

Hint

Idea: ccz181078, Solution: ccz181078, Code: ccz181078, Data: ccz181078

Constraints: 1n,q1051\leq n,q\leq 10^5, 1xin1\leq x_i\leq n, 1ai,yi1091\leq a_i,y_i\leq 10^9.

Translated by ChatGPT 5