Fruit Ninja
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1831 Accepted Submission (s): 719
Problem Description Recently, Dobby is addicted in the Fruit Ninja. As you know, Dobby are a free elf, so unlike other elves, he could do whatever he wants. But the hands of the Elves is somehow strange, so if he cuts the fruit, he can only do specific move of his hands. Moreover, he can only start his hand in point A, and then move to point B,then move to point C,and he must make sure that Point A was the lowest, point B was the highest, and point C was in the middle. Another elf, Kreacher, is isn't interested in cutting fruits, but he's very interested in numbers. Now, he wonders, give you a permutation of 1 to N, what many triples that makes such a relationship can you find? That's, how many (x, y, Z) can-find such that x < Z < y?
Input The first line contains a positive integer t (t <=), indicates the number of test cases. For each test case, the first line of input was a positive integer n (n <= 100,000), and the second line is a permutation of 1 to N.
Output for each test case, ouput the number of triples as the sample below, you just need to output the result mod 1000000 07.
Sample Input261 3 2 6 5 45 3 5 2 4 1
Sample outputcase #1:10Case #2:1 give a sequence, n number, (1~n) Ask how many of the 3-tuple compliant (AX, Ay, AZ) && (Ax < Az < Ay), ( x < y < Z) when we insert a new number Y, for each of the previous x less than Y. There are y-x-1 numbers in this open range (x, y). We want to know how many numbers are in (x, y) and the right side of Y. The practice is to set up 2 tree-like arrays, a number of maintenance numbers, and a maintenance number. When we insert a number Y, the time. The first tree array is used to find the number of CNT to the left of Y and less than Y. The second tree-shaped array sums the sum to the left of Y and the number less than Y. Then (y-1) * Cnt-sum is the sum of the numbers in (x, y), (0 <= x < y) intervals and then subtracts those on the left of Y and the number of numbers in the interval (x, y) cnt* (cnt-1)/2, joined The results can be.
#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<vector>#include<map>#include<vector>#include<queue>using namespacestd; typedefLong LongLL; typedef pair<int,int>PII;#defineX First#defineY SecondConst intN =100010;Const intMoD =100000007 ;intN, X[n]; LL c[n][2];intLowbit (intx) {returnx&-x;}voidInit () {memset (c,0,sizeofc); }voidUpdateintPOS, LL Key,intb) {if(pos = =0)return ; while(Pos < n+Ten) {C[pos][b]= (C[pos][b] + key)%MoD; POS+=Lowbit (POS); }}ll Query (intPosintb) {LL ans=0 ; while(Pos >0) {ans= (ans + c[pos][b])%MoD; POS-=Lowbit (POS); } returnans;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); #endif //LOCAL int_, cas =1 ; scanf ("%d",&_); while( _--) {printf ("Case #%d:", cas++); Init (); scanf ("%d",&N); for(inti =1; I <= N; ++i) {scanf ("%d",&X[i]); } LL ans=0 ; for(inti =1; I <= N; ++i) {update (x[i],1,0); Update (X[i], x[i],1 ); LL CNT= Query (X[i]-1,0), sum = Query (x[i]-1,1 ) ; LL tmp= CNT * (CNT-1) /2 ; Ans= (ans + 1ll* (x[i)-1) * cnt-sum-tmp)%MoD;//cout << cnt << ' << sum << ' << tmp << ' << ans << E NDL;} printf ("%lld\n", ans); }}
View Code
HDU 4000 Fruit Ninja (tree-like array)