QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#680766#7736. Red Black TreeYoralenWA 1ms5808kbC++141.5kb2024-10-26 22:36:582024-10-26 22:36:58

Judging History

你现在查看的是最新测评结果

  • [2024-10-26 22:36:58]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5808kb
  • [2024-10-26 22:36:58]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100005;
struct edge{int to,next;}w[N<<1];
int n,h[N],cnt,T,a[N],ar[N];
char s[N];
void Add(int x,int y){
    w[++cnt].to=y,w[cnt].next=h[x],h[x]=cnt;
}
int hd[N],ng[N],ans[N],sv[N];
struct Dlt{
	vector<int>ng,ps;
	int no=0,sg=0;
	void Init(){
		ng.clear();ps.clear();
		no=sg=0;
	}
	int val(int k){
		if(k<=ng.size())return ng[k-1];
		else if(k<=ng.size()+no)return 0;
		else return ps[ps.size()-(k-ng.size()-no)];
	}
	void Ins(int v){
		if(v<0)ng.push_back(v),sg+=v;
		if(v==0)no++;
		if(v>0)ps.push_back(v);
	}
	int size(){
		return ng.size()+no+ps.size();
	}
};
Dlt DFS(int u){
    int i,j,ln;hd[u]=a[u];
    Dlt S,F;S.Init();
    for(i=h[u];i;i=w[i].next){
        int e=w[i].to;
        F=DFS(e);hd[u]+=hd[e];
        if(!S.size())S=move(F),ln=F.size();
        else{
        	ln=min(ln,F.size());
        	for(j=1;j<=ln;j++)ar[j]=S.val(j)+F.val(j);
        	S.Init();int pl=1,pr=ln;
        	while(ar[pl]<0&&pl<=ln)S.Ins(ar[pl]),pl++;
        	while(ar[pr]>0&&pr>=1)S.Ins(ar[pr]),pr--;
        	S.no=pr-pl+1;
        }
    }
    if(a[u]==0)S.Ins(1);else S.Ins(-1);
    ans[u]=hd[u]+S.sg;return move(S);
}
int main(){
    int i,x;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);scanf("%s",s+1);cnt=0;
        for(i=1;i<=n;i++)a[i]=s[i]-'0',h[i]=ng[i]=ans[i]=hd[i]=0;
        for(i=2;i<=n;i++)scanf("%d",&x),Add(x,i);DFS(1);
        for(i=1;i<=n;i++)printf("%d ",ans[i]);putchar('\n');
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5808kb

input:

2
9
101011110
1 1 3 3 3 6 2 2
4
1011
1 1 3

output:

5 1 3 0 0 0 0 0 0 
2 0 0 0 

result:

wrong answer 1st lines differ - expected: '4 1 2 0 0 0 0 0 0', found: '5 1 3 0 0 0 0 0 0 '