QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#332149 | #7736. Red Black Tree | bobbilyking | WA | 244ms | 55940kb | C++20 | 3.3kb | 2024-02-19 10:39:08 | 2024-02-19 10:39:08 |
Judging History
answer
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
#define NN 100010
ll s[NN];
ll ans[NN];
vl adj[NN];
struct info {
ll c[2] = {}; // reds, blacks
vl dp; // dp vector, dp[i] = minimum amount to get dp[i]
};
info dfs(ll i) {
if (!adj[i].size()) {
ans[i] = 0;
info ret;
ret.dp.assign(2, 0);
ret.dp[!s[i]] = 1;
return ret;
} else if (adj[i].size() == 1) {
ll only = adj[i][0];
auto ret = move(dfs(only));
ans[i] = ans[only];
ret.c[s[i]]++;
return ret;
}
vector<info> children;
ll dep = 1e9;
for (auto x: adj[i]) {
children.push_back(dfs(x));
auto &bck = children.back();
dep = min(dep, bck.c[0] + bck.c[1] + ssize(bck.dp));
}
info here;
here.dp.resize(dep);
for (auto &[c, dp]: children) {
vl ndp(dep, 1e9);
reverse(A(dp));
F(i, 0, c[1]) dp.push_back(1e9);
reverse(A(dp));
vector<vl> events(dp.size()); multiset<ll> s;
F(i, 0, ssize(dp)) {
s.insert(dp[i] - i);
for (auto x: events[i]) s.erase(s.find(x));
events[i].clear();
ll nxt = i + c[0] + 1;
if (nxt < events.size()) events[nxt].push_back(dp[i] - i);
assert(s.size());
if (i < dep) ndp[i] = min(ndp[i], *s.begin() + i);
}
s.clear();
FD(i, ssize(dp)-1, -1) {
s.insert(dp[i] + i);
for (auto x: events[i]) s.erase(s.find(x));
events[i].clear();
ll nxt = i - c[1] - 1;
if (nxt >= 0) events[nxt].push_back(dp[i] + i);
assert(s.size());
if (i < dep) ndp[i] = min(ndp[i], *s.begin() - i);
}
F(i, 0, dep) here.dp[i] += ndp[i];
}
here.dp.push_back(1e9); // cost of making everything black has to be included here
// simulate shifting up/down depending on red/black
{
vl move[2] {here.dp, here.dp};
move[1].insert(move[1].begin(), 1e9); // shift everything up by one
for (auto &x: move[!s[i]]) ++x;
F(i, 0, ssize(here.dp)) here.dp[i] = min(move[0][i], move[1][i]);
}
ans[i] = *min_element(A(here.dp));
return here;
}
int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(20);
G(t) while(t--) {
G(n)
GS(str)
F(i, 0, n) s[i] = str[i] - '0';
F(i, 0, n) adj[i].clear();
F(i, 1, n) {
G(x) adj[x-1].push_back(i);
}
dfs(0);
F(i, 0, n) cout << ans[i] << " "; cout << '\n';
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5636kb
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: 244ms
memory: 55940kb
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...
result:
wrong answer 112th lines differ - expected: '7 4 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '9 4 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '