All submissions for this problem are available.### Read problems statements in [Hindi](http://www.codechef.com/download/translated/COOK109/hindi/MAGPER.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/COOK109/mandarin/MAGPER.pdf), [Russian](http://www.codechef.com/download/translated/COOK109/russian/MAGPER.pdf), [Vietnamese](http://www.codechef.com/download/translated/COOK109/vietnamese/MAGPER.pdf), and [Bengali](http://www.codechef.com/download/translated/COOK109/bengali/MAGPER.pdf) as well. Saeed bought two magic permutations of integers $1$ through $N$ and gave them to his grandsons Rami and Ghoda to encourage them to train in problem solving. Rami found out that he can only cyclically shift his permutation, i.e. choose any number of elements from the beginning and move them to the end without changing their order. On the other hand, Ghoda found out that he can only swap elements of his permutation. The distance between two permutations $P_1, P_2, \ldots, P_N$ and $R_1, R_2, \ldots, R_N$ is the sum of $|i-j|$ over all pairs of valid indices $i, j$ such that $P_i = R_j$. You should process $Q$ queries. In each query, one of Saeed's grandsons plays with his permutation (performs an operation described above), and afterwards, you should tell Saeed the distance between his grandsons' current permutations. Note that the queries are cumulative — the changes made in one query remain in subsequent queries. ### Input - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$, denoting Rami's initial permutation. - The third line contains $N$ space-separated integers $B_1, B_2, \ldots, B_N$, denoting Ghoda's initial permutation. - Each of the next $Q$ lines describes a query in one of the following formats: - `1 z`: Rami moves $z$ elements from the beginning of his permutation to the end. - `2 x y`: Ghoda swaps the $x$-th and $y$-th element of his permutation. ### Output For each query, print a single line containing one integer — the current distance between the permutations. ### Constraints - $1 \le T \le 100$ - $2 \le N, Q \le 10^5$ - $1 \le x, y \le N$ - $x \neq y$ - $1 \le z \le N-1$ - $1 \le A_i, B_i \le N$ for each valid $i$ - $A$ and $B$ are permutations - the sum of $N$ over all test cases does not exceed $10^5$ - the sum of $Q$ over all test cases does not exceed $10^5$ ### Example Input ``` 1 4 3 1 3 2 4 4 3 2 1 1 1 2 3 1 1 2 ``` ### Example Output ``` 4 2 8 ``` ### Explanation **Example case 1:** - Rami's permutation is $(1, 3, 2, 4) \rightarrow (3, 2, 4, 1) \rightarrow (3, 2, 4, 1) \rightarrow (4, 1, 3, 2)$. - Ghoda's permutation is $(4, 3, 2, 1) \rightarrow (4, 3, 2, 1) \rightarrow (2, 3, 4, 1) \rightarrow (2, 3, 4, 1)$.
|Tags||cook109, i_love_islam, lazypropagation, medium-hard, segment-tree, taran_1407|
|Time Limit:||1.5 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.