QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#278840#7108. CouleurSATSKYAC ✓2798ms56788kbC++203.7kb2023-12-07 21:15:062023-12-07 21:15:07

Judging History

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

  • [2023-12-07 21:15:07]
  • 评测
  • 测评结果:AC
  • 用时:2798ms
  • 内存:56788kb
  • [2023-12-07 21:15:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;using ll=long long;const int M=998244353;
struct Seg_tr
{
	struct node
	{
		int cnt=0,l=0,r=0;
	};
	int n,B,opt=0,dpt;vector<node>tr;vector<int>bgt;
	void ini(int _n)//[0,n]
	{
		n=_n;for(B=1;B<=n;B<<=1);tr.resize(B<<1,node());dpt=(B<<1)-1;
		for(int i=B-1;i;i--)
		{			
			tr[i].l=(i<<1),tr[i].r=(i<<1|1);
		}
		bgt.resize(n+1);bgt[0]=1;
	}
	void updp(int L,int R,int pos,int v)
	{
		//cout<<L<<"*"<<R<<"*"<<pos<<"*"<<v<<'\n';
		tr[pos].cnt++;if(L==R)return;int mid=(L+R)/2;
		if(v<=mid)tr.push_back(tr[tr[pos].l]),tr[pos].l=++dpt,updp(L,mid,dpt,v);		
		else tr.push_back(tr[tr[pos].r]),tr[pos].r=++dpt,updp(mid+1,R,dpt,v);		
	}
	void upd(int v) 
	{ 
		//cout<<opt<<"->"<<bgt[opt]<<'\n';
		tr.push_back(tr[bgt[opt]]);
		bgt[++opt]=++dpt;
		//cout<<int(tr.size())<<"&"<<bgt[opt]<<"&"<<dpt<<"&"<<B<<"&"<<v<<'\n';
		updp(0,B-1,dpt,v); 
	}
	ll quep(int l,int r,int L,int R,int pos)
	{
		if(r<L||l>R)return 0;if(l<=L&&r>=R)return tr[pos].cnt;int mid=(L+R)/2;
		return quep(l,r,L,mid,tr[pos].l)+quep(l,r,mid+1,R,tr[pos].r);
	}
	ll que(int L,int R,int l,int r)
	{
		
		//cout<<B<<"*"<<L<<"*"<<R<<"*"<<bgt[L-1]<<"*"<<bgt[R]<<'\n';
		return quep(l,r,0,B-1,bgt[R])-quep(l,r,0,B-1,bgt[L-1]);
	}
};
struct S
{
	int n;Seg_tr A;
	vector<pair<int,int>>orr;
	vector<int>arr;vector<ll>pos;
	vector<pair<int,ll>>segs;//L/cnt
	set<int>bords;
	set<pair<ll,int>,greater<pair<ll,int>>>xS;//val/R
	void ini()
	{
		cin>>n;orr.resize(n+1);arr.resize(n+2);arr[n+1]=0;xS.insert({0,n+1});
		pos.resize(n+1);segs.resize(n+1,{-1,0});A.ini(n);
		for(int i=1,x;i<=n;i++)cin>>x,orr[i]={x,i};
		sort(orr.begin()+1,orr.end());
		for(int i=1;i<=n;i++)cin>>pos[i],arr[orr[i].second]=i;
		//for(int i=1;i<=n;i++)cout<<arr[i]<<"_\n"[i==n];
		for(int i=1;i<=n;i++)A.upd(arr[i]);//,cout<<i<<":"<<A.que(1,i,1,n)<<"\n";
		//for(int i=1;i<int(A.tr.size());i++)
		{
			//cout<<i<<"/"<<A.tr[i].l<<"*"<<A.tr[i].r<<"*"<<A.tr[i].cnt<<"\n";
		}
		//cout<<"updOK\n";
	}
	pair<ll,vector<int>>gval(int l,int r)
	{
		if(l>=r)return pair<ll,vector<int>>{0,vector<int>{arr[l]}};
		int mid=(l+r)/2,pt=0,rpt=0,siz=r-l+1;vector<int>res(siz);
		auto p1=gval(l,mid),p2=gval(mid+1,r);p1.second.push_back(n+1);
		ll sum=p1.first+p2.first;int s1=mid-l+1,s2=r-mid;
		for(int i=0;i<=s1;i++)
		{
			while(pt<s2&&p2.second[pt]<p1.second[i])sum+=s1-i,res[rpt++]=p2.second[pt++];
			if(i!=s1)res[rpt++]=p1.second[i];
		}
		return {sum,res};
	}
	void solve()
	{
		ini();bords.insert(n);segs[n]={1,gval(1,n).first};//cout<<segs[n].second<<"*\n";
		xS.insert({segs[n].second,n});
		for(int i=1;i<=n;i++)
		{
			ll res=(*xS.begin()).first;
			cout<<res<<" \n"[i==n];if(i==n)break;
			//cout<<"***"<<res<<"&"<<pos[i]<<'\n';
			int x=pos[i]^res;//cout<<"x:"<<x<<"|";
			int R=*bords.lower_bound(x);//cout<<"R:"<<R<<"|";
			if(R>n)exit(0);
			int L=segs[R].first;//cout<<"L:"<<L<<"\n";
			ll Asum=segs[R].second,v1,v2;xS.erase({Asum,R});
			ll vvv=A.que(L,x,arr[x]+1,n)+A.que(x,R,1,arr[x]-1);
			Asum-=vvv;
			//cout<<x<<" between "<<L<<"&"<<R<<':'<<vvv<<'\n';
			if(x-L>R-x)
			{
				for(int j=x+1;j<=R;j++)Asum-=A.que(L,x-1,arr[j]+1,n);
				v2=gval(x+1,R).first,v1=Asum-v2;
			}
			else 
			{
				for(int j=L;j<=x-1;j++)Asum-=A.que(x+1,R,1,arr[j]-1);
				v1=gval(L,x-1).first,v2=Asum-v1;
			}
			//cout<<v1<<"*"<<v2<<'\n';
			if(x>L)
			{
				segs[x-1]={L,v1};bords.insert(x-1);xS.insert({v1,x-1});
			}
			if(R>x)
			{
				segs[R]={x+1,v2};bords.insert(R);xS.insert({v2,R});
			}
			//ll vl=gval(L,x-1)
		}
	}
};
signed main()
{
	//freopen("1.in","r",stdin);
	ios::sync_with_stdio(0);cin.tie(0);
	int t=1;cin>>t;
	while(t--) { S SS;SS.solve(); }
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2798ms
memory: 56788kb

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