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 (bi0 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=lrbj0, then the cost is not defined.

  • Otherwise:

    • Construct a bipartite flow graph with rl+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 li,jrbi<0 and bj>0, draw an edge from i to j with infinite capacity and cost of unit flow as |aiaj|.
    • Add two more vertices: source S and sink T.
    • For each i such that lir and bi<0, add an edge from S to i with cost 0 and capacity |bi|.
    • For each i such that lir 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 (2n2105,1q2105)  — length of arrays ab and the number of queries.

The next line contains n integers a1,a2an (0a1a2an109)  — the array a. It is guaranteed that a is sorted in non-decreasing order.

The next line contains n integers b1,b2bn (109bi109,bi0)  — the array b.

The i-th of the next q lines contains two integers li,ri (1lirin). It is guaranteed that j=liribj=0.

Output

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

No comments:

Post a Comment