CF2153F.Odd Queries on Odd Array

省选/NOI-

通过率:0%

时间限制:10.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

An array bb of length mm is cute if there do not exist four indices 1≤i<j<k<l≤m1\le i \lt j \lt k \lt l \le m such that bi≠bjb_i\neq b_j, bi=bkb_i = b_k and bj=blb_j = b_l.

The beauty of an array bb of length mm is defined as the sum of all distinct values that appear an odd number of times in array bb. Formally, let cnt⁡(b,x)\operatorname{cnt}(b, x) denote the number of times the value xx appears in array bb. Then, the beauty is given by $$\sum\limits_{\substack{x\in \mathbb{Z}\\\operatorname{cnt}(b, x)\text{ is odd}}}x.$$

You are given a cute array aa of length nn, and you need to answer qq queries online. Each query consists of two integers ll and rr (1≤l≤r≤n1\le l\le r\le n), and you must compute the beauty of the subarray al…ra_{l\ldots r}∗^{\text{∗}}. Note that the queries are encoded; each subsequent query can only be decoded after calculating the answer to the preceding query.

∗^{\text{∗}}The subarray al…ra_{l \ldots r} refers to the contiguous segment of the array aa that starts at index ll and ends at index rr, i.e., [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r].

长度为 mm 的数组 bb 被称为“可爱的”(cute),当且仅当不存在四个下标 1≤i<j<k<l≤m1\le i \lt j \lt k \lt l \le m,使得 bi≠bjb_i\neq b_j,bi=bkb_i = b_k 且 bj=blb_j = b_l。

长度为 mm 的数组 bb 的“美观度”(beauty)定义为:在数组 bb 中出现奇数次的所有不同数值之和。形式化地,令 cnt⁡(b,x)\operatorname{cnt}(b, x) 表示数值 xx 在数组 bb 中出现的次数,则美观度为

∑x∈Zcnt⁡(b,x) 是奇数x.\sum\limits_{\substack{x\in \mathbb{Z}\\\operatorname{cnt}(b, x)\text{ 是奇数}}}x.

你被给定一个长度为 nn 的可爱数组 aa,并需要在线回答 qq 个查询。每个查询包含两个整数 ll 和 rr(满足 1≤l≤r≤n1\le l\le r\le n),你需要计算子数组 al…ra_{l\ldots r}∗^{\text{∗}} 的美观度。注意:这些查询是经过编码的;每一个后续查询只有在计算出前一个查询的答案后才能解码。

∗^{\text{∗}} 子数组 al…ra_{l \ldots r} 指的是数组 aa 中从下标 ll 开始、到下标 rr 结束的连续段,即 [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r]。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (1≤n,q≤5⋅1051\le n, q\le 5\cdot 10^5) — the length of array aa and the number of queries.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1\le a_i\le n) — the elements of the cute array aa.

The ii-th of the next qq lines contains two integers xi′x'_i and yi′y'_i (1≤xi′,yi′≤n1\le x'_i, y'_i\le n) — the endpoints of the queried subarray in an encoded form.

Let ansi\text{ans}_i be the answer to the ii-th query, with ans0=0\text{ans}_0 = 0. Then, we calculate xi=((xi′−1+ansi−1) mod n)+1x_i = ((x'_i - 1 + \text{ans}_{i - 1}) \bmod n) + 1 and yi=((yi′−1+ansi−1) mod n)+1y_i = ((y'_i - 1 + \text{ans}_{i - 1}) \bmod n) + 1. The endpoints of the ii-th queried subarray, lil_i and rir_i, are decoded as li=min⁡(xi,yi)l_i = \min(x_i, y_i) and ri=max⁡(xi,yi)r_i = \max(x_i, y_i).

It is guaranteed that the given array aa satisfies the conditions of a cute array.

