QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#269807#7736. Red Black TreePetroTarnavskyiWA 194ms40568kbC++202.6kb2023-11-30 01:43:482023-11-30 01:43:48

Judging History

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

  • [2024-02-19 10:14:05]
  • hack成功,自动添加数据
  • (/hack/538)
  • [2023-11-30 01:43:48]
  • 评测
  • 测评结果:WA
  • 用时:194ms
  • 内存:40568kb
  • [2023-11-30 01:43:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 17;

string s;
VI g[N];
VI dp[N];
int c0[N], c1[N];
int ans[N];

VI transform(VI a, int to)
{
	VI b(SZ(a) + c0[to] + c1[to], N);
	deque<PII> dq1, dq2;
	FOR(k, 0, SZ(b))
	{
		int i = k - c1[to];
		if (i >= 0 && i < SZ(a))
		{
			int val = a[i] - i;
			while (!dq1.empty() && dq1.back().S >= val)
				dq1.pop_back();
			dq1.PB({i, val});
		}
		if (k < SZ(a))
		{
			int val = a[k] + k;
			while (!dq2.empty() && dq2.back().S >= val)
				dq2.pop_back();
			dq2.PB({k, val});
		}
		if (!dq1.empty())
			b[k] = min(b[k], k - c1[to] + dq1.front().S);
		if (!dq2.empty())
			b[k] = min(b[k], c1[to] - k + dq2.front().S);
		if (!dq1.empty() && k - c1[to] - c0[to] == dq1.front().F)
			dq1.pop_front();
		if (!dq2.empty() && k - c1[to] == dq2.front().F)
			dq2.pop_front();
	}
	return b;
}

int dfs(int v)
{
	if (g[v].empty())
	{
		c0[v] = s[v] == '0';
		c1[v] = s[v] == '1';
		dp[v] = {0};
		ans[v] = 0;
		return v;
	}
	if (SZ(g[v]) == 1)
	{
		int to = g[v][0];
		int j = dfs(to);
		c0[v] = c0[to] + (s[v] == '0');
		c1[v] = c1[to] + (s[v] == '1');
		ans[v] = ans[to];
		return j;
	}
	int m = N;
	VI indices;
	for (int to : g[v])
	{
		indices.PB(dfs(to));
		m = min(m, SZ(dp[indices.back()]) + c0[to] + c1[to]);
	}
	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		int j = indices[i];
		while (SZ(dp[j]) > 1 && SZ(dp[j]) + c0[to] + c1[to] > m)
			dp[j].pop_back();
		dp[j] = transform(dp[j], to);
	}
	VI dpSons(m);
	for (int j : indices)
		FOR(k, 0, min(m, SZ(dp[j])))
			dpSons[k] += dp[j][k];
	dp[v].resize(m + 1, N);
	FOR(k, 0, m + 1)
	{
		if (k < m)
			dp[v][k] = dpSons[k] + (s[v] == '1');
		if (k > 0)
			dp[v][k] = min(dp[v][k], dpSons[k - 1] + (s[v] == '0'));
	}
	ans[v] = *min_element(ALL(dp[v]));
	return v;
}

void solve()
{
	int n;
	cin >> n >> s;
	FOR(i, 1, n)
	{
		int p;
		cin >> p;
		p--;
		g[p].PB(i);
	}
	dfs(0);
	FOR(i, 0, n)
	{
		if (i > 0)
			cout << " ";
		cout << ans[i];
	}
	cout << "\n";
	FOR(i, 0, n)
	{
		g[i].clear();
		dp[i].clear();
		c0[i] = c1[i] = 0;
	}
}

int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);	
	int t;
	cin >> 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: 11164kb

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: 194ms
memory: 40568kb

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 35th lines differ - expected: '3 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '9 8 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0'