社区讨论

求条

P1908逆序对参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@m4f1wg96
此快照首次捕获于
2024/12/08 11:35
去年
此快照最后确认于
2025/11/04 13:09
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
int t[500005],a[500005],tmp[500005],n,m;
int lowbit(int x){
	return x&-x; 
}
void add(int x,int k){
	for(int i=x; i<=n; i+=lowbit(i)) t[i]+=k;
	return;
}
int sum(int x){
	int ans=0;
	for(int i=x; i>=1; i-=lowbit(i)) ans+=t[i];
	return ans;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin>>n;
	for(int i=1; i<=n; i++){
		cin>>a[i];
	}
	for (int i=1; i<=n; i++)
		tmp[i]=a[i];
	sort(tmp+1,tmp+n+1);
	int len=unique(tmp+1,tmp+n+1)-(tmp+1);
	for(int i=1; i<=n; ++i)
		a[i]=lower_bound(tmp+1,tmp+len+1,a[i])-tmp;
	int ans=0;
	for(int i=1; i<=n; i++){
        add(a[i],1);
        ans+=i-sum(a[i]);
    }
    cout<<ans<<"\n";
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...