Chain Chips
Contest: Educational Codeforces Round 146 (Rated for Div. 2)
Difficulty rating: 2300
Tags: data structures, dp, matrices

You are given an undirected graph consisting of $$$n$$$ vertices and $$$n-1$$$ edges. The $$$i$$$-th edge has weight $$$a_i$$$; it connects the vertices $$$i$$$ and $$$i+1$$$.

Initially, each vertex contains a chip. Each chip has an integer written on it; the integer written on the chip in the $$$i$$$-th vertex is $$$i$$$.

In one operation, you can choose a chip (if there are multiple chips in a single vertex, you may choose any one of them) and move it along one of the edges of the graph. The cost of this operation is equal to the weight of the edge.

The cost of the graph is the minimum cost of a sequence of such operations that meets the following condition:

- after all operations are performed, each vertex contains exactly one chip, and the integer on each chip is not equal to the index of the vertex where that chip is located.

You are given $$$q$$$ queries of the form:

- $$$k$$$ $$$x$$$ — change the weight of the $$$k$$$-th edge (the one which connects the vertices $$$k$$$ and $$$k+1$$$) to $$$x$$$.

After each query, print the cost of the graph. Note that you don't actually move any chips; when you compute the cost, the chips are on their initial positions.

Input format:
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).

The second line contains $$$n-1$$$ integers $$$a_1, a_2, \dots, a_{n-1}$$$ ($$$1 \le a_i \le 10^9$$$).

The third line contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$).

Then $$$q$$$ lines follow. The $$$i$$$-th of them contains two integers $$$k$$$ and $$$x$$$ ($$$1 \le k \le n-1$$$; $$$1 \le x \le 10^9$$$) for the $$$i$$$-th query.

Output format:
For each query, print one integer — the cost of the graph after the query is performed.

Constraints:
Time limit: 3.0 s
Memory limit: 512.0 MB

Example input:
10
12 6 12 15 20 8 17 12 15
8
4 10
7 3
6 14
9 9
2 10
3 5
4 11
7 11

Example output:
126
108
120
108
112
98
98
114

Editorial:
Let's try to analyze how many times we traverse each edge, in the style of "Contribution to the Sum" technique. For each edge, the number of times it is traversed must be even, since for every chip that goes from the part of the graph $$$[1..i]$$$ to the part $$$[i+1..n]$$$, there should be a chip that goes in the opposite direction (the number of chips on vertices $$$[1..n]$$$ should be unchanged).
For each vertex, at least one incident edge should be traversed at least twice — otherwise, the chip from this vertex cannot be moved to any other vertex.
We would also like to traverse the edges as rarely as possible. Is it possible to find an answer where, if we traverse any edge, we traverse it only twice? It turns out it is possible. Let's "split" the graph into several parts by removing the edges we don't traverse. If we don't break the constraint that each vertex has at least one incident edge which is traversed by some chip, then each part of the graph will contain at least two vertices. And in each part, we can make sure that each edge is traversed only twice as follows: let the part represent the segment $$$[l, r]$$$ of vertices; if we move the chip $$$r$$$ to the vertex $$$l$$$, the chip $$$l$$$ to the vertex $$$l+1$$$, the chip $$$l+1$$$ to the vertex $$$l+2$$$, ..., the chip $$$r-1$$$ to the vertex $$$r$$$, then every edge in that part will be traversed exactly twice.
So, we have shown that if we pick a subset of edges which we traverse that meets the constraint on each vertex having an incident traversed edge, then it is enough to traverse each chosen edge only twice. Now the problem becomes the following: choose a subset of edges in such a way that every vertex has at least one incident chosen edge, minimize the total weight of this subset, and print the integer which is double that total weight.
Since the structure of the graph is specific, we can run dynamic programming of the form $$$dp_{i,f}$$$ — the minimum total weight of the subset, if we considered the first $$$i$$$ edges, and $$$f$$$ is $$$0$$$ if we haven't taken the last edge, or $$$1$$$ if we have. Obviously, this dynamic programming works in $$$O(n)$$$, which is too slow because we have to process queries. We will employ a classical technique of storing the dynamic programming in segment tree: build a segment tree on $$$n-1$$$ leaves; in every vertex of the segment tree, we store a $$$2 \times 2$$$ matrix $$$d$$$; if the segment represented by the node of the segment tree is $$$[l..r]$$$, then the value of $$$d[f_1][f_2]$$$ is the minimum total weight of the subset of edges between $$$l$$$ and $$$r$$$ such that among every pair of adjacent edges, at least one is chosen; $$$f_1$$$ and $$$f_2$$$ represent the status of the first/last edge on the segment, respectively. And when some element changes, we need to recalculate only $$$O(\log n)$$$ nodes of the segment tree, so this solution works in $$$O(n \log n + q \log n)$$$, albeit with a very big constant factor.
Implementation note: don't use dynamic-size arrays (like std::vector in C++) to store the values in the matrices, it might slow your solution very seriously. Instead, use static-size arrays.