GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Sunday, 22 May 2022

[Solution] MCMF? Codeforces Solution | Codeforces Problem Solution 2022

F. MCMF?
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ∑j=lrbj≠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|.

Solution Click Below:-  CLICK HERE

Input

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 ∑j=liribj=0.

Output

For each query li, ri  — print the cost of subarray a[li:ri] modulo 109+7.

No comments:

Post a Comment