BNU 51636 squared permutation (tree-like Array) (North Division 16 school race)

Source: Internet
Author: User

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 session of Beijing Normal University program design competition final Authorquailty

Main topic:

There is an array of 1 to n arrays of AI, with M queries, asking for two operations, 1. Exchange a[l],a[r],2. A[l] ... To A[r] These numbers are the and of the number of a in the subscript.

Problem Solving Ideas:

The position of the tree array I is maintained with A[i] as the number in subscript a (ie, a[a[i]), so that every 2 query sum is a continuous interval, then 1 when asked to update the tree array:

The first step is to treat the number in a as a pointer to the number in the array, update the value of the number in the tree array to the L,R (if the LR itself points to L or R then you don't need to change it to avoid repeating it)

The second step: Update the A[L],A[R] after swapping L and R in the tree array for the new A[a[l]],a[a[r]].

2 when asked for the direct output of sum[l]-sum[i-1] can, because before the definition of the meaning of the tree array has been guaranteed this point


#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include < string> #include <vector> #include <stack> #include <queue> #include <set> #include <map > #define C (a) memset (a,0,sizeof a) #define C_1 (a) memset (a,-1,sizeof a) #define C_I (a) memset (a,0x3f,sizeof a) typedef Long long ll;using namespace Std;const int maxn = 1e6 + 20;ll A[MAXN], N, bit[maxn];void Add (int x, int y) {while (x <= N) {a[x] + = y;x + = X&-x;}} ll sum (int x) {ll s = 0;while (x > 0) {s + = a[x];x-= x&-x;} return s;} void Init () {C (a); C (bit);} int Num[maxn];int Ma[maxn];int Main () {int T; CIN >> T;while (t--) {scanf ("%lld", &n); init (); for (int i = 1; I &lt ; = N; i++) {scanf ("%d", &num[i]); Ma[num[i]] = i;} for (int i = 1; I <= n; i++) {Bit[i] = Num[num[i]];add (i, num[num[i]]);} int q;scanf ("%d", &q); int type, R, L;while (q--) {scanf ("%d%d%d", &type, &l, &r); if (type = = 1) {if (Ma[l]!) = L&&ma[l]! = r) {Add (Ma[l],-num[l]); Add (Ma[l], num[r]);} if (ma[r]! = r&&ma[r]! = L) {Add (Ma[r],-num[r]); Add (Ma[r], num[l]);} Add (R,-num[num[r]]), add (L,-num[num[l]]), swap (Num[l], num[r]), add (L, Num[num[l]]), add (R, Num[num[r]]); Ma[num[r]] = r ; Ma[num[l]] = l;} else{printf ("%lld\n", SUM (r)-sum (L-1));}}} return 0;}


BNU 51636 squared permutation (tree-like Array) (North Division 16 school race)

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.