It is guaranteed that the sum of nn and the sum of qq over all test cases does not exceed 5⋅1055\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n,q≤5⋅1051\le n, q\le 5\cdot 10^5)——分别表示数组 aa 的长度和查询次数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1\le a_i\le n)——即“可爱数组” aa 的元素。

接下来 qq 行中的第 ii 行包含两个整数 xi′x'_i 和 yi′y'_i(1≤xi′,yi′≤n1\le x'_i, y'_i\le n)——表示以编码形式给出的第 ii 次查询所对应的子数组端点。

设 ansi\text{ans}_i 为第 ii 次查询的答案,且定义 ans0=0\text{ans}_0 = 0。然后我们计算 xi=((xi′−1+ansi−1) mod n)+1x_i = ((x'_i - 1 + \text{ans}_{i - 1}) \bmod n) + 1 和 yi=((yi′−1+ansi−1) mod n)+1y_i = ((y'_i - 1 + \text{ans}_{i - 1}) \bmod n) + 1。第 ii 次查询所对应子数组的端点 lil_i 和 rir_i 通过解码得到:li=min⁡(xi,yi)l_i = \min(x_i, y_i),ri=max⁡(xi,yi)r_i = \max(x_i, y_i)。

保证所给数组 aa 满足“可爱数组”的条件。

保证所有测试用例中 nn 的总和与 qq 的总和均不超过 5⋅1055\cdot 10^5。

输出格式

For each test case, output qq integers representing the answers to each query.

对于每个测试用例,输出 qq 个整数,分别表示每个查询的答案。

输入输出样例

  • 输入#1

    3
    11 4
    1 1 2 2 3 3 3 2 2 1 1
    7 10
    5 11
    8 6
    2 8
    6 2
    1 3 2 3 4 3
    1 6
    1 4
    3 6
    3 3 3
    1 1
    1 2
    3 1
    2 2
    2 3
    3 3

    输出#1

    4 5 4 0 
    10 6 
    3 0 3 3 0 3

说明/提示

In the first test case, the queries are as follows:

  • x_1=((7−1+0)bmod11)+1=7,quady_1=((10−1+0)bmod11)+1=10x\_1 = ((7 - 1 + 0) \\bmod 11) + 1 = 7, \\quad y\_1 = ((10 - 1 + 0) \\bmod 11) + 1 = 10

    l_1=min(7,10)=7,quadr_1=max(7,10)=10l\_1 = \\min(7, 10) = 7, \\quad r\_1 = \\max(7, 10) = 10

    Thus, the queried subarray is a7…10=[3,2,2,1]a_{7\ldots 10} = [3, 2, 2, 1]. Values 33 and 11 appear once (odd), and value 22 appears twice (even). Hence, the beauty is 3+1=43 + 1 = 4.

  • x_2=((5−1+4)bmod11)+1=9,quady_2=((11−1+4)bmod11)+1=4x\_2 = ((5 - 1 + 4) \\bmod 11) + 1 = 9, \\quad y\_2 = ((11 - 1 + 4) \\bmod 11) + 1 = 4

    l_2=min(9,4)=4,quadr_2=max(9,4)=9l\_2 = \\min(9, 4) = 4, \\quad r\_2 = \\max(9, 4) = 9

    Thus, the queried subarray is a4…9=[2,3,3,3,2,2]a_{4\ldots 9} = [2, 3, 3, 3, 2, 2]. Values 22 and 33 appear three times (odd). Hence, the beauty is 2+3=52 + 3 = 5.

  • x_3=((8−1+5)bmod11)+1=2,quady_3=((6−1+5)bmod11)+1=11x\_3 = ((8 - 1 + 5) \\bmod 11) + 1 = 2, \\quad y\_3 = ((6 - 1 + 5) \\bmod 11) + 1 = 11

    l_3=min(2,11)=2,quadr_3=max(2,11)=11l\_3 = \\min(2, 11) = 2, \\quad r\_3 = \\max(2, 11) = 11

    Thus, the queried subarray is a2…11=[1,2,2,3,3,3,2,2,1,1]a_{2\ldots 11} = [1, 2, 2, 3, 3, 3, 2, 2, 1, 1]. Values 11 and 33 appear three times (odd), and value 22 appears four times (even). Hence, the beauty is 1+3=41 + 3 = 4.

  • x_4=((2−1+4)bmod11)+1=6,quady_4=((8−1+4)bmod11)+1=1x\_4 = ((2 - 1 + 4) \\bmod 11) + 1 = 6, \\quad y\_4 = ((8 - 1 + 4) \\bmod 11) + 1 = 1

    l_4=min(6,1)=1,quadr_4=max(6,1)=6l\_4 = \\min(6, 1) = 1, \\quad r\_4 = \\max(6, 1) = 6

    Thus, the queried subarray is a1…6=[1,1,2,2,3,3]a_{1\ldots 6} = [1, 1, 2, 2, 3, 3]. Values 11, 22, and 33 each appear twice (even). Hence, the beauty is 00.

