UESTC Training for Data Structures——A

 

Problem  A

Problem Description


  The Romans have attacked again,This time they are much more than the Persians but Shapue is ready to defeat them.He says:"A lion is never afraid of a hundred sheep".
  Nevertheless Shapur has to find weaknesses in the Roman army to defeat them,So he gives the army a weakness number.In Shapur's opinion the weakness of an army is equal to the number of triplets i,j,k such that i<j<k and ai>aj>ak,where ax is the power of man standing at position x.The Roman army has one special trail -- powers of all the people in it are distinct.
  Help Shapur find out how weak the Romans are.

Input

The first line is an integer T(T<=10)--The number of test cases.
For each of the test case,the first line contains a single number n(3<=n<=10^6)--the number of men in Roman army.The next line contains N different possitive integers ai(1<=ai<=10^6)--power of men in the Romen army.

Output

For each of the T cases,output a single number,the weakness of the Roman army.

Sample Input

3
3
3 2 1
3
2 3 1
4
10 8 3 1

Sample Output

1
0
4
/*将原来的数据按照从小到大一次hash成1--n,
然后每次计算[1,i]中比a[i]小的元素的个数x
每次ans增加的值就是 (i-1-x)*(a[i]-1-x),最后输出ans的值即可*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#define N 1000006
using namespace std;
struct data
{
    int num,id;
} b[N];
int a[N];
int sum[N];
void qsort(int st,int en)  //对输入的数据进行排序
{
    srand((unsigned)time(NULL));
    //产生一个st到en的随机数 ll
    int ll=rand()%(en-st)+st;
    int i=st,j=en;
    b[0]=b[ll]; b[ll]=b[st]; b[st]=b[0];
    while(i<j)
    {
        while(i<j && b[0].num<=b[j].num) j--;
        if(i<j) { b[i]=b[j]; i++; }
        while(i<j && b[0].num>=b[i].num) i++;
        if(i<j) { b[j]=b[i]; j--; }
    }
    b[i]=b[0];
    if(st<i-1) qsort(st,i-1);
    if(i+1<en) qsort(i+1,en);
}
__int64 get_sum(int *a,int pos)  //求和
{
    __int64 temp=0;
    while(pos>0)
    {
        temp+=a[pos];
        pos-=pos&(-pos);
    }
    return temp;
}
void update(int *a,int pos,int k)  //更新
{
    while(pos<N)
    {
        a[pos]+=k;
        pos+=pos&(-pos);
    }
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(sum,0,sizeof(sum));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i].num=a[i];
            b[i].id=i;
        }
        qsort(1,n);
        for(int i=1;i<=n;i++) a[b[i].id]=i; //将原来的数据按照从小到大hash成1--n
        __int64 ans=0;
        for(int i=1;i<=n;i++)
        {
            __int64 x=get_sum(sum,a[i]);  //找到[1,i]中,比a[i]小的元素的个数
            ans+=(i-1-x)*(a[i]-1-x);
            update(sum,a[i],1);  //将a[i]的元素的个数增加1个
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值