QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#576203#6790. Binary Tree Restoringxly_tytyTL 0ms13796kbC++171.4kb2024-09-19 19:19:012024-09-19 19:19:01

Judging History

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

  • [2024-09-19 19:19:01]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:13796kb
  • [2024-09-19 19:19:01]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const long long N=1e6+10,mod=998244353,INF=1e15;
ll n,m,k,a[N],b[N],sum[N],fa[N],id[N],lst[N];
bool st[N];
int x[N];
void f(int u,int l,int r,int L,int R)
{
	if(l>r)return;
	if(l==r)
	{
		fa[a[l]]=u;return;
	}
	if(a[l]!=b[L])
	{
		fa[a[l]]=u,fa[b[L]]=u;
		int idx1=-1,idx2=-1;
		for(int i=l;i<=r;i++)if(a[i]==b[L])
		{
			idx1=i;break;
		}
		for(int i=L;i<=R;i++)if(b[i]==a[l])
		{
			idx2=i;break;
		}
		f(a[l],l+1,idx1-1,idx2+1,R),f(b[L],idx1+1,r,L+1,idx2-1);
	}
	else{
		int res=0;
		int idx1=-1,idx2=-1;
		for(int i=l,j=L;i<=r;i++,j++)
		{
			res^=x[a[i]];
			res^=x[b[j]];
			if(res==0&&a[i+1]==b[j+1])
			{
				idx1=i+1,idx2=j+1;
				break;
			}
		}
		if(idx1!=-1)
		{
			fa[a[l]]=u,fa[a[idx1]]=u;
			f(a[l],l+1,idx1-1,L+1,idx2-1),f(a[idx1],idx1+1,r,idx2+1,R);
		}
		else{
			fa[a[l]]=u;
			f(a[l],l+1,r,L+1,R);
		}
	}
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)fa[i]=0;
	for(int i=1;i<=n;i++)cin>>a[i],id[a[i]]=i;
	for(int i=1;i<=n;i++)lst[i]=-1;
	for(int i=1;i<=n;i++)cin>>b[i];
	for(int i=1;i<=n;i++)x[i]=rand();
	f(a[1],2,n,2,n);
	for(int i=1;i<=n-1;i++)cout<<fa[i]<<' ';cout<<fa[n]<<endl;
}
signed main()
{
    cin.tie(0), cout.tie(0), ios::sync_with_stdio(false);
    int T;cin>>T;
    while(T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13796kb

input:

2
6
3 4 2 5 1 6
3 4 5 2 1 6
3
1 2 3
1 2 3

output:

3 4 0 3 4 1
0 1 1

result:

ok Yes

Test #2:

score: -100
Time Limit Exceeded

input:

119
384
59 43 220 380 283 327 361 90 206 20 356 89 273 113 44 86 83 371 46 145 156 17 350 362 226 337 344 12 247 266 322 127 223 117 125 93 309 294 270 281 331 169 260 35 102 140 85 141 13 115 40 63 61 26 91 111 49 6 353 241 50 154 57 41 149 254 213 70 343 33 97 92 379 48 235 19 229 179 34 16 132 17...

output:


result: