CF2147F.Exchange Queries

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are at a marketplace and there are two traders who are willing to trade on nn different items. Each trader is represented by a permutation that signifies the relative value of the nn items for that trader. Let's denote those two permutations pp and ss. If you have an item ii, you can trade it for an item jj if either pi>pjp_i \gt p_j (using the first trader) or si>sjs_i \gt s_j (using the second trader).

We say that an item ii is at least as valuable as an item jj if you can come to the marketplace with item ii, make some (possibly none) trades, and get item jj. Note that at any moment, you will have exactly one item on hand. Assume that the traders have infinite supplies of any item.

Due to the never-ending updates in the market, traders will often reevaluate their views on the item values. You will be given qq queries; each is a swap in one of the permutations. After every update, you should print the number of pairs (i,j)(i, j) (1≤i,j≤n1 \le i, j \le n) such that item ii is at least as valuable as item jj.

你身处一个市场,这里有两位交易者愿意就 nn 种不同物品进行交易。每位交易者由一个排列表示,该排列刻画了该交易者对这 nn 种物品的相对价值评估。我们记这两个排列为 pp 和 ss。若你持有物品 ii,则当且仅当 pi>pjp_i > p_j(使用第一位交易者)或 si>sjs_i > s_j(使用第二位交易者)时,你可以将物品 ii 交易为物品 jj。

我们称物品 ii 至少与物品 jj 一样有价值,当且仅当你带着物品 ii 进入市场,经过若干次(可能为零次)交易后,最终获得物品 jj。注意:在任意时刻,你手中恰好持有一件物品。假设两位交易者对任意物品均有无限供应。

由于市场持续更新,交易者会频繁重新评估他们对物品价值的看法。你将收到 qq 个查询;每个查询表示对其中一个排列执行一次交换操作。每次更新后,你需要输出满足“物品 ii 至少与物品 jj 一样有价值”的有序对 (i,j)(i, j)(其中 1≤i,j≤n1 \le i, j \le n)的总数。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (2≤n≤1052 \le n \le 10^5, 1≤q≤1051 \le q \le 10^5).

The next two lines contain the initial permutations pp and ss.

The following qq lines each contain three integers tptp, ii, jj (tp∈1,2tp \in { 1, 2 }, 1≤i,j≤n1 \le i, j \le n, i≠ji \ne j). If tp=1tp=1, swap pip_i with pjp_j. If tp=2tp=2, swap sis_i with sjs_j.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5, and the sum of qq over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 qq(2≤n≤1052 \le n \le 10^5,1≤q≤1051 \le q \le 10^5)。

接下来两行分别给出初始排列 pp 和 ss。

随后的 qq 行中,每行包含三个整数 tptp、ii、jj(tp∈{1,2}tp \in \{ 1, 2 \},1≤i,j≤n1 \le i, j \le n,i≠ji \ne j)。若 tp=1tp = 1,则交换 pip_i 与 pjp_j;若 tp=2tp = 2,则交换 sis_i 与 sjs_j。

保证所有测试用例的 nn 之和不超过 10510^5,且所有测试用例的 qq 之和不超过 10510^5。

输出格式

For each test case, output qq lines, the kk-th of them containing a single integer — the number of pairs (i,j)(i, j) such that item ii is at least as valuable as item jj after the first kk updates.

对于每个测试用例,输出 qq 行,其中第 kk 行包含一个整数——表示在前 kk 次更新之后,满足物品 ii 的价值至少与物品 jj 相当的二元组 (i,j)(i, j) 的数量。

输入输出样例

  • 输入#1

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

    输出#1

    6
    9
    9
    12
    11

说明/提示

In the first test case, after the first update, both permutations are the identity permutations and thus the only valid pairs are (1,1)(1, 1), (2,1)(2, 1), (2,2)(2, 2), (3,1)(3, 1), (3,2)(3, 2), and (3,3)(3, 3). After the second update, it is possible to get any item starting with any item. For instance, you can get item 33 from item 11 using the second trader since s1=3>1=s3s_1 = 3 \gt 1 = s_3. After the third update, it is still possible to get any item starting with any item. For instance, if you start with item 22 and want to get item 11, you can first exchange your item 22 for item 33 using the second trader, and then exchange item 33 for item 11 using the first trader.

在第一个测试用例中,第一次更新后,两个排列均为单位排列,因此唯一有效的数对为 (1,1)(1, 1)、(2,1)(2, 1)、(2,2)(2, 2)、(3,1)(3, 1)、(3,2)(3, 2) 和 (3,3)(3, 3)。第二次更新后,可以从任意物品出发获得任意其他物品。例如,你可以利用第二位交易者从物品 11 获得物品 33,因为 s1=3>1=s3s_1 = 3 \gt 1 = s_3。第三次更新后,仍然可以从任意物品出发获得任意其他物品。例如,若你起始持有物品 22 并希望获得物品 11,可先利用第二位交易者将物品 22 换成物品 33,再利用第一位交易者将物品 33 换成物品 11。

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

首页