QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#521909 | #7736. Red Black Tree | liqingyang# | WA | 135ms | 32904kb | C++17 | 1.9kb | 2024-08-16 16:35:34 | 2024-08-16 16:35:35 |
Judging History
answer
#include<iostream>
#include<vector>
using namespace std;
int T,n,fa[100010],g[100010],h[100010],ans[100010];
char s[100010];
vector<int> e[100010],f[100010];
void dfs(int now)
{
if(e[now].empty())
{
f[now].resize(1),f[now][0]=0;
g[now]=1,h[now]=s[now]-'0';
ans[now]=0;
return;
}
if(e[now].size()==1)
{
int t=e[now][0];
dfs(t);
swap(f[now],f[t]);
g[now]=g[t]+1;
h[now]=h[t]+s[now]-'0';
ans[now]=ans[t];
return;
}
int S=1e9;
for(int i=0;i<e[now].size();i++)
{
int t=e[now][i];
dfs(t);
S=min(S,int(f[t].size())+g[t]);
}
f[now].resize(S);
for(int j=0;j<S;f[now][j++]=0);
for(int i=0;i<e[now].size();i++)
{
int t=e[now][i];
static pair<int,int> q1[100010],q2[100010];
int l1=1,r1=0,l2=1,r2=0;
for(int j=0;j<S;j++)
{
int v=1e9;
if(j<f[t].size())
{
while(l1<=r1&&q1[r1].first>=f[t][j]+j)
{
r1--;
}
q1[++r1]={f[t][j]+j,j};
}
while(l1<=r1&&q1[l1].second<j-h[t])
{
l1++;
}
if(l1<=r1)
{
v=q1[l1].first-(j-h[t]);
}
if(j>h[t])
{
int J=j-h[t]-1;
if(J<f[t].size())
{
while(l2<=r2&&q2[r2].first>=f[t][J]+J)
{
r2--;
}
q2[++r2]={f[t][J]-J,J};
}
while(l2<=r2&&q2[l2].second<j-g[t])
{
l2++;
}
if(l2<=r2)
{
v=min(v,q2[l2].first+(j-h[t]));
}
}
f[now][j]+=v;
}
}
ans[now]=1e9;
for(int i=0;i<S;i++)
{
ans[now]=min(ans[now],f[now][i]);
}
g[now]=1,h[now]=s[now]-'0';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>T;
while(T--)
{
cin>>n>>s+1;
for(int i=1;i<=n;i++)
{
e[i].clear();
}
for(int i=2;i<=n;i++)
{
cin>>fa[i];
e[fa[i]].emplace_back(i);
}
dfs(1);
for(int i=1;i<=n;i++)
{
cout<<ans[i];
if(i<n)
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10216kb
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: 135ms
memory: 32904kb
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 689th lines differ - expected: '5 1 2 1 0 1 1 1 0 0 0 0 0 0 0 0 0', found: '6 1 2 1 0 1 1 1 0 0 0 0 0 0 0 0 0'