codeforces CF903G Yet Another Maxflow Problem 線段樹
$ \Rightarrow $ 戳我進CF原題
G. Yet Another Maxflow Problem
In this problem you will have to deal with a very special network.
The network consists of two parts: part $ A $ and part $ B $ .
For each index $ i (1?≤?i?<?n) $ there is a directed edge from vertex $ A_i $ to vertex $ A_{i?+?1} $ ,
and from $ B_i $ to $ B_{i?+?1} $ , respectively.
Also there might be several directed edges going from part $ A $ to part $ B $ (but never from $ B $ to $ A $ ).
You have to calculate the maximum flow value from $ A_1 $ to $ B_n $ in this network.
Capacities of edges connecting $ A_i $ to $ A_{i?+?1} $ might sometimes change,
Apart from that, the network is fixed (there are no changes in part $ B $ ,
no changes of edges going from $ A $ to $ B $ , and no edge insertions or deletions).
Take a look at the example and the notes to understand the structure of the network better.
Input
The first line contains three integer numbers $ n, m $ and $ q (2?≤?n,?m?≤?2·10^5, 0?≤?q?≤?2·10^5) $
— the number of vertices in each part, the number of edges going from $ A $ to $ B $ and the number of changes, respectively.
Then $ n?-?1 $ lines follow, $ i $ -th line contains two integers $ x_i $ and $ y_i $ denoting
that the edge from $ A_i $ to $ A_{i?+?1} $ has capacity $ x_i $ and the edge from $ B_i $ to $ B_{i?+?1} $ has capacity $ y_i (1?≤?x_i,?y_i?≤?10^9) $ .
Then $ m $ lines follow, describing the edges from $ A $ to $ B $ .
Each line contains three integers $ x, y $ and $ z $ denoting an edge from $ A_x $ to $ B_y $ with capacity $ z (1?≤?x,?y?≤?n, 1?≤?z?≤?10^9) $ .
There might be multiple edges from $ A_x $ to $ B_y $ .
And then $ q $ lines follow, describing a sequence of changes to the network.
$ i $ -th line contains two integers $ v_i $ and $ w_i $ , denoting that the capacity of the edge from $ A_{v_i} $ to $ A_{v_i?+?1} $ is set to $ w_i (1?≤?vi?<?n, 1?≤?wi?≤?10^9) $ .
Output
Firstly, print the maximum flow value in the original network.
Then print $ q $ integers, $ i $ -th of them must be equal to the maximum flow value after $ i $ -th change.
Examples
input
4 3 2
1 2
3 4
5 6
2 2 7
1 4 8
4 3 9
1 100
2 100
output
9
14
14
Note
This is the original network in the example:
題目翻譯
一張圖分為兩部分,左右都有 $ n $ 個節點,
$ A_i \rightarrow A_{i+1} $ 連邊, $ B_i \rightarrow B_{i+1} $ 連邊,容量給出
有 $ m $ 對 $ A_i \rightarrow B_j $ 有邊,容量給出
兩種操作
1.修改某條 $ A_i \rightarrow A_{i+1} $ 的邊的容量
2.詢問從 $ A_1 $ 到 $ B_n $ 的最大流
$ 2 \le n,m \le 2 \times 10^5 $ 流量 $ \le 10^9 $
感謝@yybyyb 提供的翻譯
思路:
因為最大流的流量等於最小割的容量,所以最大流不好做的時候考慮最小割。
一些顯而易見的結論:
假如在 $ A $ 中,割掉 $ A_x $ 和 $ A_{x+1} $ 之間的有向邊,
那麽割掉 $ A_y (y>x) $ 與 $ A_{y+1} $ 之間的有向邊是沒有意義的。假如在 $ B $ 中,割掉 $ B_X $ 和 $ B_{x+1} $ 之間的有向邊,
那麽割掉 $ B_y (y>x) $ 與 $ B_{y+1} $ 之間的有向邊是沒有意義的。因此,在 $ A, B $ 中至多有一條邊屬於割集。
假設在 $ A $ 中被割掉的邊是 $ (A_x, A_{x+1}) $ ,在 $ B $ 中被割掉的邊是 $ (B_y, B_{y+1}) $ ,
那麽任意 $ (A_u, B_v) (u \ge x $ 且 $ v> y) $ 都是屬於這個割集的。因此,割的大小可以看作三部分:在 $ A $ 中的割集大小,在 $ B $ 中的割集大小,在 $ A, B $ 間的割集大小。
由於後兩部分不會改變(只改 $ A $ 的邊),考慮計算出它們。
考慮在 $ A $ 中從小到大枚舉 $ A_x $ ,表示割掉 $ A_x $ 和 $ A_{x+1} $ 之間的邊, $ x=n $ 表示不存在這條邊。
那麽下面要做的事情是在 $ B $ 中找到一個最優決策點 $ y $ ,使得後兩部分的和最小。
對於每加入一條 $ A, B $ 間的邊,對答案造成的影響是連續的一段,從 $ 1 $ 開始。
所以我們只需要寫一個支持區間加,求 $ [1, n] $ 的最小值的線段樹就好了。
合並兩部分的答案。這個可以用一個可刪堆或者線段樹來維護。
時間復雜度 $ O((n+m+q)\quad log_n ) $
codeforces CF903G Yet Another Maxflow Problem 線段樹