QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#160782#7108. Couleurucup-team266#AC ✓3234ms11864kbC++204.2kb2023-09-02 21:24:182023-09-02 21:24:18

Judging History

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

  • [2023-09-02 21:24:18]
  • 评测
  • 测评结果:AC
  • 用时:3234ms
  • 内存:11864kb
  • [2023-09-02 21:24:18]
  • 提交

answer

//Author: Kevin5307
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
mt19937 rnd(20210448);
int prior[100100],val[100100],siz[100100],ls[100100],rs[100100];
int rt[100100];
ll ans[100100];
int a[100100];
int merge(int x,int y)
{
	if(!x||!y) return x+y;
	if(prior[x]>prior[y])
	{
		rs[x]=merge(rs[x],y);
		siz[x]=siz[ls[x]]+siz[rs[x]]+1;
		return x;
	}
	else
	{
		ls[y]=merge(x,ls[y]);
		siz[y]=siz[ls[y]]+siz[rs[y]]+1;
		return y;
	}
}
void split(int x,int v,int &a,int &b)
{
	if(!x)
	{
		a=b=0;
		return ;
	}
	if(val[x]<=v)
	{
		a=x;
//		ls[a]=ls[x];
		split(rs[a],v,rs[a],b);
		siz[a]=siz[ls[a]]+siz[rs[a]]+1;
	}
	else
	{
		b=x;
//		rs[b]=rs[x];
		split(ls[b],v,a,ls[b]);
		siz[b]=siz[ls[b]]+siz[rs[b]]+1;
	}
}
void ssplit(int x,int s,int &a,int &b)
{
	if(!x)
	{
		a=b=0;
		return ;
	}
	if(siz[ls[x]]+1<=s)
	{
		a=x;
//		ls[a]=ls[x];
		ssplit(rs[a],s-siz[ls[x]]-1,rs[a],b);
		siz[a]=siz[ls[a]]+siz[rs[a]]+1;
	}
	else
	{
		b=x;
//		rs[b]=rs[x];
		ssplit(ls[b],s,a,ls[b]);
		siz[b]=siz[ls[b]]+siz[rs[b]]+1;
	}
}
int count(int &x,int v)
{
	int a,b;
	split(x,v,a,b);
//	cerr<<a<<" "<<b<<endl;
	int ans=siz[a];
	x=merge(a,b);
//	cerr<<"!"<<endl;
	return ans;
}
int insert(int x,int nd)
{
	int a,b;
	split(x,val[nd],a,b);
	return merge(a,merge(nd,b));
}
int erase(int &x,int v)
{
//	cerr<<"! "<<siz[x]<<endl;
	int a,b,c;
	split(x,v-1,a,b);
//	cerr<<siz[b]<<endl;
	ssplit(b,1,b,c);
//	cerr<<"! "<<siz[a]<<" "<<siz[b]<<" "<<siz[c]<<endl;
	x=merge(a,c);
//	cerr<<"! "<<siz[x]<<endl;
//	cerr<<val[b]<<" "<<v<<endl;
	assert(val[b]==v);
	return b;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			ls[i]=rs[i]=0;
			siz[i]=1;
			val[i]=a[i];
			prior[i]=rnd();
			ans[i]=0;
			rt[i]=0;
		}
		ans[n+1]=0;
		int r=n;
		ll sm=0;
		for(int i=n-1;i;i--)
		{
			sm+=count(r,a[i]-1);
			r=insert(r,i);
		}
		ans[1]=sm;
		rt[1]=r;
		set<int> st;
		multiset<ll> ms;
		st.insert(0);
		st.insert(n+1);
		ms.insert(ans[1]);
		for(int i=1;i<=n;i++)
		{
//			cerr<<endl;
//			for(int j=1;j<=n;j++)
//				cerr<<ans[j]<<" ";
//			cerr<<endl;
			auto it=ms.end();
			it--;
			ll lstans=*it;
			cout<<lstans;
			if(i<n)
				cout<<" ";
			else
				cout<<'\n';
			ll x;
			cin>>x;
			x^=lstans;
			auto it2=st.lower_bound(x);
			int r=*it2-1;
			it2--;
			int l=*it2+1;
			st.insert(x);
			ms.erase(ms.lower_bound(ans[l]));
//			cerr<<l<<" "<<r<<" "<<x<<endl;
			if(x-l<r-x)
			{
				ll nans=0;
				int nrt=0;
				for(int j=x;j>=l;j--)
				{
					int nd=erase(rt[l],a[j]);
					nans+=count(nrt,a[j]-1);
					nrt=insert(nrt,nd);
				}
				ans[x+1]=ans[l]-nans;
				for(int j=l;j<=x;j++)
					ans[x+1]-=count(rt[l],a[j]-1);
				rt[x+1]=rt[l];
				rt[l]=nrt;
				nans-=siz[nrt]-count(nrt,a[x]);
				erase(rt[l],a[x]);
				ans[l]=nans;
			}
			else
			{
				ll nans=0;
				int nrt=0;
//				cerr<<"!"<<endl;
				for(int j=r;j>=x;j--)
				{
					int nd=erase(rt[l],a[j]);
					nans+=count(nrt,a[j]-1);
//					cerr<<count(nrt,a[j]-1)<<" ";
					assert(val[nd]==a[j]);
					nrt=insert(nrt,nd);
				}
//				cerr<<endl;
//				cerr<<nans<<" "<<siz[rt[l]]<<endl;
				ans[l]-=nans;
				for(int j=x;j<=r;j++)
					ans[l]-=(siz[rt[l]]-count(rt[l],a[j]));
				rt[x+1]=nrt;
				nans-=count(nrt,a[x]-1);
				erase(rt[x+1],a[x]);
				ans[x+1]=nans;
			}
			if(!st.count(l))
				ms.insert(ans[l]);
			if(!st.count(x+1))
				ms.insert(ans[x+1]);
		}
	}
	return 0;
}

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

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 5688kb

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: 3234ms
memory: 11864kb

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