QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#347792#7736. Red Black Treehl666TL 1ms6168kbC++171.5kb2024-03-09 15:27:472024-03-09 15:27:47

Judging History

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

  • [2024-03-09 15:27:47]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:6168kb
  • [2024-03-09 15:27:47]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#define RI register int
#define CI const int&
using namespace std;
const int N=100005;
int t,n,x,ans[N]; char s[N]; vector <int> v[N];
struct delta
{
	vector <int> pos,neg; int zero,sum;
	inline delta(void)
	{
		pos.clear(); neg.clear(); zero=sum=0;
	}
	inline delta(vector <int>& vec)
	{
		zero=sum=0; pos.clear(); neg.clear();
		for (auto x:vec) if (x<0) neg.push_back(x),sum+=x;
		else if (x==0) ++zero; else pos.push_back(x);
		reverse(pos.begin(),pos.end());
	}
	inline int size(void)
	{
		return neg.size()+zero+pos.size();
	}
	inline int at(CI x)
	{
		if (x<neg.size()) return neg[x]; else
		if (x<neg.size()+zero) return 0; else
		return pos[pos.size()-1-(x-neg.size()-zero)];
	}
	inline void insert(CI x)
	{
		if (x==-1) neg.push_back(x),--sum; else pos.push_back(x);
	}
};
inline pair <int,delta> DFS(CI now=1)
{
	int st=0; delta d;
	for (auto to:v[now])
	{
		auto [x,vec]=DFS(to); st+=x;
		if (d.size()==0) d=vec; else
		{
			int lim=min(d.size(),vec.size());
			vector <int> tmp(lim);
			for (RI i=0;i<lim;++i) tmp[i]=d.at(i)+vec.at(i);
			d=delta(tmp);
		}
	}
	st+=s[now]-'0'; d.insert(s[now]=='0'?1:-1);
	ans[now]=st+d.sum; return make_pair(st,d);
}
int main()
{
	for (scanf("%d",&t);t;--t)
	{
		RI i; for (scanf("%d%s",&n,s+1),i=2;i<=n;++i)
		scanf("%d",&x),v[x].push_back(i);
		for (DFS(),i=1;i<=n;++i) printf("%d%c",ans[i]," \n"[i==n]),v[i].clear();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6168kb

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
Time Limit Exceeded

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: