树状数组求逆序对:
------------------------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;
char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)){
x=x*10+c-‘0‘;
c=getchar();
}
return x;
}
struct node{
int cur,w;
bool operator<(const node&rhs)const{
return rhs.w<w;
}
};
node a[100005];
node b[100005];
int f[100005];
int c[100005];
int n;
const int mod=99999997;
int lowbit(int x){
return x&-x;
}
void insert(int x){
for(int i=x;i<=n;i+=lowbit(i))
c[i]++;
}
int find(int x){
int ans=0;
for(int i=x;i>0;i-=lowbit(i)){
ans+=c[i];
}
return ans;
}
int main(){
n=read();
rep(i,n){
a[i].w=read(),a[i].cur=i;
}
rep(i,n){
b[i].w=read(),b[i].cur=i;
}
sort(a+1,a+n+1);
sort(b+1,b+n+1);
rep(i,n){
f[a[i].cur]=b[i].cur;
}
/*rep(i,n){
cout<<f[i]<<" ";
}*/
int ans=0;
rep(i,n){
ans=(ans+i-1-find(f[i]))%mod;
insert(f[i]);
}
printf("%d\n",ans);
return 0;
}
------------------------------------------------------------------------------------