QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#404641 | #7736. Red Black Tree | zhangtx123 | RE | 0ms | 19500kb | C++14 | 3.5kb | 2024-05-04 11:58:03 | 2024-05-04 11:58:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define debag(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 200010
struct node {
vector<int>h1, h2;
int c, sum;
void clear() {
h1.clear(); h2.clear();
c = sum = 0;
}
}a[N];
int dep[N], sum[N], son[N], h[N], ans[N];
int id[N], top, hi[N];
vector<int> G[N];
int n, m, i, j, k, T;
char str[N];
void dfs1(int x) {
sum[x] = str[x] - '0';
dep[x] = 1; son[x] = 0;
for(int y : G[x]) {
dfs1(y); sum[x] += sum[y];
dep[x] = max(dep[x], dep[y] + 1);
if(dep[y] < dep[son[x]]) son[x] = y;
}
// debug("son[%d] = %d\n", x, son[x]);
}
void tran(int i, int h[N], int len) {
int k = 0;
for(int x : a[i].h1) {
if(k == len) return ;
h[++k] += x;
}
for(int x = 1; x <= a[i].c; ++x) {
if(k == len) return ;
h[++k] += 0;
}
for(int y = a[i].h2.size() - 1; y >= 0; --y) {
int x = a[i].h2[y];
if(k == len) return ;
h[++k] += x;
}
// debug("??? %d == %d\n", k, len);
// assert(k == len);
}
void itran(int i, int h[N], int len) {
int k = 0, k2; a[i].clear();
for(k = 1; k <= len && h[k] < 0; ++k) a[i].h1.pb(h[k]), a[i].sum += h[k];
for(; k <= len && h[k] == 0; ++k) a[i].c++;
for(k2 = len; k2 >= k && h[k2] > 0; --k2) a[i].h2.pb(h[k2]);
// debug("??? %d + 1 == %d\n", k2, k);
// assert(k2 + 1 == k);
}
void dfs2(int x) {
int &i = id[x];
if(!son[x]) {
i = ++top; a[i].clear();
if(str[x] == '0') a[i].h2.pb(1);
else a[i].h1.pb(-1), a[i].sum -= 1;
ans[x] = sum[x] + a[i].sum;
// debug("For %d is %d\n", x, a[i].sum);
return ;
}
dfs2(son[x]); i = id[son[x]];
for(int y : G[x]) if(y != son[x]) {
dfs2(y); k = 0; j = id[y];
int len = a[i].h1.size() + a[i].c + a[i].h2.size();
for(k = 1; k <= len; ++k) h[k] = hi[k] = 0;
// debug("len is %d[for %d com(%d %d)]\n", len, x, son[x], y);
tran(i, h, len); tran(j, hi, len);
// debug("># "); for(k = 1; k <= len; ++k) debug("%d ", h[k]); debug("\n");
// debug("># "); for(k = 1; k <= len; ++k) debug("%d ", hi[k]); debug("\n");
for(k = 1; k <= len; ++k) h[k] += hi[k];
// debug("# "); for(k = 1; k <= len; ++k) debug("%d ", h[k]); debug("\n");
itran(i, h, len); //tran(i, h, len);
// for(k = 1; k <= len; ++k) h[k] = 0;
// debug("#check "); for(k = 1; k <= len; ++k) debug("%d ", h[k]); debug("\n");
}
if(str[x] == '0') a[i].h2.pb(1);
else a[i].h1.pb(-1), a[i].sum -= 1;
ans[x] = sum[x] + a[i].sum;
// debug("For %d is %d[%d]\n", x, a[i].sum, ans[x]);
}
signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
T = read(); int cnt = 0;
while(T--) {
++cnt;
n = read(); scanf("%s", str + 1); top = 0; dep[0] = 1e9;
if(cnt == 325) {
printf("%s ", str + 1);
for(i = 2; i <= n; ++i) k = read(), printf("%d ", k);
printf("\n");
}
for(i = 1; i <= n; ++i) G[i].clear();
for(i = 2; i <= n; ++i) k = read(), G[k].pb(i);
dfs1(1); dfs2(1);
for(i = 1; i <= n; ++i) printf("%d ", ans[i]);
printf("\n");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 19500kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
4 1 2 0 0 0 0 0 0 2 0 0 0
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
input:
6107 12 000000001000 1 2 3 2 5 4 4 7 3 8 11 19 1100111101111011110 1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5 7 0111110 1 1 2 2 1 5 3 000 1 1 7 1000011 1 2 3 3 5 4 7 0001001 1 1 1 3 5 3 8 00111000 1 1 3 2 5 2 7 11 11111110111 1 1 1 4 5 4 5 2 5 1 15 110101101000010 1 2 3 2 1 5 2 5 6 5 8 7 9 14 10 0101000...
output:
1 1 1 1 0 0 0 0 0 0 0 0 6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 2 1 0 0 0 0 0 0 4 0 0 2 1 0 0 0 0 0 0 4 3 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 5 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0...