QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#403244 | #6790. Binary Tree Restoring | lonelywolf | TL | 0ms | 3712kb | C++20 | 1.7kb | 2024-05-01 23:19:28 | 2024-05-01 23:19:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N];
int b[N];
int fa[N];
int nxt[N];
int p1[N], p2[N];
int m[N<<1];
int n;
void dfs(int l1,int r1,int l2,int r2,int f){
if (!(1 <= l1 && l1 <= r1 && r1 <= n && 1 <= l2 && l2 <= r2 && r2 <= n)) {
return;
}
if(l1==r1){
fa[a[l1]]=f;
return;
}
if (r1 - l1 == 1) {
if (a[l1] == b[l2]) {
fa[a[l1]] = f;
fa[a[r1]] = a[l1];
} else {
fa[a[l1]] = f;
fa[a[r1]] = f;
}
return;
}
if(a[l1]==b[l2]){
fa[a[l1]]=f;
int t = nxt[l1];
if (t == 0 || t > r1) {
dfs(l1+1,r1,l2+1,r2,a[l1]);
} else {
fa[a[t]] = f;
dfs(l1+1,t-1,l2+1,l2 - l1 + t - 1,a[l1]);
dfs(t+1,r1,r2 - r1 + t + 1,r2,a[t]);
}
}
else{
fa[a[l1]]=f;
fa[b[l2]]=f;
int t = p2[a[l1]];
dfs(l1+1,l1+r2-t,t+1,r2,a[l1]);
dfs(l1+r2-t+2,r1,l2+1,t-1,b[l2]);
}
}
void solve() {
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i];
for (int i = 1; i <= n; i++) {
nxt[i] = 0;
}
for (int i = 1; i <= 2 * n; i++) {
m[i] = 0;
}
for (int i = 1; i <= n; i++) {
p1[a[i]] = i;
p2[b[i]] = i;
}
for (int i = n; i >= 1; i--) {
nxt[i] = m[i - p2[a[i]] + n];
m[i - p2[a[i]] + n] = i;
}
fa[a[1]]=0;
dfs(2,n,2,n,a[1]);
for(int i=1;i<=n;i++){
cout<<fa[i]<<" \n"[i == n];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
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 2
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...