QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#267118 | #7736. Red Black Tree | BoulevardDust | AC ✓ | 279ms | 55284kb | C++20 | 5.0kb | 2023-11-26 22:51:40 | 2024-02-19 10:15:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define see(x) cerr << x << " "
#define seeln(x) cerr << x << endl
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define re
inline void Rd(int &res){
re char c;res=0;
while(c=getchar(),c<48);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
}
const int N = 100005;
int n;
int col[N];
char str[N];
vector<int> G[N];
struct node {
int src;
int cnt;
int k;
};
const int inf = 1000000000;
int len[N];
int ans[N];
int Log[N];
int vA[N][19], vB[N][19];
void dfs(int x) {
if (G[x].empty()) {
len[x] = 1;
return;
}
len[x] = inf;
for (int y : G[x]) {
dfs(y);
if (len[y] + 1 < len[x]) {
len[x] = len[y] + 1;
}
}
}
#define sz(x) (int)(x.size())
vector<int> dp[N], Dp[N];
int sum[N];
node cur[N];
int calcdp(int x, int id) {
// out(x); out(id); outln(sz(dp[x]));
if (id < 0 || id > len[x]) return inf;
return dp[x][id];
}
int calcDp(int x, int id) {
if (id < 0 || id > len[x]) return inf;
return Dp[x][id];
}
int qA(int l, int r) {
int k = Log[r - l + 1];
return min(vA[l][k], vA[r - (1 << k) + 1][k]);
}
int qB(int l, int r) {
int k = Log[r - l + 1];
return min(vB[l][k], vB[r - (1 << k) + 1][k]);
}
void calc_top(int x) {
int y = cur[x].src;
int k = cur[x].k;
int cnt = cur[x].cnt;
// out(x); out(y); out(k); outln(cnt);
Dp[x].resize(len[x] + 1);
for (int i = 0; i <= len[x]; ++i) {
Dp[x][i] = inf;
vA[i][0] = vB[i][0] = inf;
}
for (int i = 0; i <= len[y] && i <= len[x]; ++i) {
vA[i][0] = dp[y][i] + i;
vB[i][0] = dp[y][i] - i;
}
/*for (int i = 0; i <= len[x]; ++i) {
out(i); out(vA[i][0]); outln(vB[i][0]);
}*/
for (int i = 1; i <= Log[len[x]]; ++i) {
for (int j = 0; j <= len[x] - (1 << i) + 1; ++j) {
vA[j][i] = min(vA[j][i - 1], vA[j + (1 << (i - 1))][i - 1]);
vB[j][i] = min(vB[j][i - 1], vB[j + (1 << (i - 1))][i - 1]);
}
}
/*for (int i = 0; i <= len[x]; ++i) {
for (int j = i; j <= len[x]; ++j) {
out(i); out(j); out(qA(i, j)); outln(qB(i, j));
}
}*/
for (int i = 0; i <= len[x]; ++i) {
/*for (int j = 0; j <= i && j <= k; ++j) {
Dp[x][i] = min(Dp[x][i], calcdp(y, i - j) + abs(cnt - j));
}*/
int s1, s2;
s1 = i - min(i, min(k, cnt)), s2 = i - 0;
if (s1 <= s2) {
Dp[x][i] = min(Dp[x][i], qA(s1, s2) + cnt - i);
/*for (int k = s1; k <= s2; ++k) {
Dp[x][i] = min(Dp[x][i], calcdp(y, k) + k + cnt - i);
}*/
}
s1 = i - min(i, k), s2 = i - (cnt + 1);
if (s1 <= s2) {
Dp[x][i] = min(Dp[x][i], qB(s1, s2) + i - cnt);
/*for (int k = s1; k <= s2; ++k) {
Dp[x][i] = min(Dp[x][i], calcdp(y, k) - k + i - cnt);
}*/
}
/*for (int j = 0; j <= i && j <= k && j <= cnt; ++j) {
Dp[x][i] = min(Dp[x][i], calcdp(y, i - j) + i - j + cnt - i);
}
for (int j = cnt + 1; j <= i && j <= k; ++j) {
Dp[x][i] = min(Dp[x][i], calcdp(y, i - j) + j - i + i - cnt);
}*/
}
}
node run(int x) {
if (sz(G[x]) == 0) {
cur[x] = (node){x, 0, 0};
dp[x] = {0, 0};
if (col[x]) dp[x][0] = 1;
else dp[x][1] = 1;
// out(x); outln(cur[x].src);
return cur[x];
}
if (sz(G[x]) == 1) {
node tmp = run(G[x][0]);
tmp.k += 1;
tmp.cnt += col[x];
ans[x] = ans[G[x][0]];
cur[x] = tmp;
return tmp;
}
for (int y : G[x]) run(y);
// see("run"); outln(x);
dp[x].resize(len[x] + 1);
for (int i = 0; i <= len[x]; ++i) {
sum[i] = 0;
dp[x][i] = inf;
}
for (int y : G[x]) {
if (dp[y].empty()) {
calc_top(y);
for (int i = 0; i <= len[x]; ++i) {
sum[i] = min(sum[i] + calcDp(y, i), inf);
}
} else {
for (int i = 0; i <= len[x]; ++i) {
sum[i] = min(sum[i] + calcdp(y, i), inf);
}
}
}
if (col[x]) {
for (int h = 0; h <= len[x]; ++h) {
int tmp = inf;
tmp = min(tmp, sum[h] + 1);
if (h) tmp = min(tmp, sum[h - 1]);
dp[x][h] = tmp;
}
} else {
for (int h = 0; h <= len[x]; ++h) {
int tmp = inf;
tmp = min(tmp, sum[h]);
if (h) tmp = min(tmp, sum[h - 1] + 1);
dp[x][h] = tmp;
}
}
ans[x] = inf;
for (int i = 0; i <= len[x]; ++i) {
ans[x] = min(ans[x], dp[x][i]);
}
return (node){x, 0, 0};
}
void clear() {
for (int i = 1; i <= n; ++i) {
G[i].clear();
dp[i].clear();
Dp[i].clear();
}
for (int i = 0; i <= n; ++i) sum[i] = 0;
for (int i = 0; i <= n; ++i) col[i] = 0;
for (int i = 0; i <= n; ++i) len[i] = 0;
for (int i = 0; i <= n; ++i) ans[i] = 0;
for (int i = 0; i <= n; ++i) cur[i] = (node){0, 0, 0};
}
int main() {
int T; Rd(T); while (T--) {
Rd(n);
scanf("%s", str + 1);
for (int i = 1; i <= n; ++i) col[i] = str[i] - 48;
Log[0] = -1;
for (int i = 1; i <= n; ++i) {
Log[i] = Log[i >> 1] + 1;
}
for (int i = 2; i <= n; ++i) {
int u; scanf("%d", &u);
G[u].push_back(i);
}
dfs(1); run(1);
for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], " \n"[i == n]);
clear();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 10072kb
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: 0
Accepted
time: 229ms
memory: 55284kb
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 0 0 0 0 5 3 ...
result:
ok 6107 lines
Test #3:
score: 0
Accepted
time: 279ms
memory: 26756kb
input:
10 100000 10001010000001100001000100001000010100010101100001001110110001000010000110001000000010000011000001000001010001110100000000000000000010011011100000000000001000000000100100100110000000100001010011000000110000000111010100100001100000100100101001000000010000000011100000000000000010001100011100...
output:
22440 21414 19422 13454 5328 2719 1421 1168 1478 661 4662 5037 418 183 2304 501 2008 1643 692 2211 570 1003 967 950 504 124 894 333 775 523 905 197 12 337 195 310 1325 1016 638 50 907 361 179 336 313 102 309 555 733 871 490 414 375 318 66 625 336 267 276 162 203 25 112 216 252 146 42 233 232 333 27 ...
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 111ms
memory: 16008kb
input:
10 100000 01010111111101011100011111111010011001111111110001100111111101011111110011101111110110111011010111011011010011111110101111111011111111011101011111011001110101111011011010110100011111001101001011111101111101111111111100101101000111111110111101111111111011111100111011101110110101111010101101...
output:
25019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 146ms
memory: 35624kb
input:
10 100000 00111110110011111111111010011111011111101010110111111110011110111111111111000110101110110111111101011111111111010101111111011001110110011101111001110111101101110110101000011111110100101110110100111110001111011100111101011010111111011011100011111011110111111110011110111111001111111010011100...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 166ms
memory: 34912kb
input:
10 100000 00111100100100001111011000100000000000111001100000000000100000101001001010010000001000010010111000001011010000000000001000000000010100000010010010000001000010001000000100000001010000000000000000000001000110000010100100000010000011000000000010010000100010100000000100000100100011000000001000...
output:
4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 ...
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 271ms
memory: 42936kb
input:
10 100000 00111101111001101110101101111110100001010100011011100001011100000110000100100010111010011001111011100010010011111100000011111011001001000110000101101001011110000000011100001010100001000110110101111010000100000111001110001100001001001000101110100111111000101101100000011001110111001111101011...
output:
210 210 210 210 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 261ms
memory: 34668kb
input:
10 100000 00010011111100101111110000110010101110000000001111011110011010011011101011010000111100001001111111111001000110100010001111010111101100101111101100001001100110000011010100110110010101000010111001001010110111011000010100110101110001100110101010101001100010100000100000100101011110000100001001...
output:
6360 6360 4803 4803 1549 1549 1555 1555 1555 1595 1555 1549 1549 1555 1555 1600 1555 1549 1549 1549 1555 1555 1555 1555 1595 1549 1555 1555 1555 1555 1595 1595 1600 1555 1600 1600 1595 1549 1555 1555 1549 1600 1595 1600 1555 1595 1595 1549 1549 1549 1549 1555 1549 1549 1600 1595 1555 1549 1549 1555 ...
result:
ok 10 lines
Test #9:
score: 0
Accepted
time: 138ms
memory: 19776kb
input:
10 100000 11000111101111111101001011010110110111010011000111011111011111110111110000110101111111011101111011111111101110011100011101111111001011111101011111110010011111101111111011101101100110101010011111110111111101100101010011111111111100101111111101111100011100111110111111011111011001111110011101...
output:
18217 12003 6214 6012 5991 3232 2982 3008 3004 2973 3017 1780 1451 1499 1483 1513 1495 1486 1516 1499 1474 1509 1508 1037 743 738 713 722 776 752 731 741 771 759 735 731 755 740 776 733 765 736 738 753 756 739 769 678 359 362 380 364 374 342 370 356 364 393 382 367 385 360 371 370 371 383 387 387 37...
result:
ok 10 lines
Test #10:
score: 0
Accepted
time: 132ms
memory: 30644kb
input:
10 100000 10111111011011110010111111111111110111101110110001011111101110111011111111111101101011111101101001100111011011011101001110110110101010010001010111111111111111111011011011101011011101100001111101111110111110010011011111111011101110111111111110010011110011111011011011111111101111001110111111...
output:
50037 0 50035 0 50034 0 50033 0 50033 0 50032 0 50030 0 50029 0 50029 0 50027 0 50025 0 50024 0 50023 0 50022 0 50021 0 50020 0 50019 0 50019 0 50018 0 50017 0 50015 0 50014 0 50012 0 50012 0 50011 0 50011 0 50010 0 50009 0 50008 0 50006 0 50005 0 50003 0 50002 0 50000 0 49999 0 49998 0 49997 0 4999...
result:
ok 10 lines
Extra Test:
score: 0
Extra Test Passed