luogu#P4847. 银河英雄传说V2
银河英雄传说V2
Background
Yesterday, Little H saw the problem luogu P1196, which triggered his feelings about heroes (fog). However, he could not solve it, so he went to ask Little W for help. After explaining that problem, Little W had a sudden idea—why not modify this problem a bit?
So, a very easy “check-in” problem appeared.
Problem Description
Some dalao said: Make the statement simpler so I can get an AC in one minute!
So Little W had to simplify the meaning:
Given sequences of length , the -th sequence contains one element with value . Then there are three types of operations:
M x y: move the sequence containing to after the sequence containing . If and are already in the same sequence, do nothing.D x: cut the sequence containing at position . That is, take and all elements after out as a new sequence.Q x y: query the sum of values of all elements between and (including and ). If and are not in the same sequence, output-1.
Input Format
The first line contains two integers , representing the number of elements and the number of operations. The second line contains integers, which are .
The next lines each describe one operation, in one of the following forms: M x y, D x, or Q x y, corresponding to the description.
Output Format
For each Q x y, output one integer, the sum of the values of all elements in the queried range.
5 10
57770 55352 18768 21847 79100
M 1 4
M 3 2
Q 5 2
M 3 4
M 3 5
M 4 4
M 3 1
D 1
Q 2 2
Q 2 1
-1
55352
113122
30 100
2193 75245 24438 95450 96514 84854 15292 9488 37488 940 52991 15190 64052 17398 80379 77861 88717 34751 16783 88345 27612 21748 79776 43058 35590 49064 45012 37206 70870 30643
M 18 26
M 28 27
M 25 4
M 12 22
M 26 15
M 3 1
M 20 20
M 7 21
M 18 29
M 21 26
M 29 10
M 27 23
M 30 28
M 22 10
M 13 21
M 1 23
M 25 9
M 29 27
M 23 25
M 11 12
M 1 4
M 26 14
M 26 9
D 4
M 16 8
M 16 20
M 4 27
M 9 20
M 11 1
M 19 8
Q 12 7
M 5 10
D 20
Q 29 2
Q 9 15
M 29 21
D 5
M 23 8
M 6 6
D 23
D 6
Q 4 8
D 21
Q 29 23
Q 19 4
M 21 21
M 20 25
M 27 29
D 2
Q 7 2
M 7 15
Q 11 18
D 26
Q 21 18
M 22 11
M 12 12
M 20 15
M 22 4
D 20
M 4 5
M 12 2
Q 27 20
M 30 2
M 28 9
M 20 11
M 10 21
M 12 24
Q 14 14
M 6 29
Q 13 18
Q 10 3
Q 23 3
D 4
M 27 13
M 6 23
M 7 14
Q 12 17
M 18 25
Q 2 19
D 3
D 9
Q 2 16
Q 3 8
Q 4 10
D 24
M 21 4
Q 17 15
Q 19 7
Q 1 24
Q 9 18
D 12
M 4 16
M 27 21
D 26
M 5 14
M 15 19
M 21 26
M 18 27
Q 21 8
Q 18 13
52230
-1
-1
254468
291078
112233
-1
231636
62363
-1
17398
178645
25378
219268
-1
419122
347453
-1
-1
-1
274542
-1
269126
-1
178645
Hint
(The problem setter kindly provided a larger sample!)
[Sample Explanation]
At the beginning there are sequences (each row is one sequence), arranged as:
1
2
3
4
5
The first operation puts after , becoming:
2
3
4,1
5
The second operation puts after , becoming:
2,3
4,1
5
Then query the sum between the -th element and the -nd element. Since it does not exist, output -1.
Append the sequence containing after the sequence containing , becoming:
4,1,2,3
5
Next it becomes , i.e. all elements are in sequence, so the next two merge operations are useless. Then delete the numbers after , becoming:
1,2,3
5,4
Query to , output , which is .
Query to , output , which is .
[Constraints]

To avoid some messy tricks (maybe it cannot be avoided anyway), the first points are scored in the traditional way, points per test point; the last five points are bundled as a subtask: you must pass all of them to get points, otherwise you get points.
For all testdata, , .
Translated by ChatGPT 5