• Hdu 2492 树状数组 Ping pong


     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define MAXN 100005
     5 #define M 20005
     6 typedef __int64 LL ;
     7 using namespace std;
     8 LL n,max_num ;
     9 LL s[M],a[M],b[M],c[MAXN];
    10 LL lowbit(LL x){
    11     return x&(-x);
    12 }
    13 LL sum(LL x){
    14     LL ret = 0;
    15     while(x > 0){
    16         ret += c[x];
    17         x -= lowbit(x);
    18     }
    19     return ret;
    20 }
    21 void add(LL x, LL d){
    22     while(x<=max_num){
    23         c[x] += d;
    24         x += lowbit(x);
    25     }
    26 }
    27 int main(){
    28     LL t,i,j,ans;
    29     cin>>t;
    30     while(t--){
    31         cin>>n;
    32         max_num = 0;
    33         for(i = 1;i <= n ;i++){
    34             scanf("%I64d",&s[i]);
    35             if(s[i]>max_num)
    36                 max_num = s[i];
    37         }
    38         memset(c,0,sizeof(c));
    39         for(i = 1;i <= n ; i++){
    40             add(s[i],1);
    41             a[i] = sum(s[i]-1);
    42         }
    43         memset(c,0,sizeof(c));
    44         for(i = n; i>0 ;i--){
    45             add(s[i],1);
    46             b[i] = sum(s[i]-1);
    47         }
    48         ans = 0;
    49         for(i = 1;i <= n ;i++){
    50             //cout<<a[i]<<" "<<b[i]<<endl;
    51             ans = ans+a[i]*(n-i-b[i])+(i-a[i]-1)*b[i];
    52         }
    53         cout<<ans<<endl;
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    封装小程序http请求
    ES6为数组做的扩展
    练习题
    二叉树的遍历
    快速搭建vue项目
    收集的前端面试大全
    ios兼容webp格式的图片
    小程序开发API之获取启动参数
    使用HTML编写邮件
    深入理解javascript原型和闭包(9)——this
  • 原文地址:https://www.cnblogs.com/fancy-itlife/p/4774663.html
Copyright © 2020-2023  润新知