Squared permutation
Recently, the Bored river boat classmate is playing a strange card game called "Little Q prank".
Now the cross-river boat classmate Hand has a card, respectively written, scrambled order after line, position from left to right according to the label.
The next small Q alumni to give an operation, divided into the following two kinds:
1. Given, the exchange of the first and third cards from left to right,
2. Given, for the number of cards from left to right, note the number of the card where the position is the number on the card, and ask for the result of all the numbers noted together.
Although the bored river boat students proficient in arithmetic, but to complete such a large amount of calculation is too hard, I hope you can help him handle these operations.
Input
The first line is a positive integer that represents the number of groups of test data,
For each set of test data,
The first line is an integer,
The second line contains an arrangement, where the number represents the number on the first card,
The third line is an integer that represents the operand,
Next line, each row contains three integers, which represents the type of operation.
Output
For each set of test data, output the results of all query operations sequentially, one row for each result.
Sample Input
131 2 332 1 21 1 32 2 3
Sample Output
35
Hint
For examples,
The number on the card after the second operation is 3,2,1 from left to right,
The result of the third operation is that the position is the number of cards on the 2nd card plus the number of the number on the 3rd card, that is, the number on the 2nd card plus the number on the 1th card, the result is 5.
Source
Exercises
Distinct line-segment tree
However, it is important to note that in the exchange of time, may affect the location of 4, more updates will be good
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>#include<queue>using namespacestd;Const intN = 4e6+Ten, M =30005, mod = 1e9 +7, INF = 1e9+ +; typedefLong Longll;intN,vis[n],a[n];intTr[n],l[n],r[n],las[n];ll Sum[n];voidBuildintKintXinty) {l[k]=x; R[K]=y; if(x==y) {sum[k]=A[A[X]];return ; } intMid = (x+y) >>1; Build (k<<1, X,mid); Build (k<<1|1, mid+1, y); SUM[K]= sum[k<<1]+sum[k<<1|1];}voidUpdateintKintXintc) {if(x==l[k]&&x==R[k]) {Sum[k]=C; return ; } intMID = (L[k]+r[k])/2; if(x<=mid) Update (k<<1, X,c); ElseUpdate (k<<1|1, X,c); SUM[K]= sum[k<<1]+sum[k<<1|1];} ll ask (intKintSintt) {if(L[k]==s&&t==r[k])returnSum[k]; intMID = (L[k]+r[k]) >>1 ; LL ret=0; if(t<=mid) ret = Ask (k<<1, s,t); Else if(s>mid) ret = Ask (k<<1|1, s,t); Elseret = Ask (k<<1, S,mid) +ask (k<<1|1, mid+1, T); returnret;}intMain () {intT; scanf ("%d",&T); while(t--) {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Las[a[i]]=i; } Build (1,1, N); intQ; scanf ("%d",&q); while(q--) { intOp,l,r; scanf ("%d%d%d",&op,&l,&R); if(OP = =1) { intT1 =A[r]; intt2 =A[l]; Swap (a[l],a[r]); Update (1, L,a[t1]); Update (1, R,a[t2]); LAS[A[L]]=l; LAS[A[R]]=R; Update (1, Las[l],a[l]); Update (1, Las[r],a[r]); } Else{printf ("%lld\n", Ask (1, L,r)); } } }}
BNU 51636 squared permutation segment tree