POJ-3928 Ping pong(树状数组)

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 two 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 have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?

Input

The first line of the input contains an integer T(1<=T<=20), indicating the number of test cases, followed by T lines each of 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 each 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, the total number of different games. 

Sample Input

1 
3 1 2 3

Sample Output

1

Source

写这题顺便练了一下重载运算符

 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"
10 using namespace std;
11 typedef long long LL;
12 const int MAX=100005;
13 int T;
14 int n;
15 int c[MAX];
16 struct Peo{
17     int name;
18     int sco;
19     bool operator <(const Peo &zt) const {
20         if (sco!=zt.sco)
21          return sco<zt.sco;
22         return name<zt.name;
23     }
24 }stu[MAX];
25 void add(int x,int y){
26     for (;x<=n;c[x]+=y,x+=(x&-x));
27 }
28 LL search(int x){
29     LL an(0);
30     for (;x>0;an+=c[x],x-=(x&-x));
31     return an;
32 }
33 int main(){
34     freopen ("tennis.in","r",stdin);
35     freopen ("tennis.out","w",stdout);
36     int i,j;
37     scanf("%d",&T);
38     while (T--){
39     scanf("%d",&n);
40     for (i=1;i<=n;i++)
41     {scanf("%d",&stu[i].sco);
42      stu[i].name=i;
43     }
44     sort(stu+1,stu+n+1);
45     memset(c,0,sizeof(c));
46     LL x1,x2,x3,x4,ans(0);
47     add(stu[1].name,1);
48     for (i=2;i<=n;i++)
49     {x1=search(stu[i].name-1);
50      x2=stu[i].name-1-x1;
51      x3=search(n)-search(stu[i].name);
52      x4=n-stu[i].name-x3;
53      ans=ans+x1*x4+x2*x3;
54      add(stu[i].name,1);
55     }
56     printf("%lld\n",ans);
57     }
58     return 0;
59 }

 

posted @ 2016-08-12 12:01  lonely_OIer  阅读(225)  评论(0编辑  收藏  举报