Equilibrium solution codeforces
For some segmentsof 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 from to holds . Equilibrium solution codeforces
To perform a balancing operation an even number of indices must be selected, such that array a at positions get incremented by one and the items of array b at positions get incremented by one.. Next the items of
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.
The first line contains a two integersand ( , ), the size of arrays and and the number of segments.
The second line containsintegers .
The third line containsintegers .
Each of the nextlines contains two integers and , the edges of segments.
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.
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
1 3 1 -1 -1
For the first segment fromto you can do one operation with , after this operation the arrays will be: , . Arrays are equal on a segment from to after this operation.
For the second segment fromto you can do three following operations:
After these operations, the arrays will be:, . Arrays are equal on a segment from to after these operations.
For the third segment fromto you can do one operation with , after the operation arrays will be: , . Arrays are equal on a segment from to after this operation.
It is impossible to equalize the fourth and the fifth segment.