Hdu 5147 Sequence II (tree number or segment tree + input plug-in prefix and + suffix and)

Source: Internet
Author: User

Test instructions

A permutation of a given 1~n is saved with an array of a[], asking how many subscripts (a,b,c,d) four tuples meet:
A

Analytical:

The range of n in the topic is the 50000,o (n^2) Complexity of the definite timeout. So the problem is obviously the LOG2 (n) algorithm, for this problem can be used in line tree or tree array, but also to use the input plug, otherwise it will time out.

Ideas (refer to others ' practices)

Enumerates the locations of C, the number of methods in each enumeration is the number of 1~c-1 (A, b) multiplied by the number of C~n (C,D). The answer is summed up.

The number of 1~c-1 (A, A, b) is the same as the position of the enumeration, and then calculates the number of seconds before B is smaller than a[b], the value to be saved, the next time you enumerate C, the value plus c-1 before the number of a[c-1] is smaller than the current situation in 1~c-1 (A, b), that is, the number of =c-1, because the situation before enumeration B has been counted.

Because when you calculate C, only the number of methods smaller than C is calculated, this only satisfies the number of the two-tuple smaller than c. The two-tuple in front of c is counted as well, so add a small number to the C in front of c.

Use a tree-like array to do it. The subject n range 50000, and each number is different is critical. So we open n positions, at first each position is 0, in fact each position is not 0 is 1, because each number only one.

For example 1 3 2 4 5
First C array 0 0 0 0 0
First count, then enter, because calculate a[i] before how much smaller than its number, not including its own, while the tree array calculation and the time to include its own.
I=1, the tree-like array with the sum prefix and the pre[1]=0,
At this point 0 0 0 0 0, enter 1, become 1 0 0 0 0
I=2,a[2]=3, to see 3 in front of how many number, that is to see the C array of 3 positions in front of how many 1, 1 means that has been entered,
Found 1 0 0 0 0 The first three digits are only one 1, which is pre[2]=1 (only 1 smaller than the second number entered), after entering 3, the C array becomes 1 0 1 0 0
I=3, a[3]= 2, to see how many numbers in front of 2, that is to see the C array before 2 positions in front of how many 1, found that 10100 the first two numbers only one 1, that is pre[3]=1

Prefix and can be calculated, then suffix and = num2[i] = n-i-a[i]+num[i]+1;

AC Code

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;typedef Long LongllConst intINF =0x3f3f3f3f;Const intN =5e4+Ten;intN;ll C[n], a[n];inline voidRead (ll &x) {intFlag =0; x =0;Charc = GetChar ();if(c = ='-') flag =1; while(C <' 0 '|| C >' 9 ') {if(c = ='-') flag =1;    c = GetChar (); } while(c >=' 0 '&& C <=' 9 ') x = x *Ten+ C-' 0 ', C = GetChar ();if(flag) x =-X;}inline intLowbit (intx) {returnX & (-X);}intQueryintx) {intRET =0; while(X >0) {ret + = c[x];    X-= Lowbit (x); }returnRET;}voidAddintXintD) { while(x <= N)        {C[x] + = D;    x + = Lowbit (x); }}intNum[n], num2[n];intMain () {intTscanf("%d", &t); while(t--) {ll ans =0, pre =0;scanf("%d", &n);memsetC0,sizeof(C)); for(inti =1; I <= N;            i++) {read (a[i]); Num[i] = query (A[i]);//calculate a[i] smaller than a[i] number beforeNum2[i] = n-i-a[i]+num[i]+1;//Calculate a[i] A number larger than A[i]Add (A[i],1);            Ans + = pre * Num2[i];        Pre + = Num[i]; }printf("%lld\n", ans); }return 0;}

Hdu 5147 Sequence II (tree number or segment tree + input plug-in prefix and + suffix and)

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.