CF1556E.Equilibrium

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

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=bia_i = b_i .

To perform a balancing operation an even number of indices must be selected, such that lpos1<pos2<<poskrl \le pos_1 < pos_2 < \dots < pos_k \le r . Next the items of array a at positions pos1,pos3,pos5,pos_1, pos_3, pos_5, \dots get incremented by one and the items of array b at positions pos2,pos4,pos6,pos_2, pos_4, pos_6, \dots 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.

输入格式

The first line contains a two integers nn and qq ( 2n1052 \le n \le 10^5 , 1q1051 \le q \le 10^5 ), the size of arrays aa and bb and the number of segments.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (0ai109)(0 \le a_i \le 10^9) .

The third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (0bi109)(0 \le b_i \le 10^9) .

Each of the next qq lines contains two integers lil_i and rir_i (1li<rin)(1 \le l_i < r_i \le n) , 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 segments of arrays.

输入输出样例

  • 输入#1

    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

    1
    3
    1
    -1
    -1

说明/提示

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.

首页