QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#125256 | #5663. Tangle: A DAG for storing transactions | Wavelet | TL | 0ms | 0kb | C++14 | 3.1kb | 2023-07-16 12:58:02 | 2023-07-16 12:58:04 |
Judging History
answer
#include <bits/stdc++.h>
#define up(l, r, i) for(int i = l, END##i = r;i <= END##i;++ i)
#define dn(l, r, i) for(int i = l, END##i = r;i >= END##i;-- i)
using namespace std;
using i64 = long long;
int qread(){
int w = 1, c = 0, r = 0;
while(!isdigit(c = getchar())) w = c == '-' ? -1 : 1; r = c - '0';
while( isdigit(c = getchar())) r = r * 10 + c - '0';
return w * r;
}
const int MAXN= 3e5 + 3;
const int MAXM= 6e5 + 3;
int P[MAXM], O[MAXM], X[MAXN], Y[MAXN], H[MAXM]; bool V[MAXN];
int main(){
up(1, qread(), _){
int n = qread();
up(1, 2 * n - 1, i) P[i] = qread();
P[1] = P[2 * n - 1] = 1;
stack <int> A; A.push(1);
stack <int> B; B.push(1);
{
int s = 1, ss = 0;
up(2, 2 * n - 1, i) if(P[i]){
if(P[i] == ss) ss = s, s = A.top(), A.pop();
else ss = s, A.push(s = P[i]);
} else break;
}
{
int s = 1, ss = 0;
dn(2 * n - 2, 2, i) if(P[i]){
if(P[i] == ss) ss = s, s = B.top(), B.pop();
else ss = s, B.push(s = P[i]);
} else break;
}
int xx = 0; while(!A.empty()) X[++ xx] = A.top(), A.pop();
int yy = 0; while(!B.empty()) Y[++ yy] = B.top(), B.pop();
up(xx + 1, n, i) X[i] = 0;
up(yy + 1, n, i) Y[i] = 0;
reverse(X + 1, X + 1 + xx);
reverse(Y + 1, Y + 1 + yy);
up(1, n , i) V[ i ] = false;
up(1, 2 * n - 1, i) V[P[i]] = true;
int h = 0, q = 0;
up(1, n, i) if(V[i] == false)
H[++ h] = i;
int o = 0;
// printf("xx = %d. ", xx); up(1, xx, i) printf("%d ", X[i]); puts("");
// printf("yy = %d. ", yy); up(1, yy, i) printf("%d ", Y[i]); puts("");
while(1){
// up(1, o, i) printf("%d, ", O[i]); puts("");
if(xx > yy || X[xx] != Y[xx]){
int u = X[xx - 1];
if(q < h){
int v = H[q + 1];
// printf("A : %d, %d\n", u, v);
if(u < v) -- xx, O[++ o] = u, X[xx + 1] = 0;
else ++ q, O[++ o] = v, X[++ xx] = v;
} else -- xx, O[++ o] = u, X[xx + 1] = 0;
} else if(xx < yy){
int u = Y[xx + 1];
if(q < h){
int v = H[q + 1];
if(u < v) ++ xx, O[++ o] = u, X[xx] = u;
else ++ q, O[++ o] = v, X[++ xx] = v;
} else ++ xx, O[++ o] = u, X[xx] = u;
} else if(q < h){
int u = H[q + 1];
++ q, O[++ o] = u, X[++ xx] = u;
} else break;
}
int g = 0;
up(1, 2 * n - 1, i){
if(P[i] == 0) printf("%d ", O[++ g]);
else printf("%d ", P[i]);
}
puts("");
}
return 0;
}
/*
9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0
*/
详细
Test #1:
score: 0
Time Limit Exceeded
input:
6 12 2 0 1 3 3 0 2 2 4 1 2 1 5 2 3 3 6 3 4 4 7 3 5 5