2019 ICPC 南昌邀请赛 F.Sequence
题目描述
Oldjang has a sequence $A$ of length $n$, the $i_{th}$ number in which is $A_i$. He defined a function $f(l,r) = a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$. It is simple for the cleverest boy oldjang to calculate the function, so he wants to make the problem more difficult for fun. He will perform two operations:
The operation expressed as “$0 \ x \ y$”, which means he changes the $x_{th}$ number to $y$;
The operation expressed as “$1 \ l \ r$”, which means he wants to calculate the function $F(l,r)$. $F(l,r) = f(l,l) \oplus f(l,l +1) \oplus \cdots \oplus f(l,r)\oplus f(l + 1,l +1) \oplus \cdots f(l + 1,r) \oplus \cdots \oplus f(r,r)$.That means $F(l,r)$ equals to the x or sum of all $f(i,j)$ satisfied $l \le i \le j \le r$.
Oldjang thinks the problem is still too simple for him, so he gives this task to you.
输入描述
The first line contains a integers $T(1 \le T \le 10)$ — the number of test cases .
The second line contains two integers $n,m(1 \le n,m \le 10^5)$, which means the sequence has $n$ numbers and oldjang has $m$ operations.
The next line contains $n$ integers — the sequence $A$.
In the following $m$ lines, each line contains three integers “$0 \ x \ y$” or “$1 \ l \ r$” ,descriping a operation.
输出描述
For each test case, print one line containing “Case # $x$: “ first, where $x$ is the test case number (starting from $1$).
Then output the answer after each the operations of second type.
样例输入
1 | 1 |
样例输出
1 | Case #1: |
思路
由题意和亦或的性质可得,当区间长度为奇数时
$$
F(l,r)=a_l \oplus a_{l+2} \oplus \cdots \oplus a_r
$$
当区间长度为偶数时
$$
F(l,r)=0
$$
题目是对区间的点更新和区间查询,考虑到更新和查询都是在线的,数据规模是$1e5$,需要$O(n\log_2n)$的时间复杂度才能通过。
当区间长度为奇数时,我们需要计算$a_l \oplus a_{l+2} \oplus \cdots \oplus a_r$,由于奇数项的求和与偶数项的求和互不相关,所以采用建两个树状数组的方式,分别维护奇数项和偶数项的更新和查询。
代码
1 |
|