码迷,mamicode.com
首页 > 编程语言 > 详细

LA 4329(树状数组)

时间:2015-07-17 22:17:04      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:

N <tex2html_verbatim_mark>(3技术分享N技术分享20000) <tex2html_verbatim_mark>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 <tex2html_verbatim_mark>(1技术分享T技术分享20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.

Every test case consists of N + 1 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1技术分享ai技术分享100000 <tex2html_verbatim_mark>, i = 1...N <tex2html_verbatim_mark>).

 

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


分析:对于每个a[i],算出从a[1]到a[i-1]比a[i]小的数有c[i]个,a[i+1]到a[n]比a[i]小的有d[i]个,则考虑当i为裁判时,可能的方案数为:c[i](n-i-d[i])+d[i](i-c[i]-1)。关键是算c[i]、d[i]。运用树状数组可以算出c[i],d[i]。


 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <sstream>
10 #include <queue>
11 #include <typeinfo>
12 #include <fstream>
13 #include <map>
14 #include <stack>
15 using namespace std;
16 const int maxn=20010;
17 const int maxv=100010;
18 int a[maxn],c[maxv],cc[maxn],d[maxn],n;
19 int lowbit(int x){
20     return x&-x;
21 }
22 int sum(int x){
23     int res=0;
24     while(x>0){
25         res+=c[x];
26         x-=lowbit(x);
27     }
28     return res;
29 }
30 void add(int x,int v){
31     while(x<=maxv){
32         c[x]+=v;
33         x+=lowbit(x);
34     }
35 }
36 int main()
37 {
38     int t;
39     scanf("%d",&t);
40     while(t--){
41         scanf("%d",&n);
42         memset(c,0,sizeof(c));
43         for(int i=1;i<=n;i++)
44             scanf("%d",&a[i]);
45         for(int i=1;i<=n-1;i++){
46             cc[i]=sum(a[i]-1);
47             add(a[i],1);
48         }
49         memset(c,0,sizeof(c));
50         for(int i=n;i>=2;i--){
51             d[i]=sum(a[i]-1);
52             add(a[i],1);
53         }
54         ll ans=0;
55         for(int i=1;i<=n-1;i++)
56             ans+=cc[i]*(n-i-d[i])+(i-cc[i]-1)*d[i];
57         printf("%lld\n",ans);
58     }
59     return 0;
60 }

 



LA 4329(树状数组)

标签:

原文地址:http://www.cnblogs.com/RRirring/p/4655649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!