严格逆序对排序部分:
struct Node{
int v,id;
bool operator<(const Node &b)const{
return v<b.v;
//小于
}
}b[N];
非严格逆序对排序部分:
struct Node{
int v,id;
bool operator<(const Node &b)const{
return v<=b.v;
//小于等于
}
}b[N];
完整代码:
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define endl '\n'
#define itn int
#define pi pair<int,int>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define int ll
using namespace std;
const int MOD1=1e9+7;
const int MOD2=998244353;
const int N=2e5+5;
struct Node{
int v,id;
bool operator<(const Node &b)const{
return v<=b.v;
}
}b[N];
int n,ans,a[N],t[N];
int lowbit(itn x){
return x&(-x);
}
void add(itn x,itn k){
for(int i=x;i<=n;i+=lowbit(i))
t[i]+=k;
}
int ask(int x){
int res=0;
for(int i=x;i;i-=lowbit(i))
res+=t[i];
return res;
}
inline void Solve(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>b[i].v,b[i].id=i;
stable_sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
a[b[i].id]=i;
for(int i=1;i<=n;i++){
add(a[i],1);
ans+=i-ask(a[i]);
}
cout<<ans;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
freopen("cross.in","r",stdin);
freopen("cross.out","w",stdout);
int T=1;
//cin>>T;
while(T--)
Solve();
return 0;
}
站外题 AC。求正确性证明 or hack