zoukankan      html  css  js  c++  java
  • 算法竞赛入门经典 LA 4329(树状数组)

    题意

      一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间

    问题

      

    《算法竞赛入门经典-训练指南》的分析:


    上代码

      

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 const int maxa = 100000+10;
     6 const int maxn = 20000+10;
     7 
     8 int a[maxn];
     9 int BIt[maxa];
    10 int c[maxn];
    11 int d[maxn];
    12 int n;
    13 long long ans;
    14 int lowbit(int x)
    15 {
    16     return x&(-x);
    17 }
    18 int sum(int x)
    19 {
    20     int ret = 0;
    21     while(x>0)
    22     {
    23         ret += BIt[x];
    24         x -= lowbit(x);
    25     }
    26     return ret;
    27 }
    28 void update(int x)
    29 {
    30     while(x<=maxa-1)
    31     {
    32         ++BIt[x];
    33         x += lowbit(x);
    34     }
    35 }
    36 int main()
    37 {
    38     //ios::sync_with_stdio(false);
    39     int t;
    40     //cin>>t;
    41     scanf("%d",&t);
    42     while(t--)
    43     {
    44         ans = 0;
    45         memset(BIt,0,sizeof(BIt));
    46         memset(c,0,sizeof(c));
    47         cin>>n;
    48         for(int i=1;i<=n;i++)
    49         {
    50             //cin>>a[i];
    51             scanf("%d",&a[i]);
    52         }
    53         for(int i=1;i<=n-1;i++)
    54         {
    55             c[i]=sum(a[i]-1);
    56             update(a[i]);
    57         }
    58         memset(BIt,0,sizeof(BIt));
    59         memset(d,0,sizeof(d));
    60         for(int i=n;i>=2;i--)
    61         {
    62             d[i]=sum(a[i]-1);
    63             update(a[i]);
    64         }
    65         for(int i=2;i<=n-1;i++)
    66             ans +=c[i]*(n-i-d[i])+(i-c[i]-1)*d[i];
    67         cout<<ans<<endl;
    68 
    69     }
    70     return 0;
    71 }

     ps:

      初学ing,写的不好。纯属自用

    一条有梦想的咸鱼
  • 相关阅读:
    Docker
    内存与缓存
    硬盘和内存的关系
    磁盘和硬盘、软盘
    mysql和redis的区别
    BZOJ 3110 k大数查询 (树套树)
    Codeforces 1303E Erase Subsequences (子序列匹配)
    Codeforces 1304E 1-Trees and Queries (树上距离+思维)(翻译向)
    BZOJ 2161 布娃娃(权值线段树)
    2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)
  • 原文地址:https://www.cnblogs.com/Crossea/p/11295661.html
Copyright © 2011-2022 走看看