# POJ 3928 Ping Pong (tree-like array)

Source: Internet
Author: User

Ping Pong
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1352 Accepted: 509

Description

N (3<=n<=20000) Ping pong players live along a West-east street (consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If-Players want to compete, they must choose a referee among other ping pong players and hold the game in the referee ' S house. For some reason, the contestants can ' t choose a referee whose skill rank is higher or lower than both of theirs. The contestants has to walk to the referee's house, and because they is lazy, they want to make their total walking dist Ance no more than the distance between their houses. Of course all players live in different houses and the position of their houses is all different. If the referee or any of the contestants are different, we call both games different. Now are the problem:how many different games can being held in this ping Pong street?

Input

The first line of the input contains a integer T (1<=t<=20), indicating the number of test cases, followed by T line s each of the which describes a test case.
Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, A2 ... an follow, indicating the skill rank of all player, in the order of West to east. (1 <= AI <= 100000, i = 1 ... N).

Output

For each test case, output a single line contains an integer with the total number of different games.

Sample Input

`1 3 1) 2 3`

Sample Output

`1<span style= "FONT-SIZE:18PX;" ><strong> first ranked according to the rank value, then the research problem is concentrated in the study of subscript, according to the topic requirements can be enumerated after the sorting of each point to find a position smaller than it, and bigger than him. Then multiply the results on both sides (multiplication principle). The result is that this point is used as a referee. The number of matches that can be held. SUM (i)-1 is less than the number of newly inserted numbers on the left, I-sum (i)-1 is greater than the number of newly inserted elements, per "I". id-1-(sum (i)-1) The subscript of the right element for this point is less than the number of this point.N-(I-sum (i)-1)-per "I". ID is the right element the subscript is greater than the number of subscripts below this point. The process guarantees that the rank value must be on both sides of each element enumeration. </strong></span><pre name= "code" class= "CPP" > #include <iostream> #include <sstream># include<algorithm> #include <cstdio> #include <string.h> #include <cctype> #include <string > #include <cmath> #include <vector> #include <stack> #include <queue> #include <map># include<set>using namespace Std;const int inf=200003;int cnt[inf];struct a{int v,id;} per[inf];bool cmp1 (A A,A b ) {return A.V&LT;B.V;} BOOL Cmp2 (A A, a B) {return A.V&GT;B.V;} int lowbit (int x) {return x& (-X);}        void Add (int x,int val) {while (x<inf) {cnt[x]+=val;    X+=lowbit (x);    }}int sum (int x) {int s=0;        while (x>0) {s+=cnt[x];    X-=lowbit (x); } return s;}    int main () {int t,n;    cin>>t;        while (t--) {cin>>n;        memset (cnt,0,sizeof (CNT));        Long Long ans=0; for (int i=1; i<=n; i++) {scanf ("%d", &amP;PER[I].V);        Per[i].id=i;        } sort (PER+1,PER+N+1,CMP1);            for (int i=1; i<=n; i++) {Add (per[i].id,1);            Long Long Lmin=sum (per[i].id)-1;            Long Long lmax=i-lmin-1;        ans+= (n-lmax-per[i].id) *lmin+lmax* (per[i].id-lmin-1);    } cout<<ans<<endl; } return 0;}`

POJ 3928 Ping Pong (tree-like array)

Related Keywords:

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.