QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#396187#8518. Roars IIIzhoukangyang#WA 5ms29632kbC++142.7kb2024-04-22 14:46:352024-04-22 14:46:35

Judging History

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

  • [2024-04-22 14:46:35]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:29632kb
  • [2024-04-22 14:46:35]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
#define pb emplace_back
using namespace std;
#define i128 __int128
using namespace std; 
const int N = 2e5 + 7, mod = 1e9 + 7;
const int M = 2.4e7;
int cnt, w[M], tag[M], d[M], ch[M][2];
void adt(int &x, int val) {
	if(!x)return ;
	int nw = ++cnt;
	tag[nw] = tag[x] + val;
	w[nw] = w[x] + val;
	d[nw] = d[x];
	x = nw;
}
void push(int x) {
	adt(ch[x][0], tag[x]), adt(ch[x][1], tag[x]), tag[x] = 0;
}
int merge(int x, int y) {
	if(!x || !y) return x | y;
	int nw = ++cnt;
	if(w[x] < w[y] || (w[x] == w[y] && x < y)) swap(x, y);
	push(x), tag[nw] = tag[x], ch[nw][0] = ch[x][0], ch[nw][1] = merge(ch[x][1], y), w[nw] = w[x];
	if(d[ch[nw][0]] < d[ch[nw][1]]) swap(ch[nw][0], ch[nw][1]);
	d[nw] = d[ch[nw][0]] + 1;
	return nw;
}

int n;
char s[N];
string S;
vi e[N];
struct Data {
	int p;
	ll ns;
	Data(int P = 0, ll NS = 0) {
		p = P, ns = NS;
	}
};
Data operator + (Data a, Data b) {
	return Data(merge(a.p, b.p), a.ns + b.ns);
}
Data Move(Data u) {
	adt(u.p, 1);
	return u;
}
Data Pop(Data u) {
	u.ns += w[u.p];
	push(u.p);
	u.p = merge(ch[u.p][0], ch[u.p][1]);
	return u;
}
inline int make() {
	++cnt, w[cnt] = 0;
	return cnt;
}
ll ans[N];
Data lhd[N], hd[N], up[N];
void dfs1(int x, int fa) {
	hd[x] = make();
	for(auto&v : e[x]) if(v != fa) dfs1(v, x), hd[x] = hd[x] + Move(hd[v]);
	lhd[x] = hd[x];
	if(s[x] == '0') hd[x] = Pop(hd[x]);
}
Data pref[N], suf[N];
void dfs2(int x, int fa) {
	auto u = up[x] + lhd[x];
	if(s[x] == '0')u = Pop(u);
	ans[x] = u.ns;
	vi son;
	for(auto&v : e[x]) if(v != fa) son.pb(v);
	if(!sz(son)) return;
	up[x] = up[x] + make();
	L(i, 0, sz(son))
		pref[i] = suf[i] = 0;
	L(i, 0, sz(son) - 2) {
		pref[i] = Move(hd[son[i]]);
		if(i)pref[i] = pref[i] + pref[i - 1];
	}
	R(i, sz(son) - 1, 1) {
		suf[i] = Move(hd[son[i]]);
		if(i < sz(son) - 1)suf[i] = suf[i] + suf[i + 1];
	}
	L(i, 0, sz(son) - 1) {
		up[son[i]] = up[x];
		if(i)up[son[i]] = up[son[i]] + pref[i - 1];
		if(i < sz(son) - 1)up[son[i]] = up[son[i]] + suf[i + 1];
		if(s[x] == '0') 
			up[son[i]] = Pop(up[son[i]]);
		up[son[i]] = Move(up[son[i]]);
	}
	L(i, 0, sz(son) - 1) {
		dfs2(son[i], x);
	}
}
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	cin >> S;
	L(i, 1, n) {
		s[i] = S[i - 1];
	}
	L(i, 1, n - 1) {
		int u, v;
		cin >> u >> v;
		e[u].pb(v), e[v].pb(u);
	}
	dfs1(1, 0);
	dfs2(1, 0);
	L(i, 1, n)
		cout << ans[i] << " \n"[i == n];
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 28452kb

input:

5
10101
1 2
2 3
2 4
4 5

output:

2 2 2 3 3

result:

ok 5 number(s): "2 2 2 3 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 28472kb

input:

1
0

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 28872kb

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 0ms
memory: 27780kb

input:

2
10
1 2

output:

0 1

result:

ok 2 number(s): "0 1"

Test #5:

score: 0
Accepted
time: 5ms
memory: 29632kb

input:

3
100
2 3
2 1

output:

0 1 2

result:

ok 3 number(s): "0 1 2"

Test #6:

score: 0
Accepted
time: 0ms
memory: 27840kb

input:

4
1100
4 1
4 3
4 2

output:

1 1 3 1

result:

ok 4 number(s): "1 1 3 1"

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 28320kb

input:

5
10100
4 5
1 3
2 1
3 5

output:

0 2 0 3 2

result:

wrong answer 4th numbers differ - expected: '4', found: '3'