#P10756. [COI 2023] Sličnost

[COI 2023] Sličnost

Background

Source: https://hsin.hr/hio2023/.

Problem Description

She was holding a bunch of disgusting, unsettling yellow flowers in her hands. But he ended up falling for her.

According to a well-known theorem, every person's personality can be represented by a permutation of length NN. Therefore, the personality of our protagonist—the master (Majstor)—can be represented by a permutation pp. The personality of the lady who caught his eye—Margarita—can be represented by a permutation qq.

The similarity (sličnost) of two permutations, that is, the happiness level of married life, is defined as follows: choose a subarray of length KK from permutation pp, and choose a subarray of length KK from permutation qq. Compute the size of the intersection of the elements contained in these two subarrays. Among all possible choices, the maximum such intersection size is the similarity. Here, the intersection is considered in the set sense, meaning the order of numbers inside the subarray does not matter. For example, when N=4,K=3N = 4, K = 3, the similarity of permutations (2 4 1 3)(2\ 4\ 1\ 3) and (1 2 3 4)(1\ 2\ 3\ 4) is 2, and this value can be achieved for any pair of chosen subarrays.

Ever since meeting Margarita, the master has been obsessed with the similarity of their permutations, and his personality has therefore become very changeable. Every day, two adjacent elements in his permutation swap positions. Note that this change is permanent, meaning those two elements will remain swapped in the following days. Now, he wants to know the similarity of his and her permutations when he first met her, and also the similarity after the change on each of the next QQ days. In addition, he also wants to know how many pairs of subarrays achieve this maximum similarity. Troubled by love, he asks for your help.

Input Format

The first line contains three integers N,KN, K and QQ.

The second line contains NN numbers forming the permutation pp.

The third line contains NN numbers forming the permutation qq.

The next QQ lines describe the changes. The ii-th line contains an integer tit_i (1ti<N1 \le t_i < N), meaning that in the master's permutation pp, the numbers at positions tit_i and ti+1t_i + 1 are swapped.

Output Format

On the first line, output the similarity of the initial permutations, and the number of pairs of subarrays that achieve this similarity.

On the next QQ lines, output the same two values after the change on that day.

2 1 1
1 2
1 2
1
1 2
1 2
4 3 0
2 4 1 3
1 2 3 4
2 4
5 3 3
1 4 3 2 5
4 5 1 2 3
3
1
4
2 5
2 6
3 1
3 1

Hint

Sample Explanation

Explanation of the second sample: in the first permutation, the subarrays of length 33 are (2 4 1)(2\ 4\ 1) and (4 1 3)(4\ 1\ 3); in the second permutation, they are (1 2 3)(1\ 2\ 3) and (2 3 4)(2\ 3\ 4). For the intersections, we have:

  • {2,4,1}{1,2,3}={1,2}\{2,4,1\} \cap \{1,2,3\} = \{1,2\}
  • {2,4,1}{2,3,4}={2,4}\{2,4,1\} \cap \{2,3,4\} = \{2,4\}
  • {4,1,3}{1,2,3}={1,3}\{4,1,3\} \cap \{1,2,3\} = \{1,3\}
  • {4,1,3}{2,3,4}={3,4}\{4,1,3\} \cap \{2,3,4\} = \{3,4\}

So we can see that the similarity is 22, and this similarity is achieved by four pairs of subarrays.

Constraints

In all subtasks, 2N1052 \leq N\leq 10^5, 1KN1 \leq K \leq N, 0Q1050 \leq Q \leq 10^5.

Subtask Score Constraints
11 77 Q=0,N100Q = 0, N \le 100
22 1010 Q=0,N5000Q = 0, N \le 5000
33 77 N=4N = 4
44 2020 N,Q100N, Q \le 100
55 2323 N,Q5000N, Q \le 5000
66 3333 No additional constraints

Translated by ChatGPT 5