HDU-2492 pingpong(树状数组)

Ping pong

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

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

Recommend

gaojie

题目大意:给一列数,第一个数是人数n,后面n个数依次是每个人的能力排名,每两个人比赛,要找第三个人当裁判,第三个能力必须在两人之间并且位置也必须在两人之间。三个人其中任意一个人不同都算不同的比赛,求一共有多少比赛。

解题思路:以第三个人为主体,求出每个人前面有多少人排名比自己高和低,求出每个人后面有多少人排名比自己高和低,然后 (前高 * 后低)+ (前低 * 后高) 就是此人当裁判的场数。相加即可。代码比较清晰,直接上代码。(可还是被long long 坑了几次。。)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=5*1e5+5;
long long n;
long long a[maxn],a1[maxn],a2[maxn],b1[maxn],b2[maxn],c[maxn];

long long lowbit(long long x)
{
    return x&(-x);
}

void update(long long x,long long value)
{
    for(;x<=maxn;x+=lowbit(x))
    {
        c[x] += value;
    }
}

long long getsum(long long x)
{
    long long sum=0;
    for(;x>0;x-=x&(-x))
        sum += c[x];
    return sum;
}

int main()
{
    int i,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            update(a[i],1);
            a1[i] = getsum(a[i]-1);//小
            a2[i] = i-a1[i]-1;//大
            //printf("a1[%d]--%d  a2[%d]--%d\n",i,a1[i],i,a2[i]);
        }
        memset(c,0,sizeof(c));
        for(i=n;i>0;i--)
        {
            update(a[i],1);
            b1[i] = getsum(a[i]-1);//小
            b2[i] = n-i-b1[i];//大
            //printf("b1[%d]--%d  b2[%d]--%d\n",i,b1[i],i,b2[i]);
        }
        long long ans = 0;
        for(i=1;i<=n;i++)
        {
            ans += a1[i]*b2[i]+a2[i]*b1[i];
        }
        printf("%lld\n",ans);
    }
}
时间: 2024-10-24 06:04:45

HDU-2492 pingpong(树状数组)的相关文章

HDU Cow Sorting (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cow

hdu 5193 分块 树状数组 逆序对

题意: 给出n个数,a1,a2,a3,...,an,给出m个修改,每个修改往数组的某个位置后面插入一个数,或者把某个位置上的数移除.求每次修改后逆序对的个数. 限制: 1 <= n,m <= 20000; 1 <= ai <= n 思路: 插入和删除用分块来处理,块与块之间用双向链表来维护,每一块用树状数组来求小于某个数的数有多少个. 外层可以使用分块维护下标,这样添加和删除元素的时候,也很方便,直接暴力.查找权值个数时,使用树状数组比较方便.内层通过树状数组维护权值. 每次更新即

HDU 2689Sort it 树状数组 逆序对

Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4110    Accepted Submission(s): 2920 Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent s

HDU 5493 Queue 树状数组

Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Description N people numbered from 1 to N are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now,

hdu 4455 Substrings(树状数组+递推)

题目链接:hdu 4455 Substrings 题目大意:给定一个长度为N的序列,现在有Q次询问,每次给定一个w,表示长度,输出序列中长度为w的连续子序列 的权值和.序列的权值表示序列中不同元素的个数. 解题思路:递推,先预处理处每个位置和前面相同的数据的最短距离P.dp[i]表示说长度为i子序列的权值和,dp[i+1] = dp[i] + v - c.v为[i+1~N]中P值大于i的个数,我们可以看作将长度为i的子序列长度向后增加1,那么v则为增加长度带来 的权值增加值,c则是最后一个长度为

POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

一开始想,总感觉是DP,可是最后什么都没想到.还暴力的交了一发. 然后开始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得很清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?其实y根本木有用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.edu

hdu 4031(树状数组+辅助数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "S

HDU 2838 (DP+树状数组维护带权排序)

Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意:每头牛有个愤怒值,每次交换相邻两个数进行升序排序,$cost=val_{1}+val_{2}$,求$\min \sum cost_{i}$ 解题思路: 按输入顺序DP: 第i的值val的最小cost=当前数的逆序数个数*val+当前数的逆序数和 相当于每次只

hdu 4046 Panda 树状数组

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been on the airplane to U.S. We have known for 15 years, which has exceeded one-fifth of my whole life. I still remember the first time we went to the mov

hdu 2852(树状数组+二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2668    Accepted Submission(s): 1227 Problem Description For the k-th number,