QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#562291 | #7736. Red Black Tree | chiliplus# | WA | 182ms | 26900kb | C++14 | 2.9kb | 2024-09-13 16:31:48 | 2024-09-13 16:31:49 |
Judging History
answer
#include <bits/stdc++.h>
const int MAXN = 1e5 + 5;
std :: vector<int> son[MAXN];
std :: vector<int> dp[MAXN];
char typ[MAXN];
int q1[MAXN], h1, t1;
int q2[MAXN], h2, t2;
int lag[MAXN][2], ans[MAXN];
int n;
void init() {
for( int i = 1 ; i <= n ; i ++ ) {
son[i].clear(), dp[i].clear();
lag[i][0] = lag[i][1] = 0;
}
}
void normalise( std :: vector<int> &f, const int &lim, const int &r, const int &b ) {
std :: vector<int> res( lim + 1 );
h1 = h2 = 1, t1 = t2 = 0;
for( int i = 0 ; i <= lim ; i ++ ) {
// maintain suffix
int cur = i;
if( cur < ( int ) f.size() ) {
while( h2 <= t2 && f[q2[t2]] + q2[t2] >= f[cur] + cur ) t2 --;
q2[++ t2] = cur;
}
while( h2 <= t2 && q2[h2] < i - b ) h2 ++;
// maintain prefix
cur = i - b - 1;
if( cur >= 0 ) {
while( h1 <= t1 && f[q1[t1]] - q1[t1] >= f[cur] - cur ) t1 --;
q1[++ t1] = cur;
}
while( h1 <= t1 && q1[h1] < i - b - r ) h1 ++;
res[i] = 1e9;
if( h1 <= t1 ) res[i] = std :: min( res[i], f[q1[h1]] + i - b - q1[h1] );
if( h2 <= t2 ) res[i] = std :: min( res[i], f[q2[h2]] + b + q2[h2] - i );
}
f.swap( res );
}
void dfs( const int &u ) {
if( son[u].empty() ) {
ans[u] = 0;
dp[u].resize( 1 ), dp[u][0] = 0;
} else if( ( int ) son[u].size() == 1 ) {
int v = son[u][0];
dfs( v );
lag[u][0] = lag[v][0];
lag[u][1] = lag[v][1];
dp[u].swap( dp[v] ), ans[u] = ans[v];
} else {
for( const int &v : son[u] ) dfs( v );
std :: sort( son[u].begin(), son[u].end(),
[] ( const int &a, const int &b ) -> bool {
return dp[a].size() + lag[a][0] + lag[a][1] <
dp[b].size() + lag[b][0] + lag[b][1];
} );
int s = son[u].size(), v = son[u][0];
int lim = dp[v].size() + lag[v][0] + lag[v][1] - 1;
for( int i = 0 ; i < s ; i ++ ) {
int w = son[u][i];
normalise( dp[w], lim, lag[w][0], lag[w][1] );
}
dp[u].resize( lim + 1 ), ans[u] = 1e9;
for( int i = 0 ; i <= lim ; i ++ ) {
dp[u][i] = 0;
for( int j = 0 ; j < s ; j ++ )
dp[u][i] += dp[son[u][j]][i];
ans[u] = std :: min( ans[u], dp[u][i] );
}
}
lag[u][typ[u] - '0'] ++;
for( const int &v : son[u] )
std :: vector<int> ().swap( dp[v] );
}
void solve() {
scanf( "%d", &n ), init();
scanf( "%s", typ + 1 );
for( int i = 2 ; i <= n ; i ++ ) {
int fa; scanf( "%d", &fa );
son[fa].push_back( i );
}
dfs( 1 );
for( int i = 1 ; i <= n ; i ++ )
printf( "%d%c", ans[i], " \n"[i == n] );
}
int main() {
int t;
scanf( "%d", &t );
while( t -- ) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10140kb
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: 182ms
memory: 26900kb
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:
wrong answer 302nd lines differ - expected: '5 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '5 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'