Single-track railway
시간 제한4초메모리 제한512 MB
인접한 역 사이의 이동 시간이 갱신될 때마다, 양 끝에서 출발한 두 열차가 역에서 만날 때의 최소 대기 시간을 구한다.
문제
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, . In the second line, numbers are given, corresponding to the initial travel times between the adjacent stations (the -th number is the travel time between stations and ). The third line specifies the number of changes, . This is followed by lines, each containing two numbers: the first one, , specifies the station, and the second gives the updated travel time between stations and . Keep in mind that the first station is numbered rather than .
출력
Output lines, where the -th line will contain the shortest possible waiting time after changes (the first one should correspond to the situation before any changes).
제한
- All travel times (both the initial and the updated ones) are integers from the interval .
힌트
At the beginning, the trains leaving in the opposite directions should meet at station . The first train will reach that station in minutes, and the second will arrive there in minutes; the waiting time will thus be minutes. Following the first change, the optimal meeting point becomes station . Both trains will take minutes to get there, so neither will have to wait. After the second change, they will also meet at station . This time, however, the train that arrives first will have to wait for minutes.