In the second test case, the queries are as follows:

  • x_1=((1−1+0)bmod6)+1=1,quady_1=((6−1+0)bmod6)+1=6x\_1 = ((1 - 1 + 0) \\bmod 6) + 1 = 1, \\quad y\_1 = ((6 - 1 + 0) \\bmod 6) + 1 = 6

    l_1=min(1,6)=1,quadr_1=max(1,6)=6l\_1 = \\min(1, 6) = 1, \\quad r\_1 = \\max(1, 6) = 6

    Thus, the queried subarray is a1…6=[1,3,2,3,4,3]a_{1\ldots 6} = [1, 3, 2, 3, 4, 3]. Values 11, 22, and 44 appear once (odd), and value 33 appears three times (odd). Hence, the beauty is 1+2+3+4=101 + 2 + 3 + 4 = 10.

  • x_2=((1−1+10)bmod6)+1=5,quady_2=((4−1+10)bmod6)+1=2x\_2 = ((1 - 1 + 10) \\bmod 6) + 1 = 5, \\quad y\_2 = ((4 - 1 + 10) \\bmod 6) + 1 = 2

    l_2=min(5,2)=2,quadr_2=max(5,2)=5l\_2 = \\min(5, 2) = 2, \\quad r\_2 = \\max(5, 2) = 5

    Thus, the queried subarray is a2…5=[3,2,3,4]a_{2\ldots 5} = [3, 2, 3, 4]. Values 22 and 44 appear once (odd), and value 33 appears twice (even). Hence, the beauty is 2+4=62 + 4 = 6.

In the third test case, all elements of array aa are equal to 33.

  • For any subarray of odd length, the value 33 appears an odd number of times, so the beauty is 33.
  • For any subarray of even length, the value 33 appears an even number of times, so the beauty is 00.

