QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#351699 | #7736. Red Black Tree | oscaryang# | WA | 304ms | 45220kb | C++20 | 3.5kb | 2024-03-12 12:40:26 | 2024-03-12 12:40:26 |
Judging History
answer
#include<bits/stdc++.h>
#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;
inline int read() {
int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x;
}
const int N = 1e5 + 5, inf = 1e9;
int n, a[N], ans[N];
int DFN, fa[N], dfn[N], son[N], h[N], le[N], ri[N], b[N], r[N];
char str[N];
vc<int> G[N];
namespace sgt {
int tre1[N << 3], tre2[N << 3];
#define mid (l + r >> 1)
#define ls (k << 1)
#define rs (k << 1 | 1)
inline void build(int k, int l, int r) {
tre1[k] = tre2[k] = inf;
if(l != r) build(ls, l, mid), build(rs, mid + 1, r);
}
inline void psu(int k) { tre1[k] = min(tre1[ls], tre1[rs]); tre2[k] = min(tre2[ls], tre2[rs]); }
inline void ins(int k, int l, int r, int p, int v) {
if(l == r) return tre1[k] = v - l, tre2[k] = v + l, void();
return (p <= mid ? ins(ls, l, mid, p, v) : ins(rs, mid + 1, r, p, v)), psu(k);
}
inline int ask1(int k, int l, int r, int L, int R) {
if(L > R || L > r || R < l) return inf;
if(L <= l && R >= r) return tre1[k];
return min(ask1(ls, l, mid, L, R), ask1(rs, mid + 1, r, L, R));
}
inline int ask2(int k, int l, int r, int L, int R) {
if(L > R || L > r || R < l) return inf;
if(L <= l && R >= r) return tre2[k];
return min(ask2(ls, l, mid, L, R), ask2(rs, mid + 1, r, L, R));
}
}
using namespace sgt;
inline void dfs1(int x) {
son[x] = 0;
for(auto y : G[x]) {
dfs1(y);
son[x] = h[y] > h[son[x]] ? y : son[x];
}
h[x] = h[son[x]] + 1;
}
inline void dfs2(int x) {
dfn[x] = ++DFN;
if(!son[x]) {
le[x] = DFN; ri[x] = ++DFN;
h[x] = 1; b[x] = r[x] = 0;
return ;
}
for(auto y : G[x]) dfs2(y);
if(G[x].size() == 1) {
le[x] = le[son[x]]; ri[x] = ri[son[x]];
h[x] = h[son[x]]; b[x] = b[son[x]]; r[x] = r[son[x]];
if(a[x]) ++b[x];
else ++r[x];
return ;
}
for(auto y : G[x]) h[x] = min(h[x], h[y] + 1);
le[x] = dfn[x]; ri[x] = dfn[x] + h[x]; b[x] = r[x] = 0;
}
inline int query(int x, int i) {
int res = inf;
res = min(res, ask1(1, 1, DFN, max(le[x], le[x] + i - r[x] - b[x]), min(ri[x], le[x] + i - b[x])) + le[x] + i - b[x]);
res = min(res, ask2(1, 1, DFN, max(le[x], le[x] + i - b[x]), min(ri[x], le[x] + i)) - le[x] - i + b[x]);
return res;
}
inline void dfs3(int x) {
if(!son[x]) {
ans[x] = 0;
ins(1, 1, DFN, le[x], a[x] ? 1 : 0);
ins(1, 1, DFN, ri[x], a[x] ? 0 : 1);
return ;
}
for(auto y : G[x]) dfs3(y);
if(G[x].size() == 1) {
ans[x] = ans[son[x]];
return ;
}
vc<int> dp(h[x] + 1, 0); ans[x] = inf;
for(auto y : G[x])
rep(i, 0, h[x] - 1)
dp[i] += query(y, i);
if(a[x]) {
lep(i, h[x], 1) dp[i] = dp[i - 1]; dp[0] = inf;
rep(i, 0, h[x] - 1) dp[i] = min(dp[i], dp[i + 1] + 1);
}
else {
dp[h[x]] = inf;
rep(i, 1, h[x]) dp[i] = min(dp[i], dp[i - 1] + 1);
}
rep(i, 0, h[x]) ins(1, 1, DFN, le[x] + i, h[x]), ans[x] = min(ans[x], dp[i]);
}
inline void testcase() {
n = read(); scanf("%s", str + 1);
rep(i, 1, n) a[i] = str[i] - '0', G[i].clear();
rep(i, 2, n) fa[i] = read(), G[fa[i]].pb(i);
DFN = 0; dfs1(1); dfs2(1);
build(1, 1, DFN);
dfs3(1);
rep(i, 1, n) printf("%d%c", ans[i], " \n"[i == n]);
}
signed main() {
int t = read(); while(t--) testcase();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10048kb
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
Wrong Answer
time: 304ms
memory: 45220kb
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:
2 2 2 1 0 0 0 0 0 0 0 0 6 4 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 0 0 0 0 3 1 0 0 0 0 0 0 3 0 0 2 1 0 0 0 0 0 0 2 3 0 0 2 0 0 0 0 0 0 0 0 0 0 4 0 1 0 0 0 0 0 0 0 4 5 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 7 1 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 3 4 ...
result:
wrong answer 1st lines differ - expected: '1 1 1 1 0 0 0 0 0 0 0 0', found: '2 2 2 1 0 0 0 0 0 0 0 0'