Codeforces Round 672 (Div. 2)
C2
Pokémon Army (hard version)
Codeforces
2100
原题
← C1D →
翻译中…

This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved.

翻译中…

Pikachu is a cute and friendly pokémon living in the wild pikachu herd.

翻译中…

But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist.

翻译中…

First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct.

翻译中…

As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 \lt b_2 \lt \dots \lt b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$.

翻译中…

The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$.

翻译中…

Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon.

翻译中…

Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation.

翻译中…

Help Andrew and the pokémon, or team R will realize their tricky plan!

翻译中…

Input

翻译中…

Each test contains multiple test cases.

翻译中…

The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively.

翻译中…

The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon.

翻译中…

$i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap.

翻译中…

Note

翻译中…

Let's look at the third test case:

翻译中…

Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$.

翻译中…

After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$.

翻译中…

After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$.

翻译中…

After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$.

翻译中…

After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$.

翻译中…

After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.

翻译中…

样例

Input #1
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
Output #1
3
4
2
2
2
9
10
10
10
9
11
InputCopy OutputCopy
题目信息
题号
1420C2
时间限制
2 s
内存限制
256 MB
标签
数据结构
分治
动态规划
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。