CF1682F.MCMF?
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two integer arrays a and b (bi=0 and ∣bi∣≤109). Array a is sorted in non-decreasing order.
The cost of a subarray a[l:r] is defined as follows:
-
If $ \sum\limits_{j = l}^{r} b_j \neq 0$, then the cost is not defined.
-
Otherwise:
- Construct a bipartite flow graph with r−l+1 vertices, labeled from l to r, with all vertices having bi<0 on the left and those with bi>0 on right. For each i,j such that l≤i,j≤r, bi<0 and bj>0, draw an edge from i to j with infinite capacity and cost of unit flow as ∣ai−aj∣.
- Add two more vertices: source S and sink T.
- For each i such that l≤i≤r and bi<0, add an edge from S to i with cost 0 and capacity ∣bi∣.
- For each i such that l≤i≤r and bi>0, add an edge from i to T with cost 0 and capacity ∣bi∣.
- The cost of the subarray is then defined as the minimum cost of maximum flow from S to T.
You are given q queries in the form of two integers l and r. You have to compute the cost of subarray a[l:r] for each query, modulo 109+7.
If you don't know what the minimum cost of maximum flow means, read here.
给你两个整数数组 a 和 b(其中 bi=0 且 ∣bi∣≤109)。数组 a 按非递减顺序排序。
子数组 a[l:r] 的代价定义如下:
-
若 j=l∑rbj=0,则该代价未定义;
-
否则:
- 构造一个二分流图,包含 r−l+1 个顶点,编号从 l 到 r:所有满足 bi<0 的顶点置于左侧,所有满足 bi>0 的顶点置于右侧;对任意 i,j 满足 l≤i,j≤r、bi<0 且 bj>0,添加一条从 i 到 j 的有向边,容量为无穷大,单位流量的费用为 ∣ai−aj∣;
- 再添加两个顶点:源点 S 和汇点 T;
- 对每个满足 l≤i≤r 且 bi<0 的 i,添加一条从 S 到 i 的边,费用为 0,容量为 ∣bi∣;
- 对每个满足 l≤i≤r 且 bi>0 的 i,添加一条从 i 到 T 的边,费用为 0,容量为 ∣bi∣;
- 该子数组的代价即为从 S 到 T 的最大流的最小费用。
你将收到 q 个查询,每个查询由两个整数 l 和 r 组成。对每个查询,你需要计算子数组 a[l:r] 的代价,并对 109+7 取模。
若你不了解“最大流的最小费用”的含义,请参阅此处。
输入格式
The first line of input contains two integers n and q (2≤n≤2⋅105,1≤q≤2⋅105) — length of arrays a, b and the number of queries.
The next line contains n integers a1,a2…an (0≤a1≤a2…≤an≤109) — the array a. It is guaranteed that a is sorted in non-decreasing order.
The next line contains n integers b1,b2…bn (−109≤bi≤109,bi=0) — the array b.
The i-th of the next q lines contains two integers li,ri (1≤li≤ri≤n). It is guaranteed that $ \sum\limits_{j = l_i}^{r_i} b_j = 0$.
输入的第一行包含两个整数 n 和 q(2≤n≤2⋅105,1≤q≤2⋅105)——分别表示数组 a、b 的长度以及查询次数。
下一行包含 n 个整数 a1,a2,…,an(0≤a1≤a2≤⋯≤an≤109)——即数组 a。保证 a 是按非递减顺序排序的。
再下一行包含 n 个整数 b1,b2,…,bn(−109≤bi≤109,且 bi=0)——即数组 b。
接下来的 q 行中,第 i 行包含两个整数 li,ri(1≤li≤ri≤n)。保证 j=li∑ribj=0。
输出格式
For each query li, ri — print the cost of subarray a[li:ri] modulo 109+7.
对于每个查询 li、ri,输出子数组 a[li:ri] 的代价对 109+7 取模的结果。
输入输出样例
输入#1
8 4 1 2 4 5 9 10 10 13 6 -1 1 -3 2 1 -1 1 2 3 6 7 3 5 2 6
输出#1
2 0 9 15
说明/提示
In the first query, the maximum possible flow is 1 i.e one unit from source to 2, then one unit from 2 to 3, then one unit from 3 to sink. The cost of the flow is 0⋅1+∣2−4∣⋅1+0⋅1=2.
In the second query, the maximum possible flow is again 1 i.e from source to 7, 7 to 6, and 6 to sink with a cost of 0⋅∣10−10∣⋅1+0⋅1=0.
In the third query, the flow network is shown on the left with capacity written over the edge and the cost written in bracket. The image on the right shows the flow through each edge in an optimal configuration.

Maximum flow is 3 with a cost of 0⋅3+1⋅1+4⋅2+0⋅1+0⋅2=9.
In the fourth query, the flow network looks as –

The minimum cost maximum flow is achieved in the configuration –

The maximum flow in the above network is 4 and the minimum cost of such flow is 15.
在第一次查询中,最大可能流为 1,即:1 个单位从源点流向节点 2,再从节点 2 流向节点 3,最后从节点 3 流向汇点。该流的费用为 0⋅1+∣2−4∣⋅1+0⋅1=2。
在第二次查询中,最大可能流仍为 1,即:从源点流向节点 7,再从节点 7 流向节点 6,最后从节点 6 流向汇点,其费用为 0⋅∣10−10∣⋅1+0⋅1=0。
在第三次查询中,左侧展示了流网络图,其中每条边上的数值表示容量,括号内数值表示单位流量费用。右侧图像展示了最优配置下各条边上的实际流量。

该网络的最大流为 3,对应最小费用为 0⋅3+1⋅1+4⋅2+0⋅1+0⋅2=9。
在第四次查询中,流网络结构如下所示:

最小费用最大流在如下配置中实现:

上述网络中的最大流为 4,且该最大流对应的最小费用为 15。
输入解题思路,AI测评打分。不知道怎么写?