hdu2492(树状数组)

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

题目大意:给定n个数,求有多少个三元组(也就是三个数一组,不一定要连续),三元组必须是递增或者递减的序列
思路:明天我们要找到三元组中间的那个数,假设为i,那么就要算出i前面有多少个大于a[i]的数 ,记作left1[i],多少个小于a[i]的数,记作right1[i];同时算出i后面有多少个大于a[i]的数,记作left2[i],多少个小于a[i]的数记作right2[i]
那么以i为中点的三元组就算出来了 left1[i]×right2[i] + left2[i]×right1[i]; 最后的ans就是遍历每个数字即可

那么最后用数组数组维护4个数组即可,我写的比较冗余,但是比较清楚

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1000005;//注意ai的范围是1000000不是20000
int left1[maxn],right1[maxn],n,a[maxn];
int left2[maxn],right2[maxn];
int cnt1[maxn],cnt2[maxn];//两个树状数组
int lowbit(int x)
{
    return x&(-x);
}
void update(int i)
{
    while(i<=maxn-1)
    {
        cnt1[i]++;//因为是统计个数
        i+=lowbit(i);
    }
}
int getsum(int i)
{
    int res=0;
    while(i>0)
    {
        res += cnt1[i];
        i-=lowbit(i);
    }
    return res;
}
void update2(int i)
{
    while(i<=maxn-1)
    {
        cnt2[i]++;//因为是统计个数
        i+=lowbit(i);
    }
}
int getsum2(int i)
{
    int res=0;
    while(i>0)
    {
        res += cnt2[i];
        i-=lowbit(i);
    }
    return res;
}
void init()
{
    memset(left1,0,sizeof left1);
    memset(right1,0,sizeof right1);
    memset(left2,0,sizeof left2);
    memset(right2,0,sizeof right2);
    memset(cnt1,0,sizeof cnt1);
    memset(cnt2,0,sizeof cnt2);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            update(a[i]);
            left1[i]=getsum(a[i])-1;//a[i]前面小于a[i]的个数
            right1[i]=i-left1[i]-1;//a[i]前面大于a[i]的个数,也就是getsum(n)-getsum(a[i])
//          right1[i]=getsum(maxn)-getsum(a[i]);//和上面等价
        }
        //反向处理一下
        for(int i=n;i>=1;i--)
        {
            update2(a[i]);
            left2[i]=getsum2(a[i])-1;//和上面的操作一样
            right2[i]=(n-i+1)-left2[i]-1;
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=(ll)left1[i]*right2[i] + (ll)left2[i]*right1[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值