Equilibrium solution codeforces

Equilibrium solution codeforces

William has two arrays aa and bb, each consisting of nn items.

For some segments l..rl..r of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each ii from ll to rr holds ai=biai=bi. Equilibrium solution codeforces

To perform a balancing operation an even number of indices must be selected, such that lpos1<pos2<<poskrl≤pos1<pos2<⋯<posk≤r. Next the items of array a at positions pos1,pos3,pos5,pos1,pos3,pos5,… get incremented by one and the items of array b at positions pos2,pos4,pos6,pos2,pos4,pos6,… get incremented by one.

William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently.

Input Equilibrium solution codeforces

The first line contains a two integers nn and qq (2n1052≤n≤1051q1051≤q≤105), the size of arrays aa and bb and the number of segments.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai109)(0≤ai≤109).

The third line contains nn integers b1,b2,,bnb1,b2,…,bn (0bi109)(0≤bi≤109).

Each of the next qq lines contains two integers lili and riri (1li<rin)(1≤li<ri≤n), the edges of segments.

Output Equilibrium solution codeforces

For each segment output a single number — the minimal number of balancing operations needed or “-1” if it is impossible to equalize segmens of arrays.


Copy Equilibrium solution codeforces
8 5
0 1 2 9 3 2 7 5
2 2 1 9 4 1 5 8
2 6
1 7
2 4
7 8
5 8

Note Equilibrium solution codeforces

For the first segment from 22 to 66 you can do one operation with pos=[2,3,5,6]pos=[2,3,5,6], after this operation the arrays will be: a=[0,2,2,9,4,2,7,5]a=[0,2,2,9,4,2,7,5]b=[2,2,2,9,4,2,5,8]b=[2,2,2,9,4,2,5,8]. Arrays are equal on a segment from 22 to 66 after this operation.

For the second segment from 11 to 77 you can do three following operations:

  1. pos=[1,3,5,6]pos=[1,3,5,6]
  2. pos=[1,7]pos=[1,7]
  3. pos=[2,7]pos=[2,7]

After these operations, the arrays will be: a=[2,2,2,9,4,2,7,5]a=[2,2,2,9,4,2,7,5]b=[2,2,2,9,4,2,7,8]b=[2,2,2,9,4,2,7,8]. Arrays are equal on a segment from 11 to 77 after these operations.

For the third segment from 22 to 44 you can do one operation with pos=[2,3]pos=[2,3], after the operation arrays will be: a=[0,2,2,9,3,2,7,5]a=[0,2,2,9,3,2,7,5]b=[2,2,2,9,4,1,5,8]b=[2,2,2,9,4,1,5,8]. Arrays are equal on a segment from 22 to 44 after this operation.

It is impossible to equalize the fourth and the fifth segment.

2 thoughts on “Equilibrium solution codeforces”

  1. Pingback: Amazon Quiz Answers today 31 August 2021- 100% correct answers – Hindimaintutorial

  2. Pingback: Amazon Quiz Answers today 1 September 2021- 100% correct answers – Hindimaintutorial

Leave a Comment

Your email address will not be published. Required fields are marked *