CF2206J.Worldwide Playlist

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

Initially, the app generates a playlist for these nn songs in the form of a permutation of 1,2,…,n1, 2, \ldots, n, denoted by a1,a2,…,ana_1, a_2, \ldots, a_n. It plays the nn songs in the order a1,a2,…,ana_1, a_2, \ldots, a_n: song a1a_1 plays first, song a2a_2 plays second, and so on. The playlist is infinite: each time after song ana_n plays, it starts all over from song a1a_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 nn songs, denoted by b1,b2,…,bnb_1, b_2, \ldots, b_n. This means that you want to listen to nn songs in full in the order of b1,b2,…,bnb_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 b1b_1, the second to be song b2b_2, and so on, until the nn-th is song bnb_n. After listening to these nn songs in full, you stop listening.

You use this playlist for dd days. Between each pair of consecutive days, you update the permutations using three integers cc, xx, and yy as follows:

  • If c=1c = 1, then you swap axa_x and aya_y.
  • If c=2c = 2, then you swap bxb_x and byb_y.

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

For each day, assuming you start listening from song a1a_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,…,bnb_1, b_2, \ldots, b_n in this order.

你正在计划一次环球旅行。为此,你安装了一个音乐应用程序,其中包含 nn 首歌曲,编号为 11 到 nn。

初始时,该应用为这 nn 首歌曲生成一个播放列表,形式为 1,2,…,n1, 2, \ldots, n 的一个排列,记作 a1,a2,…,ana_1, a_2, \ldots, a_n。它按顺序 a1,a2,…,ana_1, a_2, \ldots, a_n 播放这 nn 首歌曲:第 a1a_1 首歌最先播放,第 a2a_2 首歌其次播放,依此类推。该播放列表是无限循环的:每当第 ana_n 首歌播放完毕后,立即从第 a1a_1 首歌重新开始。

当前歌曲播放结束后,下一首歌曲会自动开始播放。但在当前歌曲播放结束前,你也可以按下“跳过”按钮,立即跳转到播放列表中的下一首歌曲。

你有一个期望的 nn 首歌曲排列,记作 b1,b2,…,bnb_1, b_2, \ldots, b_n。这意味着,你需要通过有策略地零次或多次按下跳过按钮,完整收听(即不跳过)nn 首歌曲,并且顺序恰好为 b1,b2,…,bnb_1, b_2, \ldots, b_n。换言之,你希望你完整收听的第一首歌(未被跳过的首首)是 b1b_1,第二首是 b2b_2,……,第 nn 首是 bnb_n;在完整收听了这 nn 首歌之后,你便停止收听。

你将使用该播放列表共 dd 天。在每对相邻的两天之间,你将使用三个整数 cc、xx 和 yy 更新排列:

  • 若 c=1c = 1,则交换 axa_x 与 aya_y;
  • 若 c=2c = 2,则交换 bxb_x 与 byb_y。

注意:每次更新的效果将持续影响后续各天。

对每一天,在假设你从歌曲 a1a_1 开始收听的前提下,请确定你需要按下的跳过按钮的最少次数,使得你完整收听的歌曲序列恰好为 b1,b2,…,bnb_1, b_2, \ldots, b_n(按此顺序)。

输入格式

The first line of input contains two integers nn and dd (2≤n≤200 0002 \le n \le 200\,000; 2≤d≤200 0002 \le d \le 200\,000).

The second line contains nn integers representing the initial values of a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n; ai≠aja_i \neq a_j for all i≠ji \neq j).

The third line contains nn integers representing the initial values of b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤n1 \le b_i \le n; bi≠bjb_i \neq b_j for all i≠ji \neq j).

The kk-th of the next d−1d-1 lines contains three integers cc, xx, and yy (c∈1,2c \in {1, 2}; 1≤x<y≤n1 \le x \lt y \le n), representing the update between the kk-th and (k+1)(k+1)-th days.

输入的第一行包含两个整数 nn 和 dd(2≤n≤200 0002 \le n \le 200\,000;2≤d≤200 0002 \le d \le 200\,000)。

第二行包含 nn 个整数,表示初始值 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n;对所有 i≠ji \neq j,有 ai≠aja_i \neq a_j)。

第三行包含 nn 个整数,表示初始值 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤n1 \le b_i \le n;对所有 i≠ji \neq j,有 bi≠bjb_i \neq b_j)。

接下来的 d−1d-1 行中,第 kk 行包含三个整数 cc、xx 和 yy(c∈{1,2}c \in \{1, 2\};1≤x<y≤n1 \le x \lt y \le n),表示第 kk 天与第 k+1k+1 天之间的更新操作。

输出格式

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

输出 dd 行,其中第 kk 行应包含第 kk 天所需的最少跳过次数。

输入输出样例

  • 输入#1

    4 3
    1 4 2 3
    3 2 1 4
    1 3 4
    2 1 3

    输出#1

    6
    2
    6
  • 输入#2

    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

    输出#2

    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 66 skips:

  • Song 11 plays. Skip this song.
  • Song 44 plays. Skip this song.
  • Song 22 plays. Skip this song.
  • Song 33 plays. Listen to this song in full.
  • Song 11 plays. Skip this song.
  • Song 44 plays. Skip this song.
  • Song 22 plays. Listen to this song in full.
  • Song 33 plays. Skip this song.
  • Song 11 plays. Listen to this song in full.
  • Song 44 plays. Listen to this song in full.

On the second day, the permutations are (a1,…,a4)=(1,4,3,2)(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 22.

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

样例输入/输出 #1 的解释

第一天,(a1,…,a4)=(1,4,2,3)(a_1, \ldots, a_4) = (1, 4, 2, 3),(b1,…,b4)=(3,2,1,4)(b_1, \ldots, b_4) = (3, 2, 1, 4)。你可以在期望的顺序下完整收听这些歌曲,共需 66 次跳过:

  • 歌曲 11 播放。跳过此歌曲。
  • 歌曲 44 播放。跳过此歌曲。
  • 歌曲 22 播放。跳过此歌曲。
  • 歌曲 33 播放。完整收听此歌曲。
  • 歌曲 11 播放。跳过此歌曲。
  • 歌曲 44 播放。跳过此歌曲。
  • 歌曲 22 播放。完整收听此歌曲。
  • 歌曲 33 播放。跳过此歌曲。
  • 歌曲 11 播放。完整收听此歌曲。
  • 歌曲 44 播放。完整收听此歌曲。

第二天,排列为 (a1,…,a4)=(1,4,3,2)(a_1, \ldots, a_4) = (1, 4, \mathbf{3}, \mathbf{2}),(b1,…,b4)=(3,2,1,4)(b_1, \ldots, b_4) = (3, 2, 1, 4)。最少跳过次数为 22。

第三天,排列为 (a1,…,a4)=(1,4,3,2)(a_1, \ldots, a_4) = (1, 4, 3, 2),(b1,…,b4)=(1,2,3,4)(b_1, \ldots, b_4) = (\mathbf{1}, 2, \mathbf{3}, 4)。最少跳过次数为 66。

输入解题思路,AI测评打分。不知道怎么写?

首页