玄关求条,动态开点 RE 50
  • 板块P1908 逆序对
  • 楼主Z_kazuha
  • 当前回复3
  • 已保存回复3
  • 发布时间2025/1/11 11:31
  • 上次更新2025/1/11 17:32:27
查看原帖
玄关求条,动态开点 RE 50
1419569
Z_kazuha楼主2025/1/11 11:31
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,sum=1e9+7,ans,cnt=1;
struct node{int l,r,ls,rs,p;}tree[N<<2];
void pushup(int p){
	tree[p].p=tree[tree[p].ls].p+tree[tree[p].rs].p;
}
void update(int p,int L){
	if(tree[p].l==tree[p].r&&tree[p].l==L){
		tree[p].p++;
		return;
	}
	int mid=(tree[p].l+tree[p].r)>>1;
	if(!tree[p].ls){
		tree[p].ls=++cnt;
		tree[cnt].l=tree[p].l,tree[cnt].r=mid;
	}
	if(!tree[p].rs){
		tree[p].rs=++cnt;
		tree[cnt].l=mid+1,tree[cnt].r=tree[p].r;		
	}
	if(L<=mid)update(tree[p].ls,L);
	else update(tree[p].rs,L);
	pushup(p);
}
int query(int p,int L,int R){
	if(p==0)return 0;
	if(tree[p].l>=L&&tree[p].r<=R){
		return tree[p].p;
	}
	int mid=(tree[p].l+tree[p].r)>>1;
//	if(L<=mid)res+=query(tree[p].ls,L,R);
//	if(R>mid)res+=query(tree[p].rs,L,R);
//	return res;
	if(R<=mid)return query(tree[p].ls,L,R);
	else if(L>mid)return query(tree[p].rs,L,R);
	else{
		return query(tree[p].ls,L,mid)+query(tree[p].rs,mid+1,R);
	} 
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	tree[1].l=1,tree[1].r=sum;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		update(1,x);
		ans+=query(1,x+1,sum);
	}
	cout<<ans;
	return 0;
}
2025/1/11 11:31
加载中...