아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Single-track railway

시간 제한4초메모리 제한512 MB

요약
인접한 역 사이의 이동 시간이 갱신될 때마다, 양 끝에서 출발한 두 열차가 역에서 만날 때의 최소 대기 시간을 구한다.
난이도

보통10점 중 6점

유형
누적 합, 이분 탐색, 배열
정답자
아직 제출이 없습니다

문제

Trains running on a single-track railway can only meet at the stations. Suppose that a pair of trains simultaneously leave in the opposite directions, one from the initial and the other from the final station, i.e. the initial station in the opposite direction. It is likely that one of the trains will have to wait for the other one at one of the stations along the railway. To minimize the delays, the trains meet at the station such that the waiting time is minimized.

We know the travel time between each two adjacent stations, equal in both directions. Unfortunately, the travel times constantly change because of the works along the railway. You are given the initial travel times and an updated travel time for the affected section after each change. Write a program that computes the shortest possible waiting time for a pair of trains leaving from the opposite ends of the railway after each of the changes.

입력

The first line specifies the number of stations, nn. In the second line, n−1n - 1 numbers are given, corresponding to the initial travel times between the adjacent stations (the ii-th number is the travel time between stations ii and i+1i + 1). The third line specifies the number of changes, kk. This is followed by kk lines, each containing two numbers: the first one, j∈\[1,n−1]j ∈ \[1, n - 1], specifies the station, and the second gives the updated travel time between stations jj and j+1j + 1. Keep in mind that the first station is numbered 11 rather than 00.

출력

Output n+1n+1 lines, where the ii-th line will contain the shortest possible waiting time after i−1 i - 1 changes (the first one should correspond to the situation before any changes).

제한

  • 2≤n≤200,0002 ≤ n ≤ 200\\,000
  • 0≤k≤200,0000 ≤ k ≤ 200\\,000
  • All travel times (both the initial and the updated ones) are integers from the interval \[1,106]\[1, 10^6].

힌트

At the beginning, the trains leaving in the opposite directions should meet at station 33. The first train will reach that station in 9090 minutes, and the second will arrive there in 100100 minutes; the waiting time will thus be 1010 minutes. Following the first change, the optimal meeting point becomes station 44. Both trains will take 130130 minutes to get there, so neither will have to wait. After the second change, they will also meet at station 44. This time, however, the train that arrives first will have to wait for 4040 minutes.

예제1

  1. 예제 1

    입력
    6
    20 70 40 10 50
    2
    4 80
    2 30
    
    예상 출력
    10
    0
    40