P3157 动态逆序对 Windows与洛谷样例结果不同 求原因
  • 板块学术版
  • 楼主shiruoyu114514
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/2/27 20:16
  • 上次更新2024/2/27 22:00:53
查看原帖
P3157 动态逆序对 Windows与洛谷样例结果不同 求原因
766764
shiruoyu114514楼主2024/2/27 20:16
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

struct node{
	int l,r;
	int lson,rson;
	tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> tree_less;
	tree<int,null_type,greater<int>,rb_tree_tag,tree_order_statistics_node_update> tree_greater;
	
	node(){
		tree_less.clear();
		tree_greater.clear();
	}
	void add(int x){
		tree_less.insert(x);
		tree_greater.insert(x);
	}
	int ask_less(int x){
		return tree_less.order_of_key(x);
	}
	int ask_greater(int x){
		return tree_greater.order_of_key(x);
	}
};

int answer=0;

struct Tree{
	vector<node> work;
	int n;
	
	Tree(int n){
		work.reserve(4*n+1);
		work.resize(2);
	}
	void build(int now,int l,int r){
		work[now].l=l;
		work[now].r=r;
		if(l!=r){
			node emp;
			work.push_back(emp);
			work[now].lson=work.size()-1;
			work.push_back(emp);
			work[now].rson=work.size()-1;
			build(work[now].lson,l,(l+r)/2);
			build(work[now].rson,(l+r)/2+1,r);
		}
	}
	int ask_greater(int now,int l,int r,int x){
		if(l>work[now].r||r<work[now].l){
			return 0;
		}
		if(l<=work[now].l&&work[now].r<=r){
			return work[now].ask_greater(x);
		}
		return ask_greater(work[now].lson,l,r,x)+ask_greater(work[now].rson,l,r,x);
	}
	int ask_less(int now,int l,int r,int x){
		if(l>work[now].r||r<work[now].l){
			return 0;
		}
		if(l<=work[now].l&&work[now].r<=r){
			return work[now].ask_less(x);
		}
		return ask_less(work[now].lson,l,r,x)+ask_less(work[now].rson,l,r,x);
	}
	void insert(int now,int x,int val){
		//cerr<<now<<" "<<x<<" "<<val<<"\n";
		if(work[now].l<=x&&x<=work[now].r){
			work[now].add(val);
			if(work[now].l!=work[now].r){
				insert(work[now].lson,x,val);
				insert(work[now].rson,x,val);
			}
		}
	}
	void Insert(int x,int val){
		answer+=ask_greater(1,1,x-1,val);
		answer+=ask_less(1,x+1,n,val);
		insert(1,x,val);
	}
};

int main(){
	int n,m;
	cin>>n>>m;
	int a[n+1];
	int weizhi[n+1];
	bool erased[n+1];
	for(int i=1;i<=n;i++){
		cin>>a[i];
		weizhi[a[i]]=i;
		erased[i]=0;
	}
	int op[m+1];
	for(int i=1;i<=m;i++){
		cin>>op[i];
	}
	for(int i=1;i<=m;i++){
		erased[op[i]]=1;
	}
	int ans[m+1];
	Tree work(n);
	work.build(1,1,n);
	for(int i=1;i<=n;i++){
		if(!erased[i]){
			work.Insert(i,a[i]);
		}
	}
	//return 0;
	for(int i=m;i>=1;i--){
		work.Insert(weizhi[op[i]],op[i]);
		ans[i]=answer;
	}
	for(int i=1;i<=m;i++){
		cout<<ans[i]<<"\n";
	}
}

本地结果正确,洛谷输出四个1

2024/2/27 20:16
加载中...