社区讨论
树状数组求逆序对WA求调
学术版参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mj53gtfr
- 此快照首次捕获于
- 2025/12/14 10:17 2 个月前
- 此快照最后确认于
- 2025/12/14 10:18 2 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
#define int long long
int n,m,tr[N];
int lowbit(int x){
return x&(-x);
}
void add(int x,int c){
while(x<=n){
tr[x]+=c;
x+=lowbit(x);
}
}
int query(int x){
int cnt = 0;
while(x){
cnt+=tr[x];
x-=lowbit(x);
}
return cnt;
}
signed main(){
cin>>n;
int cnt = 0;
for(int i = 1,tmp;i<=n;i++){
cin>>tmp;
add(tmp,1);
cnt+=(i-query(tmp));
}
cout<<cnt;
return 0;
}
题目数据无需离散化
回复
共 0 条回复,欢迎继续交流。
正在加载回复...