树状数组模板
【代码】
树状数组模板
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define f(i,n) for(int i=1;i<=(n);i++)
#define ll long long
#define INF 1<<30
#define N 100010
#define c 99999997
using namespace std;
int z[N];
int szsz[N];
int n;
int lowbit(int x)
{
return x&(-x);
}
int query(int k)
{
int ans=0;
for(;k;k-=lowbit(k))ans+=szsz[k];
return ans;
}
int add(int k)
{
for(;k<=n;k+=lowbit(k))szsz[k]++;
}
int main()
{
scanf("%d",&n);
f(i,n)scanf("%d",&z[i]);
int ans=0;
f(i,n)
{
add(z[i]);
ans+=i-query(z[i]);
}
printf("%d",ans);
}