LA 4329(树状数组)

题目描述:

N <tex2html_verbatim_mark>(3N20000) <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>(1T20) <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 ( 1ai100000 <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 }
				
时间: 2024-11-03 05:44:29

LA 4329(树状数组)的相关文章

LA 4329 (树状数组) Ping pong

第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到1000,这样与运算以后不变 最右边的1左边部分取反,加一不会影响左半部分,所以与运算以后全部为0 对于这道题来说貌似不是很容易能联想到树状数组 注意题中说了每个人的技能值互不相同. 从左往右扫描每个a[i],另x[a[i]] = 1,然后统计x[1]...x[a[i]-1]的和就是第i个人左边技能值比

poj Ping pong LA 4329 (树状数组统计数目)

Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2302   Accepted: 879 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 im

LA 2191 树状数组 稍修改

题意:给出n个数字,操作有修改(S)和输出区间和(M). #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <stack> #include <map> #include <

树状数组 LA 4329 亚洲赛北京赛区题

复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=501&page=show_problem&problem=4174 学到几点: 1.树状数组C[i]的构建,一则c[i]=s[i]-s[i-lowbit(i)];这是一直用的做法.如今学到一种新的,直接add(i,a[i]),(s[i]为a[1]到a[i]的和) 2.前缀和思想,

LA 4329(树状数组)

算法竞赛入门经典 p197 题目大意: 一条大街上住着n个乒乓球爱好者.常常比赛切磋技术.每一个人都有一个不同的技能值a[i].每场比赛须要3个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两名选手之间.问一共能组织多少种比赛. 分析:    如果a[1]到a[i-1]中小于a[i]的数有p[i].a[i+1]到a[n]中小于a[i]的数有s[i]个; 这样当i为裁判时可以组织的比赛数目为:p[i]*(n-i-s[i]) + (i-1-p[i])*s

UVAlive - 4329 —— Ping pong 【树状数组】

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; const int MAXA = 100000 + 5; int n; int c[MAXA]; int sum(int i) { int

UVALive 4329 Ping pong (树状数组)

白书上的例题.花了很多时间在找bug上,刚学树状数组,这道题add操作时要注意上限不是n. #include <bits/stdc++.h> using namespace std; #define ll long long const ll maxn = 1e5 + 10; ll C[maxn]; ll n; inline ll lowbit(ll x) { return x & (-x); } ll sum(ll x) { ll ret = 0; while(x > 0) {

树状数组 + 位运算 LA 4013 A Sequence of Numbers

题目传送门 题意:n个数,两种操作,一是每个数字加x,二是查询& (1 << T) == 1 的个数 分析:因为累加是永远的,所以可以离线处理.树状数组点是c[16][M] 表示数字x%(1 << j) 后的数字pos,考虑第j位的个数.当询问时根据add不同的值不同的处理情况. #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 5; c

UVALive 4329 Ping pong(树状数组)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在每场比赛需要3个人,两个选手,一个裁判:裁判须住在他们之间,且其技能值必须在两选手之间,求一共能组织多少种比赛. 注意到技能值的范围(1 ≤ ai ≤ 100000),所以我们可以树状数组(O(nlogn))处理得到一个ll数组,ll[x]表示住在x左边的并且技能值小于x的技能值的人数.类似逆着处理