A15720 | Serval and Toxel's Arrays
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Toxel likes arrays. Before traveling to the Paldea region, Serval gave him an array $a$ as a gift. This array has $n$ pairwise distinct elements.
In order to get more arrays, Toxel performed $m$ operations with the initial array. In the $i$ -th operation, he modified the $p_{i}$ -th element of the $(i-1)$ -th array to $v_{i}$ , resulting in the $i$ -th array (the initial array $a$ is numbered as $0$ ). During modifications, Toxel guaranteed that the elements of each array are still pairwise distinct after each operation.
Finally, Toxel got $m+1$ arrays and denoted them as $A_{0}=a, A_{1},\ldots,A_{m}$ . For each pair $(i,j)$ ( $0\le i<j\le m$ ), Toxel defines its value as the number of distinct elements of the concatenation of $A_{i}$ and $A_{j}$ . Now Toxel wonders, what is the sum of the values of all pairs? Please help him to calculate the answer.
In order to get more arrays, Toxel performed $m$ operations with the initial array. In the $i$ -th operation, he modified the $p_{i}$ -th element of the $(i-1)$ -th array to $v_{i}$ , resulting in the $i$ -th array (the initial array $a$ is numbered as $0$ ). During modifications, Toxel guaranteed that the elements of each array are still pairwise distinct after each operation.
Finally, Toxel got $m+1$ arrays and denoted them as $A_{0}=a, A_{1},\ldots,A_{m}$ . For each pair $(i,j)$ ( $0\le i<j\le m$ ), Toxel defines its value as the number of distinct elements of the concatenation of $A_{i}$ and $A_{j}$ . Now Toxel wonders, what is the sum of the values of all pairs? Please help him to calculate the answer.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1\le t\le10^{4}$ ). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ( $1\le n,m\le2\cdot10^{5}$ ) — the length of the array and the number of operations.
The second line of each test case contains $n$ integers $a_{1},a_{2},\dots,a_{n}$ ( $1\le a_{i}\le n+m$ ). It is guaranteed that all $a_i$ are pairwise distinct.
Each of the next $m$ lines of each test case contains two integers $p_{i}$ and $v_{i}$ ( $1\le p_{i}\le n$ , $1\le v_{i}\le n+m$ ) — the position of the modified element and its new value. It is guaranteed that the elements of each array are still pairwise distinct after each modification.
It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2\cdot10^{5}$ .
The first line of each test case contains two integers $n$ and $m$ ( $1\le n,m\le2\cdot10^{5}$ ) — the length of the array and the number of operations.
The second line of each test case contains $n$ integers $a_{1},a_{2},\dots,a_{n}$ ( $1\le a_{i}\le n+m$ ). It is guaranteed that all $a_i$ are pairwise distinct.
Each of the next $m$ lines of each test case contains two integers $p_{i}$ and $v_{i}$ ( $1\le p_{i}\le n$ , $1\le v_{i}\le n+m$ ) — the position of the modified element and its new value. It is guaranteed that the elements of each array are still pairwise distinct after each modification.
It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2\cdot10^{5}$ .
输出格式
For each test case, print a single integer — the sum of the values of all pairs of arrays.
输入输出样例
输入 #1
3 3 2 1 2 3 1 4 2 5 1 1 1 1 1 10 10 4 6 9 12 16 20 2 10 19 7 1 3 5 4 2 17 2 18 6 11 7 1 8 17 5 5 5 5 2 2
输出 #1
13 1 705
In the first test case, the arrays change as follows: $[1,2,3]\to[\underline{4},2,3]\to[4,\underline{5},3]$ .
The concatenation of the $0$ -th array and the $1$ -st array is $[1,2,3,4,\sout{2},\sout{3}]$ . There are $4$ distinct elements.
The concatenation of the $0$ -th array and the $2$ -nd array is $[1,2,3,4,5,\sout{3}]$ . There are $5$ distinct elements.
The concatenation of the $1$ -st array and the $2$ -nd array is $[4,2,3,\sout{4},5,\sout{3}]$ . There are $4$ distinct elements.
Strikethrough elements are duplicates in the array.
Therefore, the answer is $4+5+4=13$ .
In the second test case, note that the array may remain unchanged after operations.
The concatenation of the $0$ -th array and the $1$ -st array is $[1,2,3,4,\sout{2},\sout{3}]$ . There are $4$ distinct elements.
The concatenation of the $0$ -th array and the $2$ -nd array is $[1,2,3,4,5,\sout{3}]$ . There are $5$ distinct elements.
The concatenation of the $1$ -st array and the $2$ -nd array is $[4,2,3,\sout{4},5,\sout{3}]$ . There are $4$ distinct elements.
Strikethrough elements are duplicates in the array.
Therefore, the answer is $4+5+4=13$ .
In the second test case, note that the array may remain unchanged after operations.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted