zoukankan      html  css  js  c++  java
  • poj 3928 ping pong 树状数组

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<math.h>
     6 #define maxx 100002
     7 using namespace std;
     8 int a[20022],c[100022],x[100022],n,zuo[100022],you[100022];
     9 
    10 int lowbit(int x)
    11  {
    12      return x&(-x);
    13  }
    14 
    15  __int64 sum(int p)
    16  {
    17      __int64 ans=0;
    18      while(p>0)
    19      {
    20          ans+=c[p];//printf("i %d   c %d  ans %d
    ",p,c[p],ans);
    21          p-=lowbit(p);
    22      }
    23      return ans;
    24  }
    25 
    26  void update(int p)
    27  {
    28      while(p<=100010)       //!!!!!!!!!!!!!!!!不是p<=n
    29      {
    30          c[p]++;
    31          p+=lowbit(p);
    32      }
    33 
    34  }
    35 int main()
    36 {
    37     int i,j,m,t;
    38     scanf("%d",&t);
    39     while(t--)
    40     {
    41         scanf("%d",&n);
    42         for(i=1;i<=n;i++)
    43             scanf("%d",&a[i]);
    44 
    45         memset(c,0,sizeof(c));
    46         for(i=1;i<=n;i++)
    47         {
    48             zuo[i]=sum(a[i]);
    49             update(a[i]);
    50         }
    51 
    52         memset(c,0,sizeof(c));
    53         for(i=n;i>=1;i--)
    54         {
    55             you[i]=sum(a[i]);
    56             update(a[i]);
    57         }
    58         long long final=0;
    59         for(i=1;i<=n;i++)
    60         {
    61             final+=(zuo[i]*(n-i-you[i]))+(you[i]*(i-zuo[i]-1));
    62         }
    63         printf("%lld
    ",final);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    java注释
    Java程序的编译与运行
    java 变量-数据类型转换
    java 基本数据类型之四类八种
    编写并解释第一个java程序
    java 基础知识(配置环境变量)
    常用DOS 命令
    java安装文件简介
    mysql权限问题
    vim 文本编辑器
  • 原文地址:https://www.cnblogs.com/assult/p/3545251.html
Copyright © 2011-2022 走看看