在第一个测试用例中,查询如下:

  • x_1=((7−1+0)bmod11)+1=7,quady_1=((10−1+0)bmod11)+1=10x\_1 = ((7 - 1 + 0) \\bmod 11) + 1 = 7, \\quad y\_1 = ((10 - 1 + 0) \\bmod 11) + 1 = 10

    l_1=min(7,10)=7,quadr_1=max(7,10)=10l\_1 = \\min(7, 10) = 7, \\quad r\_1 = \\max(7, 10) = 10

    因此,所查询的子数组为 a7…10=[3,2,2,1]a_{7\ldots 10} = [3, 2, 2, 1]。其中值 33 和 11 各出现一次(奇数次),值 22 出现两次(偶数次)。故美丽值为 3+1=43 + 1 = 4。

  • x_2=((5−1+4)bmod11)+1=9,quady_2=((11−1+4)bmod11)+1=4x\_2 = ((5 - 1 + 4) \\bmod 11) + 1 = 9, \\quad y\_2 = ((11 - 1 + 4) \\bmod 11) + 1 = 4

    l_2=min(9,4)=4,quadr_2=max(9,4)=9l\_2 = \\min(9, 4) = 4, \\quad r\_2 = \\max(9, 4) = 9

    因此,所查询的子数组为 a4…9=[2,3,3,3,2,2]a_{4\ldots 9} = [2, 3, 3, 3, 2, 2]。其中值 22 和 33 各出现三次(奇数次)。故美丽值为 2+3=52 + 3 = 5。

  • x_3=((8−1+5)bmod11)+1=2,quady_3=((6−1+5)bmod11)+1=11x\_3 = ((8 - 1 + 5) \\bmod 11) + 1 = 2, \\quad y\_3 = ((6 - 1 + 5) \\bmod 11) + 1 = 11

    l_3=min(2,11)=2,quadr_3=max(2,11)=11l\_3 = \\min(2, 11) = 2, \\quad r\_3 = \\max(2, 11) = 11

    因此,所查询的子数组为 a2…11=[1,2,2,3,3,3,2,2,1,1]a_{2\ldots 11} = [1, 2, 2, 3, 3, 3, 2, 2, 1, 1]。其中值 11 和 33 各出现三次(奇数次),值 22 出现四次(偶数次)。故美丽值为 1+3=41 + 3 = 4。

  • x_4=((2−1+4)bmod11)+1=6,quady_4=((8−1+4)bmod11)+1=1x\_4 = ((2 - 1 + 4) \\bmod 11) + 1 = 6, \\quad y\_4 = ((8 - 1 + 4) \\bmod 11) + 1 = 1

    l_4=min(6,1)=1,quadr_4=max(6,1)=6l\_4 = \\min(6, 1) = 1, \\quad r\_4 = \\max(6, 1) = 6

    因此,所查询的子数组为 a1…6=[1,1,2,2,3,3]a_{1\ldots 6} = [1, 1, 2, 2, 3, 3]。其中值 11、22 和 33 各出现两次(偶数次)。故美丽值为 00。

在第二个测试用例中,查询如下:

  • x_1=((1−1+0)bmod6)+1=1,quady_1=((6−1+0)bmod6)+1=6x\_1 = ((1 - 1 + 0) \\bmod 6) + 1 = 1, \\quad y\_1 = ((6 - 1 + 0) \\bmod 6) + 1 = 6

    l_1=min(1,6)=1,quadr_1=max(1,6)=6l\_1 = \\min(1, 6) = 1, \\quad r\_1 = \\max(1, 6) = 6

    因此,所查询的子数组为 a1…6=[1,3,2,3,4,3]a_{1\ldots 6} = [1, 3, 2, 3, 4, 3]。其中值 11、22 和 44 各出现一次(奇数次),值 33 出现三次(奇数次)。故美丽值为 1+2+3+4=101 + 2 + 3 + 4 = 10。

  • x_2=((1−1+10)bmod6)+1=5,quady_2=((4−1+10)bmod6)+1=2x\_2 = ((1 - 1 + 10) \\bmod 6) + 1 = 5, \\quad y\_2 = ((4 - 1 + 10) \\bmod 6) + 1 = 2

    l_2=min(5,2)=2,quadr_2=max(5,2)=5l\_2 = \\min(5, 2) = 2, \\quad r\_2 = \\max(5, 2) = 5

    因此,所查询的子数组为 a2…5=[3,2,3,4]a_{2\ldots 5} = [3, 2, 3, 4]。其中值 22 和 44 各出现一次(奇数次),值 33 出现两次(偶数次)。故美丽值为 2+4=62 + 4 = 6。

在第三个测试用例中,数组 aa 的所有元素均等于 33。

  • 对于任意长度为奇数的子数组,值 33 出现奇数次,因此美丽值为 33。
  • 对于任意长度为偶数的子数组,值 33 出现偶数次,因此美丽值为 00。

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

首页