QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#599708#7108. CouleurxuzhihaodedieAC ✓2902ms76832kbC++203.3kb2024-09-29 09:30:272024-09-29 09:30:27

Judging History

你现在查看的是最新测评结果

  • [2024-09-29 09:30:27]
  • 评测
  • 测评结果:AC
  • 用时:2902ms
  • 内存:76832kb
  • [2024-09-29 09:30:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define x first
//#define y second
#define PII pair<int,int>
const int N=1e5+10;
struct node {
	int l,r,sum;
}t[N*25];
int root[N],idx,a[N];
int z[N];
void build(int &x,int l,int r) {
	x=++idx;
	if(l==r) return ;
	int mid=l+r>>1;
	build(t[x].l,l,mid);
	build(t[x].r,mid+1,r);
}
void insert(int x,int &y,int l,int r,int k) {
	y=++idx;
	t[y]=t[x];
	t[y].sum++;
	if(l==r) return ;
	int mid=l+r>>1;
	if(k<=mid) insert(t[x].l,t[y].l,l,mid,k);
	else insert(t[x].r,t[y].r,mid+1,r,k);
}
int query(int x,int y,int l,int r,int val) {//找比val大的数有几个 
	if(l==r) {
		if(l>=val) return t[y].sum-t[x].sum;
		return 0;
	}
	int mid=l+r>>1;
	if(val<=l) return t[y].sum-t[x].sum; 
	if(val>r) return 0;
	if(val<=mid) return query(t[x].l,t[y].l,l,mid,val)+query(t[x].r,t[y].r,mid+1,r,val);
	else return query(t[x].r,t[y].r,mid+1,r,val);
}
int query1(int x,int y,int l,int r,int val) {//找比val小的数有几个 
	if(l==r) {
		if(l<=val) return t[y].sum-t[x].sum;
		return 0;
	}
	int mid=l+r>>1;
	if(val>=r) return t[y].sum-t[x].sum; 
	if(val<l) return 0;
	if(val<=mid) return query1(t[x].l,t[y].l,l,mid,val);
	else return query1(t[x].r,t[y].r,mid+1,r,val)+query1(t[x].l,t[y].l,l,mid,val);
}
void solve() {
	int n;
	cin>>n;
	int maxn=0;
	idx=0;
	for(int i=1;i<=n;i++) cin>>a[i],maxn=max(maxn,a[i]);
	int ans=0;
	build(root[0],1,maxn);
	for(int i=1;i<=n;i++) {
		ans+=query(root[0],root[i-1],1,maxn,a[i]+1);
		insert(root[i-1],root[i],1,maxn,a[i]);
	}
	multiset<int> s;
	set<int> st;
	st.insert(0);
	st.insert(n+1);
	s.insert(ans);
	vector<int> w;
	for(int i=1;i<=n;i++) cin>>z[i];
	map<PII,int> mp;
	mp[{0,n+1}]=ans;
	for(int i=1;i<=n;i++) {
		auto p=s.end();
		p--;
		ans=*p;
		w.push_back(ans);
		int x=z[i];
		x^=ans;
		auto q=st.upper_bound(x);
		int r=*q;
		q--;
		int l=*q;
		int res1=0,res2=0,res3=0;
		if(x-l<=r-x) {
			for(int j=l+1;j<x;j++) {
				res1+=query(root[l],root[j-1],1,maxn,a[j]+1);//左区间逆序对数 
			}
			for(int j=l+1;j<x;j++) {
				res2+=query1(root[x],root[r-1],1,maxn,a[j]-1);//横跨两区间的逆序对数
			}
			int ret1=0,ret2=0;
			ret1+=query(root[l],root[x-1],1,maxn,a[x]+1);
			ret2+=query1(root[x],root[r-1],1,maxn,a[x]-1);
			res3=ret1+ret2;
			int res=mp[{l,r}];
			int ret=res-res1-res2-res3;//右区间逆序对个数
			s.erase(s.lower_bound(res));
			s.insert(ret);
			s.insert(res1);
			mp[{l,x}]=res1;
			mp[{x,r}]=ret;
		} else {
			for(int j=x+1;j<r;j++) {
				res1+=query(root[x],root[j-1],1,maxn,a[j]+1);//右区间逆序对数 
			}
			for(int j=x+1;j<r;j++) {
				res2+=query(root[l],root[x-1],1,maxn,a[j]+1);//横跨两区间的逆序对数 
			}
			int ret1=0,ret2=0;
			ret1+=query(root[l],root[x-1],1,maxn,a[x]+1);
			ret2+=query1(root[x],root[r-1],1,maxn,a[x]-1);
			res3=ret1+ret2;
			int res=mp[{l,r}];
			int ret=res-res1-res2-res3;//左区间逆序对个数
			s.erase(s.lower_bound(res));
			s.insert(ret);
			s.insert(res1);
			mp[{l,x}]=ret;
			mp[{x,r}]=res1;
		}
		st.insert(x);
	}	
	for(int i=0;i<w.size();i++) {
		if(i<w.size()-1) cout<<w[i]<<" ";
		else cout<<w[i]<<endl;
	}
}
signed main() {
	int T=1;
	cin>>T;
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(T--) {
		solve();
	}
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5640kb

input:

3
5
4 3 1 1 1
5 4 5 3 1
10
9 7 1 4 7 8 5 7 4 8
21 8 15 5 9 2 4 5 10 6
15
4 8 8 1 12 1 10 14 7 14 2 9 13 10 3
37 19 23 15 7 2 10 15 2 13 4 5 8 7 10

output:

7 0 0 0 0
20 11 7 2 0 0 0 0 0 0
42 31 21 14 14 4 1 1 1 0 0 0 0 0 0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2902ms
memory: 76832kb

input:

11116
10
10 5 10 3 6 4 8 5 9 8
31 27 24 11 12 3 0 2 3 1
10
8 2 7 2 8 10 1 10 9 10
6 5 2 13 2 1 0 1 3 1
10
7 10 7 6 1 3 10 6 7 9
21 18 10 1 6 5 4 8 9 10
10
2 10 4 8 8 5 7 2 6 7
20 10 9 1 15 0 4 2 9 7
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
6 3 5 2 7 10 9 1 4 8
10
1 10 1 3...

output:

21 18 16 12 10 6 4 1 1 0
12 12 10 10 4 4 4 2 1 0
20 16 9 5 3 3 3 0 0 0
22 14 8 8 5 5 2 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
19 12 7 4 4 2 2 1 0 0
20 18 8 3 1 1 0 0 0 0
45 21 21 10 3 3 3 0 0 0
17 11 8 2 1 1 1 0 0 0
13 4 1 0 0 0 0 0 0 0
29 27 22 15 9 7 4 3 1 0
26 16 9 2 1 1 1 1 1 0
0 0 0 0 0 ...

result:

ok 11116 lines

Extra Test:

score: 0
Extra Test Passed