QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562456#7736. Red Black TreeReanap#TL 0ms11124kbC++142.6kb2024-09-13 17:46:182024-09-13 17:46:18

Judging History

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

  • [2024-09-13 17:46:18]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:11124kb
  • [2024-09-13 17:46:18]
  • 提交

answer

#include <bits/stdc++.h>
#define pii pair <int , int>
#define pll pair <LL , LL>
#define mp make_pair
#define fs first
#define sc second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

//const int Mxdt=100000;
//static char buf[Mxdt],*p1=buf,*p2=buf;
//#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxdt,stdin),p1==p2)?EOF:*p1++;

template <typename T>
void read(T &x) {
	T f=1;x=0;char s=getchar();
	while(s<'0'||s>'9') {if(s=='-') f=-1;s=getchar();}
	while(s>='0'&&s<='9') {x=(x<<3)+(x<<1)+(s^'0');s=getchar();}
	x *= f;
}

template <typename T>
void write(T x , char s='\n') {
	if(!x) {putchar('0');putchar(s);return;}
	if(x<0) {putchar('-');x=-x;}
	T t = 0 , tmp[25] = {};
	while(x) tmp[t ++] = x % 10 , x /= 10;
	while(t -- > 0) putchar(tmp[t] + '0');
	putchar(s);
}

const int MAXN = 1e5 + 5;

int n;
char s[MAXN];
int lson[MAXN] , dis[MAXN] , Ans[MAXN];
vector <int> G[MAXN]; 

void dfs1(int x) {
	dis[x] = 1e9;
	for (auto v:G[x]) {
		dfs1(v);
		dis[x] = min(dis[v] + 1 , dis[x]);
		if(dis[v] > dis[lson[x]]) lson[x] = v;
	}
	if(!G[x].size()) dis[x] = 1;
}

int val[MAXN];
multiset <int> S[MAXN];

void dfs(int x) {
	if(!G[x].size()) {
//		printf("%d %c\n",x,s[x]);
		if(s[x] == '1') val[x] = 1 , S[x].insert(-1) , Ans[x] = 0;
		else val[x] = 0 , S[x].insert(1) , Ans[x] = 0;
		return;
	}
	if((int)G[x].size() == 1) {
		dfs(lson[x]);
		swap(S[x] , S[lson[x]]);
		if(s[x] == '0') {
			val[x] = val[lson[x]];
			S[x].insert(1);
		}
		else {
			val[x] = val[lson[x]] + 1;
			S[x].insert(-1);
		}
		Ans[x] = Ans[lson[x]];
		return;
	}
	dfs(lson[x]);
	swap(S[x] , S[lson[x]]);
	val[x] = val[lson[x]];
	for (auto v:G[x]) {
		if(v == lson[x]) continue;
		dfs(v);
		val[x] += val[v];
		multiset <int> tmp;
		while(S[v].size() && S[x].size()) {
			auto l = S[v].begin();
			auto r = S[x].begin();
			tmp.insert((*l) + (*r));
			S[v].erase(l) , S[x].erase(r);
		}
		swap(S[x] , tmp);
	}
	if(s[x] == '0') {
		S[x].insert(1);
	}
	else {
		val[x] ++;
		S[x].insert(-1);
	}
	
	int cur = val[x];Ans[x] = val[x];
//	printf("%d: %d",x,val[x]);
	for (auto v:S[x]) {
		cur += (v);
//		printf(" %d",v);
		Ans[x] = min(Ans[x] , cur);
	}
//	puts("");
}

void solve() {
	read(n);
	scanf("%s" , s + 1);
	for (int i = 2; i <= n; ++i) {
		int p;read(p);
		G[p].push_back(i);
	}
	
	dfs1(1);
	dfs(1);
	
	for (int i = 1; i <= n; ++i) write(Ans[i] , ' ');
	puts("");
	
	for (int i = 1; i <= n; ++i) val[i] = Ans[i] = 0 , G[i].clear() , S[i].clear();
	
}

int main() {
	
	int T;read(T);
	while(T -- > 0) solve();
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 11124kb

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 3 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 
0 0 0 
1 1 1 0 0 0 0 
1 0 0 0 0 0 0 
3 2 0 0 0 0 0 0 
4 0 0 2 1 0 0 0 0 0 0 
6 5 0 0 4 0 0 0 0 0 0 0 0 0 0 
4 2 2 0 0 0 0 0 0 0 
10 6 3 3 0 0 0 0 0 0 0 1 0 0 0 0 0 
1 1 0 0 0 
8 2 1 1 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 ...

result: