QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#621497 | #7736. Red Black Tree | SCAU_xyjkanade | WA | 0ms | 6396kb | C++14 | 1.3kb | 2024-10-08 14:59:01 | 2024-10-08 14:59:01 |
Judging History
answer
#include <bits/stdc++.h>
#define MAXN ((int) 1e5)
using namespace std;
int n, ans[MAXN + 10];
char s[MAXN + 10];
vector<int> e[MAXN + 10];
typedef pair<int, vector<int> > pim;
pim dfs(int sn) {
int v = 0;
vector<int> cf;
for (int fn : e[sn]) {
pim tmp = dfs(fn);
v += tmp.first;
if (cf.empty()) cf = move(tmp.second);
else {
int sz = min(cf.size(), tmp.second.size());
// 只保留两个差分数组较短的前缀
for (int i = 0; i < sz; i++) {
cf[i] += tmp.second[i];
}
}
}
// 根据 sn 原来的颜色,往差分数组里插入 1 或 -1
v += s[sn] - '0';
if (s[sn] == '0') {
cf.push_back(1);
} else {
cf.push_back(-1);
for (int& d : cf) d--;
}
int negSum = 0;
for (int d : cf) negSum += d;
ans[sn] = v + negSum;
return pim(v, move(cf));
}
void solve() {
scanf("%d%s", &n, s + 1);
for (int i = 1; i <= n; i++) e[i].clear();
for (int i = 2; i <= n; i++) {
int x; scanf("%d", &x);
e[x].push_back(i);
}
dfs(1);
for (int i = 1; i <= n; i++) printf("%d%c", ans[i], "\n "[i < n]);
}
int main() {
int tcase; scanf("%d", &tcase);
while (tcase--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 6396kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
-5 1 -3 1 -1 -3 -1 -1 1 -2 1 -3 -1
result:
wrong answer 1st lines differ - expected: '4 1 2 0 0 0 0 0 0', found: '-5 1 -3 1 -1 -3 -1 -1 1'