#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 songs, numbered from to .
Initially, the app generates a playlist for these songs in the form of a permutation of , denoted by . It plays the songs in the order : song plays first, song plays second, and so on. The playlist is infinite: each time after song plays, it starts all over from song .
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 songs, denoted by . This means that you want to listen to songs in full in the order of 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 , the second to be song , and so on, until the -th is song . After listening to these songs in full, you stop listening.
You use this playlist for days. Between each pair of consecutive days, you update the permutations using three integers , , and as follows:
- If , then you swap and .
- If , then you swap and .
Note that the effect of each update persists to subsequent days.
For each day, assuming you start listening from song , determine the minimum number of times you need to press the skip button so that the songs you listen to in full are in this order.
输入格式
The first line of input contains two integers and ( ; ).
The second line contains integers representing the initial values of ( ; for all ).
The third line contains integers representing the initial values of ( ; for all ).
The -th of the next lines contains three integers , , and ( ; ), representing the update between the -th and -th days.
输出格式
Output lines, where the -th line should contain the minimum number of skips for the -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, and . You can listen to these songs in full in the desired order with skips:
- Song plays. Skip this song.
- Song plays. Skip this song.
- Song plays. Skip this song.
- Song plays. Listen to this song in full.
- Song plays. Skip this song.
- Song plays. Skip this song.
- Song plays. Listen to this song in full.
- Song plays. Skip this song.
- Song plays. Listen to this song in full.
- Song 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 . The minimum number of skips is .
On the third day, the permutations are and $(b_1, \ldots, b_4) = (\mathbf{1}, 2, \mathbf{3}, 4)$ . The minimum number of skips is .