CF1682F.MCMF?
普及/提高-
通过率:0%
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.
输入格式
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$ .
输出格式
For each query li , ri — print the cost of subarray a[li:ri] modulo 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.