Squares
Contest: Codeforces Round 706 (Div. 1)
Difficulty rating: 3300
Tags: constructive algorithms, data structures, dp, graphs, trees

There are $$$n$$$ squares drawn from left to right on the floor. The $$$i$$$-th square has three integers $$$p_i,a_i,b_i$$$, written on it. The sequence $$$p_1,p_2,\dots,p_n$$$ forms a permutation.

Each round you will start from the leftmost square $$$1$$$ and jump to the right. If you are now on the $$$i$$$-th square, you can do one of the following two operations:

1. Jump to the $$$i+1$$$-th square and pay the cost $$$a_i$$$. If $$$i=n$$$, then you can end the round and pay the cost $$$a_i$$$.
2. Jump to the $$$j$$$-th square and pay the cost $$$b_i$$$, where $$$j$$$ is the leftmost square that satisfies $$$j > i, p_j > p_i$$$. If there is no such $$$j$$$ then you can end the round and pay the cost $$$b_i$$$.

There are $$$q$$$ rounds in the game. To make the game more difficult, you need to maintain a square set $$$S$$$ (initially it is empty). You must pass through these squares during the round (other squares can also be passed through). The square set $$$S$$$ for the $$$i$$$-th round is obtained by adding or removing a square from the square set for the $$$(i-1)$$$-th round.

For each round find the minimum cost you should pay to end it.

Input format:
The first line contains two integers $$$n$$$, $$$q$$$ ($$$1\le n,q\le 2 \cdot 10^5$$$)  — the number of squares and the number of rounds.

The second line contains $$$n$$$ distinct integers $$$p_1,p_2,\dots,p_n$$$ ($$$1\le p_i\le n$$$). It is guaranteed that the sequence $$$p_1,p_2,\dots,p_n$$$ forms a permutation.

The third line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-10^9\le a_i\le 10^9$$$).

The fourth line contains $$$n$$$ integers $$$b_1,b_2,\dots,b_n$$$ ($$$-10^9\le b_i\le 10^9$$$).

Then $$$q$$$ lines follow, $$$i$$$-th of them contains a single integer $$$x_i$$$ ($$$1\le x_i\le n$$$). If $$$x_i$$$ was in the set $$$S$$$ on the $$$(i-1)$$$-th round you should remove it, otherwise, you should add it.

Output format:
Print $$$q$$$ lines, each of them should contain a single integer  — the minimum cost you should pay to end the corresponding round.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Let's consider the character $$$T$$$ as the end of a round. Then we can draw two graphs for the first and the second test.

In the first round of the first test, the set that you must pass through is $$$\{1\}$$$. The path you can use is $$$1\to 3\to T$$$ and its cost is $$$6$$$.

In the second round of the first test, the set that you must pass through is $$$\{1,2\}$$$. The path you can use is $$$1\to 2\to 3\to T$$$ and its cost is $$$8$$$.

Example input:
3 2
2 1 3
10 -5 4
3 -2 3
1
2

Example output:
6
8

Example input:
5 4
2 1 5 3 4
6 -5 3 -10 -1
0 3 2 7 2
1
2
3
2

Example output:
-8
-7
-7
-8

Editorial:
If we let the parent of $$$i(1\le i\le n)$$$ is the rightmost $$$j$$$ satisfying $$$j<i,p_j>p_i$$$(if that $$$j$$$ doesn't exist, the parent of $$$i$$$ is $$$0$$$), we can get a tree and we can dfs the tree with the order $$$0,1,2,\dots,n$$$. Let's call the parent of $$$i$$$ is $$$\operatorname{pa}_i$$$ and the children set of $$$i$$$ is $$$\operatorname{child}_i$$$.
Here is an example. $$$n=5$$$ and the permutation is $$$2,1,5,3,4$$$.
The graphs we build follow.
Let's call the edges with the cost $$$a_i$$$ A-type and the edges with the cost $$$b_i$$$ B-type. Consider that now you are on the $$$i$$$-th square. If you choose a B-type edge(with the cost $$$b_i$$$), you will jump over all the squares in $$$i$$$'s subtree($$$i$$$ is not included), that is to say, you will not pass through them. If you choose an A-type edge, we can think you enter $$$i$$$'s subtree and you must pass through all of the $$$i$$$'s child.
To simplify the problem, we think you choose a node $$$i$$$ if and only if you pass through the A-type edge from the $$$i$$$-th square. Let's call the chosen set is $$$S$$$. It is not hard to find out that the node $$$i$$$ can be chosen if and only if all the ancestors of $$$i$$$ in the tree are chosen(if not, you even don't have the chance to pass through $$$i$$$). And that you pass through the $$$i$$$-th square is equivalent to you choosing the parent of $$$i$$$.
Then the problem is simplified. You purpose is to choose some nodes in the tree ($$$0$$$ must be chosen as it is the root) and minimize your cost. Here your cost is $$$\sum_{i\in S}a_i+\sum_{i\not\in S,\operatorname{pa}_i\in S}b_i$$$. If we let $$$c_i=a_i-b_i+\sum_{j\in\operatorname{child}_i}b_j$$$, the cost can be considered as $$$\sum_{i\in S}c_i+\sum_{j\in\operatorname{child}_0}b_j$$$. It is very easy to solve it with binups.
The time complexity is $$$O((n+q)\log n)$$$.