QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#576195 | #6790. Binary Tree Restoring | xly_tyty | WA | 0ms | 13908kb | C++17 | 1.4kb | 2024-09-19 19:16:48 | 2024-09-19 19:16:49 |
Judging History
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;i++)cout<<fa[i]<<' ';cout<<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: 0
Wrong Answer
time: 0ms
memory: 13908kb
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:
wrong output format Expected EOLN