QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#461515 | #8518. Roars III | zhaohaikun | WA | 2ms | 15928kb | C++20 | 5.9kb | 2024-07-02 19:47:28 | 2024-07-02 19:47:29 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define debug cerr << "\33[32m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
const int N = 2e5 + 10;
int n, dep[N], fa[N], sz[N], rt[N], tt[N], son[N], o;
ll a1[N], a2[N], aw[N];
string st;
vector <int> v[N];
const int M = N * 25;
int ls[M], rs[M], info[M], tot, mx[N];
void pushup(int num) {
info[num] = info[ls[num]] + info[rs[num]];
}
int merge(int num1, int num2, int l, int r) {
if (!num1 || !num2) return num1 ^ num2;
int num = ++tot;
if (l == r) {
info[num] = info[num1] + info[num2];
return num;
}
ls[num] = merge(ls[num1], ls[num2], l, mid);
rs[num] = merge(rs[num1], rs[num2], mid + 1, r);
pushup(num);
return num;
}
int findback(int num, int l, int r) {
if (l == r) return l;
if (info[rs[num]]) return findback(rs[num], mid + 1, r);
return findback(ls[num], l, mid);
}
int find2(int num1, int num2, int l, int r) {
if (l == r) return l;
if (info[rs[num1]] ^ info[rs[num2]]) return find2(info[rs[num1]], info[rs[num2]], mid + 1, r);
return find2(info[ls[num1]], info[ls[num2]], l, mid);
}
int modify(int lastnum, int l, int r, int x, int y) {
int num = ++tot;
info[num] = info[lastnum] + y;
if (l == r) return num;
if (mid >= x) rs[num] = rs[lastnum], ls[num] = modify(ls[lastnum], l, mid, x, y);
else ls[num] = ls[lastnum], rs[num] = modify(rs[lastnum], mid + 1, r, x, y);
return num;
}
int query(int num, int l, int r, int x) {
if (l == r) return info[num];
if (mid >= x) return query(ls[num], l, mid, x);
return query(rs[num], mid + 1, r, x);
}
void dfs(int x, int fa) {
sz[x] = 1;
dep[x] = dep[fa] + 1;
for (int i: v[x])
if (i != fa) {
dfs(i, x);
aw[x] += aw[i];
a1[x] += a2[i];
sz[x] += sz[i];
if (sz[i] > sz[son[x]]) son[x] = i;
rt[x] = merge(rt[x], rt[i], 1, n);
}
tt[x] = rt[x];
aw[x] += info[rt[x]];
a1[x] += info[rt[x]];
a2[x] = a1[x];
// debug << x << "! " << st[x] << endl;
if (st[x] == '1') {
// debug << info[rt[x]] << endl;
rt[x] = modify(rt[x], 1, n, dep[x], 1);
// debug << info[rt[x]] << endl;
mx[x] = findback(rt[x], 1, n);
} else {
if (info[rt[x]]) {
mx[x] = findback(rt[x], 1, n);
rt[x] = modify(rt[x], 1, n, mx[x], - 1);
rt[x] = modify(rt[x], 1, n, dep[x], 1);
a2[x] -= mx[x] - dep[x];
}
}
// debug << x << " " << a1[x] << " " << a2[x] << " " << info[rt[x]] << endl;
}
vector <int> g[N];
int cur[N * 2], be, ed;
ll sum, aa[N];
void bot(int num, int l, int r, int id) {
if (!info[num]) return;
if (l == r) {
g[id].push_back(info[num]);
return;
}
bot(ls[num], l, mid, id), bot(rs[num], mid + 1, r, id);
}
vector <int> gg;
void dfs2(int x, int fa) {
// aa[x] = sum;
for (int i: v[x])
if (i != fa && i != son[x]) bot(rt[i], 1, n, i);
for (int i: v[x])
if (i != fa) {
aw[i] = aw[x] - info[rt[i]] + (o - info[rt[i]]);
// debug << i << " " << be << " " << ed << " " << son[x] << endl;
// F(i, be, ed) cout << cur[i] << " "; debug << endl;
int wbe = be;
ll wsum = sum;
if (i == son[x]) {
for (int j: v[x])
if (j != fa && j != i) {
F(k, 0, SZ(g[j])) {
sum += k + 1;
cur[ed - (k + 1)] += g[j][k];
}
chkmin(be, ed - SZ(g[j]));
}
} else {
// debug << info[tt[x]] << " " << info[rt[i]] << endl;
if (info[tt[x]] != info[rt[i]]) {
gg.push_back(x);
F(k, 0, SZ(g[i])) {
sum -= k + 1;
cur[ed - (k + 1)] -= g[i][k];
}
sum += a1[x];
chkmin(be, ed - (find2(rt[x], rt[i], 1, n) - dep[x]));
}
}
if (st[x] == '1') cur[ed]++, chkmin(be, ed);
else {
if (be < ed) {
// debug << be << " " << cur[be] << endl;
int ss = --cur[be];
sum -= ed - be;
for (int j: gg) {
int dis = dep[j] - dep[x];
// debug << "@ " << dis << endl;
if (dis <= ed - be) {
int w = dep[j] + ed - be - dis;
ss += query(rt[j], 1, n, w);
}
}
// debug << gg.size() << endl;
// debug << i << endl;
assert(ss >= 0);
if (!ss) be++;
cur[ed]++;
}
}
// debug << sum << endl;
sum += o - info[rt[i]];
// debug << i << " " << info[rt[i]] << endl;
ed++;
aa[i] = sum + a1[i];
if (st[i] == '0') aa[i] -= max(ed - be, mx[i] - dep[i]);
// if (i == 5) {
// debug << sum << " " << a1[i] << " " << max(ed - be, mx[i] - dep[i]) << endl;
// }
// debug << i << " " << aa[i] << endl;
// debug << i << " " << aa[i] << " " << sum << " " << a1[i] << endl;
// debug << "! " << i << endl;
dfs2(i, x);
// debug << "! " << i << endl;
ed--;
be = wbe;
sum = wsum;
if (i == son[x]) {
for (int j: v[x])
if (j != fa && j != i)
F(k, 0, SZ(g[j])) cur[ed - (k + 1)] -= g[j][k];
} else {
if (info[rt[x]] != info[rt[i]]) {
gg.pop_back();
F(k, 0, SZ(g[i])) cur[ed - (k + 1)] += g[i][k];
}
}
if (st[x] == '1') cur[ed]--;
else if (be < ed) {
cur[be]++;
cur[ed]--;
}
// debug << i << endl;
// F(i, be, ed) cout << cur[i] << " "; debug << endl;
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0); // don't use puts
cin >> n >> st;
o = count(all(st), '1');
st = ' ' + st;
F(i, 1, n - 1) {
int x, y; cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
aa[1] = a2[1];
// debug << 1 << " " << aa[1] << endl;
be = 2 * n, ed = n;
dfs2(1, 0);
// F(i, 1, n) debug << aw[i] << endl;
F(i, 1, n) cout << aw[i] - aa[i] << ' ';
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15928kb
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: 13996kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 13868kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 15840kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 2ms
memory: 13808kb
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: 13752kb
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: 2ms
memory: 11816kb
input:
5 10100 4 5 1 3 2 1 3 5
output:
0 1 0 4 2
result:
wrong answer 2nd numbers differ - expected: '2', found: '1'