Poj3928ping pong[tree-like array reverse order pairs]

Source: Internet
Author: User

Ping Pong
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3109 Accepted: 1148

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

Source

Beijing 2008 PetitionF[i] is the number of people with skill values less than I before I, G[i] is less than I after Ifind the method similar to reverse order, X[i] The skill value of I is the person, with a tree array maintenance x, Sum can
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;Const intn=2e4+5, m=1e5+5; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intT,n,m,a[n],bit[m],f[n],g[n];inlineintLowbit (intx) {returnx&-x;} InlinevoidAddintPintVintbit[]) {     for(intI=p;i<=m;i+=lowbit (i)) bit[i]+=v;} InlineintSumintPintbit[]) {    intans=0;  for(intI=p;i>0; I-=lowbit (i)) ans+=Bit[i]; returnans;}intMain () {T=read ();  while(t--) {n=read ();  for(intI=1; i<=n;i++) A[i]=read (), m=Max (m,a[i]); memset (bit,0,sizeof(bit));  for(intI=1; i<=n;i++) {Add (A[i],1, bit); F[i]=sum (a[i]-1, bit); } memset (bit,0,sizeof(bit));  for(inti=n;i>=1; i--) {Add (A[i],1, bit); G[i]=sum (a[i]-1, bit); } ll ans=0;  for(intI=2; i<=n-1; i++) ans+=f[i]* (N-i-g[i]) +g[i]* (i-1-f[i]);//, printf ("t%d%d%d\n", I,f[i],g[i]);printf"%lld\n", ans); }}

Poj3928ping pong[tree-like array reverse order pairs]

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.