POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】

Ping pong

                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
 

链接:hdu
2492

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

Source

2008 Asia Regional Beijing

题意

有N个人从左到右排成一排,每个人有一个唯一的技能值,选出三个人——两名比赛选手还有一名裁判,裁判的技能值不能比这两个人都高,也不能比这两个人都低,并且这两个人到裁判的距离总和不能大于他们之间的距离。不同的人比赛或者比赛时候的裁判不同算不同的比赛,求一共能比几场。

分析

将题意抽象出来,就是已知数列{aN}(3<=N<=20000)其中ai,从左往右取三个不同的数(可以不相邻),求使这三个数排成升序或降序的取法数。

设L为第i个人左边的人中,技能值小于他的人数, R为第i个人左边的人中,技能值小于他的人数,那么选第i个人作为裁判的方法数Ans[i]等于(L[i] * (N - i - R[i]) +(i - 1 - L[i]) * R[i]),最终输出的答案就是枚举N个人,对Ans[i]求和。那么现在的问题就是如何求L和R数组,那么很清晰,直接树状数组对区间求和即可,方法类似于求逆序数。

/****************************>>>>HEADFILES<<<<****************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
#define fst             first
#define snd             second
#define root            1,N,1
#define lson            l,mid,rt<<1
#define rson            mid+1,r,rt<<1|1
#define PB(a)           push_back(a)
#define MP(a,b)         make_pair(a,b)
#define CASE(T)         for(scanf("%d",&T);T--;)
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//typedef __int64         LL;
typedef long long       LL;
typedef pair<int, int>   PII;
const int INF = 0x3f3f3f3f;
const int maxn = 100000 + 5;
const int maxm = 20000 + 5;
/****************************>>>>SEPARATOR<<<<****************************/
int T, N, MAX, a[maxm];
int S[maxn], L[maxm], R[maxm];
inline int lowbit(int x) { return x & (-x); }
void Add(int pos, int val)
{
    for(; pos <= MAX; pos += lowbit(pos)) S[pos] += val;
}
int Query(int pos)
{
    int ret = 0;
    for(; pos > 0; pos -= lowbit(pos)) ret += S[pos];
    return ret;
}
int main()
{
//    FIN;
    CASE(T)
    {
        scanf("%d", &N);
        memset(S, 0, sizeof(S));
        MAX = 0;
        for(int i = 1; i <= N; i++)
        {
            scanf("%d", &a[i]);
            MAX = max(MAX, a[i]);
        }
        for(int i = 1; i <= N; i++)
        {
            L[i] = Query(a[i]);
            Add(a[i], 1);
        }
        memset(S, 0, sizeof(S));
        for(int i = N; i >= 1; i--)
        {
            R[i] = Query(a[i]);
            Add(a[i], 1);
        }
        LL ans = 0;
        for(int i = 2; i < N; i++)
        {
            ans += (L[i] * (N - i - R[i]) + (i - 1 - L[i]) * R[i]);
        }
        cout << ans << endl;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 20:43:09

POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】的相关文章

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong 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

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it 

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从

POJ 2155 Matrix 【二维树状数组】

题目链接:http://poj.org/problem?id=2155 题目大意:给出一个N*N的0矩阵,下面给出两种指令:1. 给出的第一个数据为'C',再给出四个整形数据,x1,y1,y1,y2,对以(x1,y1)(x2,y2)分别为左上角和右下角坐标的矩阵内的元素进行反转(0变1,1变0)         2. 给出的第一个数据为'Q',再给出两个数据,x,y,然后输出此时这个坐标上的元素. 这题用二维树状数组解,树状数组能够对某区间更新所有元素的和,树状数组维护的是c[1][1]到c[i

HDU 1892 二维树状数组

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3485    Accepted Submission(s): 1103 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno

HDU 3333 Turing Tree (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstrin