QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344984 | #7736. Red Black Tree | ucup-team173 | TL | 1522ms | 34020kb | C++17 | 3.6kb | 2024-03-05 22:16:48 | 2024-03-05 22:16:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back
#define SZ(a) (int(a.size()))
typedef long long ll;
typedef double db;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
std::mt19937_64 gen(std::chrono::system_clock::now().time_since_epoch().count());
ll get(ll l, ll r) { std::uniform_int_distribution<ll> dist(l, r); return dist(gen); }
void solve() {
int n;
cin >> n;
string s;
cin >> s;
s = ' ' + s;
vector G(n + 1, vi());
for(int i = 2; i <= n; i++) {
int p;
cin >> p;
G[p].pb(i);
}
vector pos(n + 1, vi()), neg(n + 1, vi());
vi cnt(n + 1), dep(n + 1), p(n + 1), ans(n + 1), sz(n + 1);
function<void(int)> dfs = [&](int x) {
dep[x] = 1e9, sz[x] = s[x] == '1';
for(int y : G[x]) {
dfs(y);
dep[x] = min(dep[x], dep[y] + 1);
sz[x] += sz[y];
}
sort(G[x].begin(), G[x].end(), [&](int a, int b) {
return dep[a] < dep[b];
});
int fir;
if(SZ(G[x])) {
fir = G[x][0];
swap(pos[x], pos[fir]);
swap(neg[x], neg[fir]);
swap(cnt[x], cnt[fir]);
swap(p[x], p[fir]);
}
for(int y : G[x]) if(y != fir) {
assert(SZ(neg[y]) + cnt[y] + SZ(pos[y]) - p[y] == dep[y]);
int lst = dep[y] - dep[x] + 1;
while(lst && p[y] < SZ(pos[y])) p[y]++, lst--;
while(lst && cnt[y]) cnt[y]--, lst--;
while(lst && SZ(neg[y])) neg[y].pop_back(), lst--;
for(int i = 0; i < SZ(neg[y]); i++) {
if(i < SZ(neg[x])) {
neg[x][i] += neg[y][i];
} else if(i < SZ(neg[x]) + cnt[x]) {
cnt[x]--, neg[x].pb(neg[y][i]);
} else {
int id = SZ(pos[x]) - (i - SZ(neg[x]) - cnt[x]) - 1;
pos[x][id] += neg[y][i];
if(pos[x][id] == 0) {
cnt[x]++;
pos[x].pop_back();
} else if(pos[x][id] < 0) {
neg[x].pb(pos[x][id]);
pos[x].pop_back();
}
}
}
for(int i = 0; i + p[y] < SZ(pos[y]); i++) {
if(i + p[x] < SZ(pos[x])) {
pos[x][i + p[x]] += pos[y][i + p[y]];
} else if(i + p[x] - SZ(pos[x]) < cnt[x]) {
cnt[x]--, pos[x].pb(pos[y][i + p[y]]);
} else {
int id = SZ(neg[x]) - (i + p[x] - SZ(pos[x]) - cnt[x]) - 1;
neg[x][id] += pos[y][i + p[y]];
if(neg[x][id] == 0) {
cnt[x]++;
neg[x].pop_back();
} else if(neg[x][id] > 0) {
pos[x].pb(neg[x][id]);
neg[x].pop_back();
}
}
}
}
int g = (s[x] == '0') - (s[x] == '1');
if(g > 0) pos[x].pb(g);
else neg[x].pb(g);
ans[x] = sz[x];
for(int i : neg[x]) ans[x] += i;
if(SZ(G[x]) == 0) dep[x] = 1;
};
dfs(1);
for(int i = 1; i <= n; i++) {
cout << ans[i] << " \n"[i == n];
}
}
signed main() {
atexit([](){cerr << "time: " << (db)clock()/CLOCKS_PER_SEC << "s\n";});
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3976kb
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: 1522ms
memory: 34020kb
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: 16956kb
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: 135ms
memory: 16872kb
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: -100
Time Limit Exceeded
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 ...