QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#266895 | #7736. Red Black Tree | ucup-team2307# | WA | 214ms | 28940kb | C++20 | 2.5kb | 2023-11-26 19:08:26 | 2023-11-26 19:08:26 |
Judging History
answer
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
template<typename F>
void multitest(F func) {
int t;
cin >> t;
while(t--) func();
}
void report(int ok) {
cout << (ok?"Yes":"No") << '\n';
}
struct DP {
int best = 0, cc = 0, line_r = 0, line_b = 0, dep = 1, col;
vector<int> vals { 0 };
DP(int Col = 0) : col(Col) {
(col ? line_b : line_r)++;
}
void attach(DP &ch) {
if(cc == 0) {
swap(vals, ch.vals);
line_r += ch.line_r;
line_b += ch.line_b;
best = ch.best;
dep = 1 + ch.dep;
cc = 1;
} else {
(col ? ch.line_b : ch.line_r)++;
dep = min(dep, 1 + ch.dep);
unfuck(dep);
ch.unfuck(dep);
best = 1<<30;
for(int i = 0; i <= dep; i++) {
vals[i] += ch.vals[i];
best = min(best, vals[i]);
}
}
}
void unfuck(int S) {
// vector<int> ndp(S + 1, n + 100);
// cout << line_r << " " << line_b << endl;
int sz = max(1 + S, (int)vals.size());
// cout << line_r <<"r:\n";
for(int z = 1; line_r; z *= 2) {
z = min(z, line_r);
// cout << z << endl;
line_r -= z;
vals.resize(sz, 1<<20);
vector<int> nvals(sz);
for(int i = nvals.size(); i--;)
nvals[i] = min(vals[i], i - z >= 0 ? vals[i - z] + z : 1<<20);
vals = nvals;
}
// cout << line_b <<"b:\n";
for(int z = 1; line_b; z *= 2) {
z = min(z, line_b);
// cout << z << endl;
line_b -= z;
// auto nvals = vals;
// nvals.insert(nvals.begin(), 1<<20);
vals.resize(sz, 1<<20);
vector<int> nvals(sz);
for(int i = nvals.size(); i--;)
nvals[i] = min(i - z >= 0 ? vals[i - z] : 1<<20, vals[i] + z);
vals = nvals;
}
vals.resize(S + 1, 1<<20);
// for(int i = 0; i <= S; i++) {
// if(i - line_b >= 0)
// ndp[i] = vals[i - line_b];
// }
// for(int z = 1<<30; z>>=1;) {
// if(line_b) {}
// }
}
};
signed main() {
cin.tie(0)->sync_with_stdio(0);
int T;cin >>T;
while(T--) {
int n;
cin >> n;
string col;
cin >> col;
vector<vector<int>> g(n);
for(int t, i = 1; i < n; i++) {
cin >> t;
g[t - 1].push_back(i);
}
vector<DP> dp(n);
vector<int> ans(n);
auto dfs = [&](auto self, int v) -> void {
dp[v] = DP(col[v] - '0');
for(auto i : g[v]) {
self(self, i);
dp[v].attach(dp[i]);
}
ans[v] = dp[v].best;
};
dfs(dfs, 0);
for(auto i : ans) cout << i << " ";
cout << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
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: 214ms
memory: 28940kb
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 2 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 2 0 0 0 0 0 0 0 0...
result:
wrong answer 3rd lines differ - expected: '1 0 0 0 0 0 0', found: '2 0 0 0 0 0 0 '