树状数组求逆序数及变形(个人理解)

        树状数组可以省时间而且省空间的求值和修改,相比于线段树来说代码量少,但我感觉树状数组求逆序数的功能更为强大,树状数组

        可以利用从当前加入的数到最大全部添加的优势快速的使比当前加入的数大的所有数加一,省时省空间.

    代码:

#include<bits/stdc++.h>
using namespace std;
int x[100010];
int szsz[100010];
int lowbit(int a){
    return a&(-a);
}
void add(int a){
    for(int i=a;i<=100000;i+=lowbit(i)){
        szsz[i]+=1;
    }
}
int qiuhe(int a){
    int ans=0;
    for(int i=a;i>=1;i-=lowbit(i)){
        ans+=szsz[i];
    }
    return max(ans-1,0);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x[i]);
    }
    memset(szsz,0,sizeof(szsz));
    int start,eend;   // start代表从几号开始,eend代表到几号结束,求中间的逆序数个数 、
    scanf("%d %d",&start,&eend);
    for(int i=start;i<=eend;i++){
        add(x[i]);
        printf("%d ",qiuhe(99999)-qiuhe(x[i]));
    }
    printf("\n");
    return 0;
}

      但对于题目来说单单求逆序数就很没意思了,例如让你求 x > y 时 ax<y 的个数,这种呢主要看你对于逆序数和公式的理解,一般来说

    这种题目比较简单的,因为观察可知qiuhe(MAX_N)- qiuhe(x[ i ])后面打入的是x[ i ],所以得出的是对于X[ i ]的逆序数,综上所述,如

    果想得到 y 的逆序数只需要打入  y  即可.

    例题    https://codeforces.com/problemset/problem/961/E

    ac代码 :

#include<bits/stdc++.h>
using namespace std;
int x[200020];
int szsz[200020];
int m,n;
vector <int> vic[200020];
int lowbit(int a){
    return a&(-a);
}
void add(int a){
    for(int i=a;i<=200015;i+=lowbit(i)){
        szsz[i]+=1;
    }
}
int qiuhe(int a){
    int ans=0;
    for(int i=a;i>=1;i-=lowbit(i)){
        ans+=szsz[i];
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x[i]);
        x[i]=min(n,x[i]);
        vic[min(i-1,x[i])].push_back(i);
    }
    memset(szsz,0,sizeof(szsz));
    long long sum=0;
    for(int i=1;i<=n;i++){
        add(x[i]);
        for(int j=0;j<vic[i].size();j++){
            sum+=qiuhe(n)-qiuhe(vic[i][j]-1);
        }
    }
    printf("%lld\n",sum);
    return 0;
}

原文地址:https://www.cnblogs.com/fzw1523/p/10268439.html

时间: 2024-08-30 01:37:43

树状数组求逆序数及变形(个人理解)的相关文章

Codeforces Round #261 (Div. 2) D. Pashmak and Parmida&#39;s problem (树状数组求逆序数 变形)

题目链接 题意: 给出一些数a[n],求(i, j), i<j 的数量,使得:f(1, i, a[i]) > f(j, n, a[j]) . f(lhs, rhs, x) 指在 { [lhs, rhs]范围中,a[k]的值=x } 的数量. 1.  f(1, i, a[i]) 就是指a[i]前面包括a[i]的数中,有几个值=a[i]. 2.  f(j, n, a[j]) 就是指a[j]后面包括a[j]的数中有几个值=a[j]. 虽然a[x]范围不小,但是n的范围是1000,不是很大,所以我们可

ZOJ-2386 Ultra-QuickSort 【树状数组求逆序数+离散化】

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input seque

树状数组求逆序数

poj 2299 树状数组求逆序数题目链接:http://poj.org/problem?id=2299 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

poj2299 Ultra-QuickSort 树状数组求逆序数

poj2299 Ultra-QuickSort   树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 49587   Accepted: 18153 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequenc

Dynamic Inversions II 逆序数的性质 树状数组求逆序数

Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N个数a[1],a[2] ... a[N],a[1]...a[N]是1-N的一个排列,即1 <= a[i] <= N且每个数都不相同.有M个操作,每个操作给出x,y两个数,你将a[x],a[y]交换,然后求交换后数组的逆序

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include