Under construction

Still being built. Things may change

Similarity

Time limit3sMemory limit1024 MB

Summary
Find the max intersection size of a length-K window of p and a length-K window of q, and count the window pairs achieving it, after each adjacent swap in p.
Level

Hard8 of 10

Topics
Sliding window, Segment tree, Array
Solved
No attempts yet

Problem

She was holding yellow flowers that were ugly and disturbing. Still, he liked her.

According to a well-known theorem, the personality of each person can be represented by a permutation of length NN. So the personality of our hero, Majstor, can be represented by a permutation pp, and the personality of Margarita, the lady who caught his eye, can be represented by a permutation qq.

The similarity of two permutations, which measures how well a marriage will go, is the largest size of the intersection of a subinterval of length KK of pp and a subinterval of length KK of qq. The intersection is taken as sets, so the order of numbers inside the subintervals does not matter. For example, when N=4N = 4 and K=3K = 3, the similarity of (2 4 1 3)(2\,4\,1\,3) and (1 2 3 4)(1\,2\,3\,4) is 22, and this value is obtained for any choice of subintervals.

Since he saw Margarita, Majstor has been obsessed with the similarity between his permutation and hers, and his personality has become very changeable. Every day, two adjacent elements of his permutation are swapped. The change is permanent, so the two elements stay swapped on the following days. He wants to know the similarity when he first saw her, and the similarity after each of the next QQ days of changes. He also wants to know the number of pairs of subintervals that achieve this similarity. Help him with his lovesick problem.

Input

The first line contains the numbers NN, KK, and QQ.

The second line contains NN numbers, where the ii-th number is pip_i.

The third line contains NN numbers, where the jj-th number is qjq_j.

Each of the next QQ lines describes a change. The ii-th of these lines contains a number tit_i (1≤ti<N1 \le t_i < N), which means that the numbers at positions tit_i and ti+1t_i + 1 in Majstor's permutation pp are swapped.

Output

On the first line, print the initial similarity and the number of pairs of subintervals that achieve it.

On each of the next QQ lines, print the same two values after the change of that day.

Hint

These are the subintervals of length three. In the first permutation they are (2 4 1)(2\,4\,1) and (4 1 3)(4\,1\,3), and in the second they are (1 2 3)(1\,2\,3) and (2 3 4)(2\,3\,4). The intersections are:

{2,4,1}∩{1,2,3}={1,2},{2,4,1}∩{2,3,4}={2,4},\{2, 4, 1\} \cap \{1, 2, 3\} = \{1, 2\}, \quad \{2, 4, 1\} \cap \{2, 3, 4\} = \{2, 4\},

{4,1,3}∩{1,2,3}={1,3},{4,1,3}∩{2,3,4}={3,4},\{4, 1, 3\} \cap \{1, 2, 3\} = \{1, 3\}, \quad \{4, 1, 3\} \cap \{2, 3, 4\} = \{3, 4\},

So the similarity is 2, and it is achieved by four pairs of subintervals.

Examples3

  1. Example 1

    Input
    2 1 1
    1 2
    1 2
    1
    
    Expected output
    1 2
    1 2
    
  2. Example 2

    Input
    4 3 0
    2 4 1 3
    1 2 3 4
    
    Expected output
    2 4
    
  3. Example 3

    Input
    5 3 3
    1 4 3 2 5
    4 5 1 2 3
    3
    1
    4
    
    Expected output
    2 5
    2 6
    3 1
    3 1