Similarity
Time limit3sMemory limit1024 MB
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 . So the personality of our hero, Majstor, can be represented by a permutation , and the personality of Margarita, the lady who caught his eye, can be represented by a permutation .
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 of and a subinterval of length of . The intersection is taken as sets, so the order of numbers inside the subintervals does not matter. For example, when and , the similarity of and is , 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 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 , , and .
The second line contains numbers, where the -th number is .
The third line contains numbers, where the -th number is .
Each of the next lines describes a change. The -th of these lines contains a number (), which means that the numbers at positions and in Majstor's permutation 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 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 and , and in the second they are and . The intersections are:
So the similarity is 2, and it is achieved by four pairs of subintervals.