#P16568. [ICPC 2026 APC] Worldwide Playlist

[ICPC 2026 APC] Worldwide Playlist

题目描述

You are planning a trip around the world. For this trip, you have installed a music app containing n n songs, numbered from 1 1 to n n .

Initially, the app generates a playlist for these n n songs in the form of a permutation of 1,2,,n 1, 2, \ldots, n , denoted by a1,a2,,an a_1, a_2, \ldots, a_n . It plays the n n songs in the order a1,a2,,an a_1, a_2, \ldots, a_n : song a1 a_1 plays first, song a2 a_2 plays second, and so on. The playlist is infinite: each time after song an a_n plays, it starts all over from song a1 a_1 .

The next song starts playing automatically when the current song finishes. Before a song finishes, you may instead press a skip button to immediately jump to the next song in the playlist.

You have a desired permutation of the n n songs, denoted by b1,b2,,bn b_1, b_2, \ldots, b_n . This means that you want to listen to n n songs in full in the order of b1,b2,,bn b_1, b_2, \ldots, b_n by strategically pressing the skip button zero or more times. In other words, you want the first song you listen to in full (not skipped) to be song b1 b_1 , the second to be song b2 b_2 , and so on, until the n n -th is song bn b_n . After listening to these n n songs in full, you stop listening.

You use this playlist for d d days. Between each pair of consecutive days, you update the permutations using three integers c c , x x , and y y as follows:

  • If c=1 c = 1 , then you swap ax a_x and ay a_y .
  • If c=2 c = 2 , then you swap bx b_x and by b_y .

Note that the effect of each update persists to subsequent days.

For each day, assuming you start listening from song a1 a_1 , determine the minimum number of times you need to press the skip button so that the songs you listen to in full are b1,b2,,bn b_1, b_2, \ldots, b_n in this order.

输入格式

The first line of input contains two integers n n and d d ( 2n200000 2 \le n \le 200\,000 ; 2d200000 2 \le d \le 200\,000 ).

The second line contains n n integers representing the initial values of a1,a2,,an a_1, a_2, \ldots, a_n ( 1ain 1 \le a_i \le n ; aiaj a_i \neq a_j for all ij i \neq j ).

The third line contains n n integers representing the initial values of b1,b2,,bn b_1, b_2, \ldots, b_n ( 1bin 1 \le b_i \le n ; bibj b_i \neq b_j for all ij i \neq j ).

The k k -th of the next d1 d-1 lines contains three integers c c , x x , and y y ( c{1,2} c \in \{1, 2\} ; 1x<yn 1 \le x \lt y \le n ), representing the update between the k k -th and (k+1) (k+1) -th days.

输出格式

Output d d lines, where the k k -th line should contain the minimum number of skips for the k k -th day.

4 3
1 4 2 3
3 2 1 4
1 3 4
2 1 3
6
2
6
7 5
4 7 1 2 6 5 3
2 6 5 1 4 3 7
1 2 5
2 6 7
1 6 7
2 1 5
16
26
21
20
6

提示

Explanation for the sample input/output #1

On the first day, (a1,,a4)=(1,4,2,3) (a_1, \ldots, a_4) = (1, 4, 2, 3) and (b1,,b4)=(3,2,1,4) (b_1, \ldots, b_4) = (3, 2, 1, 4) . You can listen to these songs in full in the desired order with 6 6 skips:

  • Song 1 1 plays. Skip this song.
  • Song 4 4 plays. Skip this song.
  • Song 2 2 plays. Skip this song.
  • Song 3 3 plays. Listen to this song in full.
  • Song 1 1 plays. Skip this song.
  • Song 4 4 plays. Skip this song.
  • Song 2 2 plays. Listen to this song in full.
  • Song 3 3 plays. Skip this song.
  • Song 1 1 plays. Listen to this song in full.
  • Song 4 4 plays. Listen to this song in full.

On the second day, the permutations are $(a_1, \ldots, a_4) = (1, 4, \mathbf{3}, \mathbf{2})$ and (b1,,b4)=(3,2,1,4) (b_1, \ldots, b_4) = (3, 2, 1, 4) . The minimum number of skips is 2 2 .

On the third day, the permutations are (a1,,a4)=(1,4,3,2) (a_1, \ldots, a_4) = (1, 4, 3, 2) and $(b_1, \ldots, b_4) = (\mathbf{1}, 2, \mathbf{3}, 4)$ . The minimum number of skips is 6 6 .