The Fair Nut and Amusing Xor
Contest: Codeforces Round 526 (Div. 1)
Difficulty rating: 3300
Tags: data structures

The Fair Nut has two arrays $$$a$$$ and $$$b$$$, consisting of $$$n$$$ numbers. He found them so long ago that no one knows when they came to him.

The Fair Nut often changes numbers in his arrays. He also is interested in how similar $$$a$$$ and $$$b$$$ are after every modification.

Let's denote similarity of two arrays as the minimum number of operations to apply to make arrays equal (every operation can be applied for both arrays). If it is impossible, similarity will be equal $$$-1$$$.

Per one operation you can choose a subarray with length $$$k$$$ ($$$k$$$ is fixed), and change every element $$$a_i$$$, which belongs to the chosen subarray, to $$$a_i \oplus x$$$ ($$$x$$$ can be chosen), where $$$\oplus$$$ denotes the bitwise XOR operation.

Nut has already calculated the similarity of the arrays after every modification. Can you do it?

Note that you just need to calculate those values, that is you do not need to apply any operations.

Input format:
The first line contains three numbers $$$n$$$, $$$k$$$ and $$$q$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$, $$$0 \le q \le 2 \cdot 10^5$$$) — the length of the arrays, the length of the subarrays, to which the operations are applied, and the number of queries.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i < 2^{14}$$$) — elements of array $$$a$$$.

The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i < 2^{14}$$$) — elements of array $$$b$$$.

Each of the next $$$q$$$ lines describes query and contains string $$$s$$$ and two integers $$$p$$$ and $$$v$$$ ($$$1 \le p \le n$$$, $$$0 \le v < 2^{14}$$$) — array, which this query changes («a» or «b» without quotes), index of changing element and its new value.

Output format:
On the first line print initial similarity of arrays $$$a$$$ and $$$b$$$.

On the $$$i$$$-th of following $$$q$$$ lines print similarity of $$$a$$$ and $$$b$$$ after applying first $$$i$$$ modifications.

Constraints:
Time limit: 4.0 s
Memory limit: 256.0 MB

Note:
In the first sample making arrays $$$[0, 4, 2]$$$ and $$$[1, 2, 3]$$$ is impossible with $$$k=3$$$. After the modification, you can apply the operation with $$$x=1$$$ to the whole first array (its length is equal to $$$k$$$), and it will be equal to the second array.

In order to make arrays equal in the second sample before changes, you can apply operations with $$$x=1$$$ on subarray $$$[1, 2]$$$ of $$$a$$$ and with $$$x=2$$$ on subarray $$$[2, 3]$$$ of $$$b$$$.

After all queries arrays will be equal $$$[0, 3, 2]$$$ and $$$[1, 0, 0]$$$. The same operations make them equal $$$[1, 2, 2]$$$.

Example input:
3 3 1
0 4 2
1 2 3
b 2 5

Example output:
-1
1

Example input:
3 2 2
1 3 2
0 0 0
a 1 0
b 1 1

Example output:
2
-1
2

Editorial:
Let $$$c_{i}=a_{i} \oplus b_{i}$$$.
Let's notice that if there is list of operations to both arrays, which makes them equal, applying these operations to $$$a$$$ makes it equal to $$$b$$$. Because of this, applying them to $$$c$$$ makes all elements equal to $$$0$$$.
Now we are processing modifications of $$$c$$$. Let's make array $$$d$$$ with length $$$n + 1$$$, consisting of $$$c_{0}$$$, $$$n - 1$$$ values $$$c_{i} \oplus c_{i + 1}$$$ for $$$1 \le i \le n - 1$$$, and $$$c_{n - 1}$$$. The only result in $$$d$$$ of applying operation to $$$c$$$ is changing two elements $$$d_{i}$$$, $$$d_{i + k}$$$ to $$$d_{i} \oplus x$$$, $$$d_{i + k} \oplus x$$$.
Let's divide array $$$d$$$ into $$$k$$$ groups. Elements with the same indexes by modulo $$$k$$$ will be in one group. Tasks for different groups are independent and matches up to the initial problem for $$$k = 2$$$.
Now we are solving problem for $$$k = 2$$$. Let's calculate prefix xors. Operation with $$$k = 2$$$ changes only one prefix xor (and it is not the last prefix xor). So, if the last prefix xor isn't equal to 0, the answer is $$$-1$$$. Otherwise, minimal number of operations to apply is number of prefix xors, which are not equal to $$$0$$$.
Let's see what modification does with prefix xors. It changes all elements from $$$a$$$ to $$$a \oplus x$$$ on some suffix. To recalculate number of zeros fastly, let's divide array to blocks with length $$$O(\sqrt n)$$$ and keep in every block count of every element and modifier. To process modification, let's update modifier for all blocks, which are covered by suffix and recalculate all counts for block, which is partly covered by it. Number of zeros in a block is count of elements, which are equal to modifier. We also have to store the last element of array, which is equal to xor of all $$$x$$$ from modifications.
Note, that this task is solved for each of $$$k$$$ groups, so we have to store sum of counts of non-zero prefix xors, and number of groups, where the last prefix xor is not zero.