Tree-like array---squared permutation

Source: Internet
Author: User

Bnuoj 51636

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 14th Beijing Normal University Program design competition final idea: the use of a tree array can easily find a continuous sequence of the and;
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespacestd;Long Longs[100100];inta[100100],b[100100],flag[100100],t,n,m,time;intf[Ten],tot;intLowbit (intx) {    returnX & (-x);}voidUpdateintXLong Longy) {     while(x <=N) {s[x]+=y; X+=lowbit (x); }}Long LongGetsum (intx) {    Long LongTMP =0;  while(X >0) {tmp+=S[x]; X-=lowbit (x); }    returntmp;}intMain () {scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(inti =1; I <= n;i++) {scanf ("%d",&A[i]); B[a[i]]=i; S[i]=0; }         for(inti =1; I <= n;i++) Update (i,a[a[i]]); scanf ("%d",&m);  for(inti =1; I <= m;i++)        {            intOp,l,r; scanf ("%d%d%d",&op,&l,&R); if(OP = =1) { time++; Tot=2; f[1] =l; f[2] =R; FLAG[L]=Time ; FLAG[R]=Time ; if(Flag[b[l]]! =Time ) {                    ++tot; F[tot]=B[l]; FLAG[B[L]]=Time ; }                if(Flag[b[r]]! =Time ) {                    ++tot; F[tot]=B[r]; FLAG[B[R]]=Time ; }                 for(inti =1; I <= tot;i++) Update (F[i],-A[a[f[i]]);                Swap (a[l],a[r]); B[A[L]]=l; B[A[R]]=R;  for(inti =1; I <= tot;i++) Update (f[i],a[a[f[i]]); }            Elseprintf"%lld\n", Getsum (R)-getsum (l1)); }    }    return 0;}

Tree-like array---squared permutation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.