CF1682F.MCMF?

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two integer arrays aa and bb (bi≠0b_i \neq 0 and ∣bi∣≤109|b_i| \leq 10^9). Array aa is sorted in non-decreasing order.

The cost of a subarray a[l:r]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+1r-l+1 vertices, labeled from ll to rr, with all vertices having bi<0b_i \lt 0 on the left and those with bi>0b_i \gt 0 on right. For each i,ji, j such that l≤i,j≤rl \le i, j \le r, bi<0b_i \lt 0 and bj>0b_j \gt 0, draw an edge from ii to jj with infinite capacity and cost of unit flow as ∣ai−aj∣|a_i-a_j|.
    • Add two more vertices: source SS and sink TT.
    • For each ii such that l≤i≤rl \le i \le r and bi<0b_i \lt 0, add an edge from SS to ii with cost 00 and capacity ∣bi∣|b_i|.
    • For each ii such that l≤i≤rl \le i \le r and bi>0b_i \gt 0, add an edge from ii to TT with cost 00 and capacity ∣bi∣|b_i|.
    • The cost of the subarray is then defined as the minimum cost of maximum flow from SS to TT.

You are given qq queries in the form of two integers ll and rr. You have to compute the cost of subarray a[l:r]a[l:r] for each query, modulo 109+710^9 + 7.

If you don't know what the minimum cost of maximum flow means, read here.

给你两个整数数组 aa 和 bb(其中 bi≠0b_i \neq 0 且 ∣bi∣≤109|b_i| \leq 10^9)。数组 aa 按非递减顺序排序。

子数组 a[l:r]a[l:r] 的代价定义如下:

  • 若 ∑j=lrbj≠0\sum\limits_{j = l}^{r} b_j \neq 0,则该代价未定义;

  • 否则:

    • 构造一个二分流图,包含 r−l+1r-l+1 个顶点,编号从 ll 到 rr:所有满足 bi<0b_i \lt 0 的顶点置于左侧,所有满足 bi>0b_i \gt 0 的顶点置于右侧;对任意 i,ji, j 满足 l≤i,j≤rl \le i, j \le r、bi<0b_i \lt 0 且 bj>0b_j \gt 0,添加一条从 ii 到 jj 的有向边,容量为无穷大,单位流量的费用为 ∣ai−aj∣|a_i-a_j|;
    • 再添加两个顶点:源点 SS 和汇点 TT;
    • 对每个满足 l≤i≤rl \le i \le r 且 bi<0b_i \lt 0 的 ii,添加一条从 SS 到 ii 的边,费用为 00,容量为 ∣bi∣|b_i|;
    • 对每个满足 l≤i≤rl \le i \le r 且 bi>0b_i \gt 0 的 ii,添加一条从 ii 到 TT 的边,费用为 00,容量为 ∣bi∣|b_i|;
    • 该子数组的代价即为从 SS 到 TT 的最大流的最小费用。

你将收到 qq 个查询,每个查询由两个整数 ll 和 rr 组成。对每个查询,你需要计算子数组 a[l:r]a[l:r] 的代价,并对 109+710^9 + 7 取模。

若你不了解“最大流的最小费用”的含义,请参阅此处。

输入格式

The first line of input contains two integers nn and qq (2≤n≤2⋅105,1≤q≤2⋅105)(2 \leq n \leq 2\cdot 10^5, 1 \leq q \leq 2\cdot10^5) — length of arrays aa, bb and the number of queries.

The next line contains nn integers a1,a2…ana_1,a_2 \ldots a_n (0≤a1≤a2…≤an≤109)0 \leq a_1 \le a_2 \ldots \le a_n \leq 10^9) — the array aa. It is guaranteed that aa is sorted in non-decreasing order.

The next line contains nn integers b1,b2…bnb_1,b_2 \ldots b_n (−109≤bi≤109,bi≠0)(-10^9\leq b_i \leq 10^9, b_i \neq 0) — the array bb.

The ii-th of the next qq lines contains two integers li,ril_i,r_i (1≤li≤ri≤n)(1\leq l_i \leq r_i \leq n). It is guaranteed that $ \sum\limits_{j = l_i}^{r_i} b_j = 0$.

输入的第一行包含两个整数 nn 和 qq(2≤n≤2⋅1052 \leq n \leq 2\cdot 10^5,1≤q≤2⋅1051 \leq q \leq 2\cdot10^5)——分别表示数组 aa、bb 的长度以及查询次数。

下一行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤a1≤a2≤⋯≤an≤1090 \leq a_1 \leq a_2 \leq \cdots \leq a_n \leq 10^9)——即数组 aa。保证 aa 是按非递减顺序排序的。

再下一行包含 nn 个整数 b1,b2,…,bnb_1,b_2,\ldots,b_n(−109≤bi≤109-10^9 \leq b_i \leq 10^9,且 bi≠0b_i \neq 0)——即数组 bb。

接下来的 qq 行中,第 ii 行包含两个整数 li,ril_i,r_i(1≤li≤ri≤n1 \leq l_i \leq r_i \leq n)。保证 ∑j=liribj=0\sum\limits_{j = l_i}^{r_i} b_j = 0。

输出格式

For each query lil_i, rir_i — print the cost of subarray a[li:ri]a[l_i:r_i] modulo 109+710^9 + 7.

对于每个查询 lil_i、rir_i,输出子数组 a[li:ri]a[l_i:r_i] 的代价对 109+710^9 + 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 11 i.e one unit from source to 22, then one unit from 22 to 33, then one unit from 33 to sink. The cost of the flow is 0⋅1+∣2−4∣⋅1+0⋅1=20 \cdot 1 + |2 - 4| \cdot 1 + 0 \cdot 1 = 2.

In the second query, the maximum possible flow is again 11 i.e from source to 77, 77 to 66, and 66 to sink with a cost of 0⋅∣10−10∣⋅1+0⋅1=00 \cdot |10 - 10| \cdot 1 + 0 \cdot 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 33 with a cost of 0⋅3+1⋅1+4⋅2+0⋅1+0⋅2=90 \cdot 3 + 1 \cdot 1 + 4 \cdot 2 + 0 \cdot 1 + 0 \cdot 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.

在第一次查询中,最大可能流为 11,即:11 个单位从源点流向节点 22,再从节点 22 流向节点 33,最后从节点 33 流向汇点。该流的费用为 0⋅1+∣2−4∣⋅1+0⋅1=20 \cdot 1 + |2 - 4| \cdot 1 + 0 \cdot 1 = 2。

在第二次查询中,最大可能流仍为 11,即:从源点流向节点 77,再从节点 77 流向节点 66,最后从节点 66 流向汇点,其费用为 0⋅∣10−10∣⋅1+0⋅1=00 \cdot |10 - 10| \cdot 1 + 0 \cdot 1 = 0。

在第三次查询中,左侧展示了流网络图,其中每条边上的数值表示容量,括号内数值表示单位流量费用。右侧图像展示了最优配置下各条边上的实际流量。

该网络的最大流为 33,对应最小费用为 0⋅3+1⋅1+4⋅2+0⋅1+0⋅2=90 \cdot 3 + 1 \cdot 1 + 4 \cdot 2 + 0 \cdot 1 + 0 \cdot 2 = 9。

在第四次查询中,流网络结构如下所示:

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

上述网络中的最大流为 44,且该最大流对应的最小费用为 1515。

输入解题思路,AI测评打分。不知道怎么写?

首页