CF1556E.Equilibrium

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

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 l≤pos1<pos2<⋯<posk≤rl \le pos_1 \lt pos_2 \lt \dots \lt 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.

威廉有两组数组 aa 和 bb,每组均包含 nn 个元素。

对于这两组数组的某些区间 l..rl..r,威廉希望判断:能否通过若干次“平衡操作”使该区间内所有对应位置的元素值相等。形式化地说,即对每个从 ll 到 rr 的下标 ii,均有 ai=bia_i = b_i。

一次平衡操作要求选取一个偶数个下标 pos1<pos2<⋯<poskpos_1 < pos_2 < \dots < pos_k,满足 l≤pos1<pos2<⋯<posk≤rl \le pos_1 < pos_2 < \dots < pos_k \le r;然后将数组 aa 在位置 pos1,pos3,pos5,…pos_1, pos_3, pos_5, \dots(即奇数序号的位置)上的元素各加 11,并将数组 bb 在位置 pos2,pos4,pos6,…pos_2, pos_4, pos_6, \dots(即偶数序号的位置)上的元素各加 11。

威廉希望确定:对每一个给定区间,是否可通过若干次平衡操作使两数组在该区间内对应元素全部相等;若可以,最少需要多少次操作。注意:对每个区间,其平衡操作是独立进行的。

输入格式

The first line contains a two integers nn and qq (2≤n≤1052 \le n \le 10^5, 1≤q≤1051 \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 (0≤ai≤109)(0 \le a_i \le 10^9).

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

Each of the next qq lines contains two integers lil_i and rir_i (1≤li<ri≤n)(1 \le l_i \lt r_i \le n), the edges of segments.

第一行包含两个整数 nn 和 qq(2≤n≤1052 \le n \le 10^5,1≤q≤1051 \le q \le 10^5),分别表示数组 aa 和 bb 的大小以及区间的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤1090 \le a_i \le 10^9)。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(0≤bi≤1090 \le b_i \le 10^9)。

接下来的 qq 行中,每行包含两个整数 lil_i 和 rir_i(1≤li<ri≤n1 \le l_i \lt r_i \le n),表示区间的左右端点。

输出格式

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。

输入输出样例

  • 输入#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.

对于第一个区间 22 到 66,你可以执行一次操作,其中 pos=[2,3,5,6]pos = [2, 3, 5, 6];执行该操作后,数组将变为: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]。此时,数组在区间 22 到 66 上相等。

对于第二个区间 11 到 77,你可以执行以下三次操作:

  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]

执行这些操作后,数组将变为: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]。此时,数组在区间 11 到 77 上相等。

对于第三个区间 22 到 44,你可以执行一次操作,其中 pos=[2,3]pos = [2, 3];执行该操作后,数组将变为: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]。此时,数组在区间 22 到 44 上相等。

第四个和第五个区间无法被统一(即无法使两数组在对应区间上完全相等)。

输入解题思路,AI测评打分。不知道怎么写?

首页