Pokémon Army (hard version) SOLUTION
This is the hard form of the issue. The contrast between the adaptations is that the simple form has no trade tasks. You can make hacks just if all adaptations of the issue are explained.
Pikachu is an adorable and benevolent pokémon living in the wild pikachu crowd.
However, it has become known as of late that notorious group R needed to take all these pokémon! Pokémon mentor Andrew chose to help Pikachu to manufacture a pokémon armed force to stand up to.
To begin with, Andrew tallied all the pokémon — there were actually n pikachu. The quality of the I-th pokémon is equivalent to ai, and every one of these numbers are particular.
As a military, Andrew can pick any non-void aftereffect of pokemons. As it were, Andrew picks some exhibit b from k lists to such an extent that 1≤b1<b2<⋯<bk≤n, and his military will comprise of pokémons with powers ab1,ab2,… ,abk.
The quality of the military is equivalent to the substituting whole of components of the aftereffect; that is, ab1−ab2+ab3−ab4+… .
Andrew is trying different things with pokémon request. He performs q activities. In I-th activity Andrew trades li-th and ri-th pokémon.
Andrew needs to know the maximal stregth of the military he can accomplish with the underlying pokémon position. He additionally has to know the maximal quality after every activity.
Help Andrew and the pokémon, or group R will understand their dubious arrangement!
Each test contains various experiments.
The primary line contains one sure number t (1≤t≤103) indicating the quantity of experiments. Depiction of the experiments follows.
The principal line of each experiment contains two whole numbers n and q (1≤n≤3⋅105,0≤q≤3⋅105) indicating the quantity of pokémon and number of tasks individually.
The subsequent line contains n particular positive numbers a1,a2,… ,a (1≤ai≤n) indicating the qualities of the pokémon.
I-th of the last q lines contains two positive numbers li and ri (1≤li≤ri≤n) meaning the lists of pokémon that were traded in the I-th activity.
It is ensured that the entirety of n over all experiments doesn’t surpass 3⋅105, and the aggregate of q over all experiments doesn’t surpass 3⋅105.
For each experiment, print q+1 whole numbers: the maximal quality of armed force before the trades and after each trade.
1 3 2
1 2 5 4 3 6 7
We should take a gander at the third experiment:
At first we can manufacture a military in such manner: [1 2 5 4 3 6 7], its quality will be 5−3+7=9.
After first activity we can fabricate a military in such manner: [2 1 5 4 3 6 7], its quality will be 2−1+5−3+7=10.
After second activity we can manufacture a military in such manner: [2 1 5 4 3 7 6], its quality will be 2−1+5−3+7=10.
After third activity we can construct a military in such manner: [2 1 4 5 3 7 6], its quality will be 2−1+5−3+7=10.
After forward activity we can assemble a military in such manner: [1 2 4 5 3 7 6], its quality will be 5−3+7=9.
After all activities we can fabricate a military in such manner: [1 4 2 5 3 7 6], its quality will be 4−2+5−3+7=11.