社区讨论

不吸氧就T?

P1908逆序对参与者 4已保存回复 3

讨论操作

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

当前回复
3 条
当前快照
1 份
快照标识符
@lo14epxh
此快照首次捕获于
2023/10/22 15:01
2 年前
此快照最后确认于
2023/11/02 14:33
2 年前
查看原帖
RT
CPP
//code by xiaozhangma
#include<bits/stdc++.h>
#define LL long long
#define F(x,s,t) for(int x=s;x<=t;x++)
#define lowbit(x) (x & -x)
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void write(LL x){
	if(x < 0)putchar('-'),x = -x;
	if(x > 9)write(x / 10);
	putchar(x % 10 + '0');
}
const int N = 5e5 + 10;
LL n, a[N], b[N], tr[N];
map <int, int> m;
void add(int x,int k){
	while(x <= n){
		tr[x] += k;
		x += lowbit(x);
	}
}
LL ask(int x){
	LL ans = 0;
	while(x){
		ans += tr[x];
		x -= lowbit(x);
	}
	return ans;
}
int main(){
	n = read();
	F(i, 1, n){
		a[i] = read();
		b[i] = a[i];
	}
	
	sort(b + 1, b + n + 1);
	int idx = 0;
	F(i, 1, n){
		if(!m[b[i]])m[b[i]] = ++ idx ;
	}
	
	LL ans = 0; 
	/*
	F(i, 1, n){
		ans += ask(idx);
		ans -= ask(m[a[i]] - 1);
		add(m[a[i]], 1);
	}
	*/
	for(int i = n; i; i --){
		ans += ask(m[a[i]] - 1);
		add(m[a[i]], 1);
	}
	write(ans);
	puts("");
	return 0;
}

回复

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

正在加载回复...