HDU 2492 Ping pong (树状数组)

Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1567    Accepted Submission(s): 554


Problem 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
 
 
对于每一个裁判,要当裁判的个数等于  1~左边比他小的个数*右边比他大的个数 + 2~左边比他大的个数*右边比他小的个数
 
 
才发现树状数组能求一个数的左右边比他小或大的个数。。。。。
 
之前都是数组a[i]里的i作为区间的每个点,而这题是整个a[i]的值作为区间的每个点,c[i]表示从i<=的一段连续的数的总个数
 
View Code
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>

using namespace std;
//Constant Declaration
/*
--------------------------*/
//#define LL long long
#define LL __int64
const int M=100001;
const int INF=1<<30;
const double EPS = 1e-11;
const double PI = acos(-1.0);
/*--------------------------*/
// some essential funtion
/*
----------------------------------*/
void Swap(int &a,int &b){ int t=a;a=b;b=t; }
int Max(int a,int b){ return a>b?a:b; }
int Min(int a,int b){ return a<b?a:b; }
int Gcd(int a,int b){ while(b){b ^= a ^=b ^= a %= b;} return a; }
/*----------------------------------*/
//for (i = 0; i < n; i++)
/*
----------------------------------*/

LL c[M];
int a[M];
int left_lower[M], right_lower[M];//分别是,在第i个数左右边,比第i个数小的个数
int n = 100000;


int LowBit(int x)
{
return x&(-x);
}

int Sum(int k)
{
int sum = 0;
while (k > 0)
{
sum += c[k];
k -= LowBit(k);
}
return sum;
}

void Update(int k, int sc)
{
while (k <= 100000)
{
c[k] += sc;
k += LowBit(k);
}
}



int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t, case1 = 0;
scanf("%d", &t);
int m;
int i, j;
int num;
//scanf("%d%d", &n, &m);
while (t--)
{
scanf("%d", &num);
memset(c, 0, sizeof(c));
memset(left_lower, 0, sizeof(left_lower));
memset(right_lower, 0, sizeof(right_lower));
for (i = 1; i <= num; i++)
{
scanf("%d", &a[i]);
left_lower[i] += Sum(a[i] - 1);///Sum(a[i] - 1):插入该数前,区间1到a[i]-1的总个数
Update(a[i], 1);
}

memset(c, 0, sizeof(c));
for (i = num; i > 0; i--)//顺序插入
{
right_lower[i] += Sum(a[i] - 1);
Update(a[i], 1);
}

LL ans = 0;

for (i = 1; i <= num; i++)//逆序插入
{
ans += left_lower[i]*(num - i - right_lower[i]);//由于只能求比其小的个数,可以用i右边的总个数见减比他小的来求比他大的个数。
ans += (i - 1 - left_lower[i])*right_lower[i];//
}

printf("%I64d\n", ans);
}


return 0;
}
原文地址:https://www.cnblogs.com/qiufeihai/p/2391988.html