POJ-3928 Ping Pong (tree-like array)

Source: Internet
Author: User

Ping Pong
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2985 Accepted: 1104

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

I'm writing this. Overloaded operators

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <cstdlib>5#include <queue>6#include <stack>7#include <vector>8#include <iostream>9#include"algorithm"Ten using namespacestd; OnetypedefLong LongLL; A Const intmax=100005; - intT; - intN; the intC[max]; - structpeo{ -     intname; -     intSCO; +     BOOL operator< (ConstPeo &zt)Const { -         if(sco!=Zt.sco) +          returnsco<Zt.sco; A         returnname<Zt.name; at     } - }stu[max]; - voidAddintXinty) { -      for(; x<=n;c[x]+=y,x+= (x&-x)); - } -LL Search (intx) { inLL an (0); -      for(;x>0; an+=c[x],x-= (x&-x)); to     returnan ; + } - intMain () { theFreopen ("tennis.in","R", stdin); *Freopen ("Tennis.out","W", stdout); $     inti,j;Panax Notoginsengscanf"%d",&T); -      while(t--){ thescanf"%d",&n); +      for(i=1; i<=n;i++) A{scanf ("%d",&Stu[i].sco); theStu[i].name=i; +     } -Sort (stu+1, stu+n+1); $Memset (c,0,sizeof(c)); $LL X1,x2,x3,x4,ans (0); -Add (stu[1].name,1); -      for(i=2; i<=n;i++) the{X1=search (stu[i].name-1); -x2=stu[i].name-1-X1;WuyiX3=search (N)-search (stu[i].name); thex4=n-stu[i].name-X3; -ans=ans+x1*x4+x2*X3; WuAdd (Stu[i].name,1); -     } Aboutprintf"%lld\n", ans); $     } -     return 0; -}

POJ-3928 Ping Pong (tree-like array)

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.