QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#129807 | #6742. Leaves | UndertrainedOverpressure# | WA | 3ms | 5512kb | C++23 | 4.8kb | 2023-07-23 00:52:20 | 2023-07-23 00:52:21 |
Judging History
answer
#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
const int M = 507;
const int N = 1007;
int dp[M][M];
int ndp[M][M];
int buff[M];
int main() {
#ifdef ONPC
freopen("input", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> l(n, -1), r(n, -1);
vector<int> a(n, -1);
for (int i = 0; i < n; ++i) {
int t;
cin >> t;
if (t == 1) {
cin >> l[i] >> r[i];
--l[i], --r[i];
} else {
cin >> a[i];
}
}
vector<int> sz(n);
vector<int> h(n);
vector<vector<int>> layers(n + 1);
function<void(int)> dfs = [&](int u) {
layers[h[u]].push_back(u);
if (a[u] != -1) {
sz[u] = 1;
}
for (int v : {l[u], r[u]}) {
if (v != -1) {
h[v] = h[u] + 1;
dfs(v);
sz[u] += sz[v];
}
}
};
dfs(0);
vector<int> pos(n, -1);
for (int i = 0; i <= n; ++i) {
for (int j = 0; j < (int)layers[i].size(); ++j) {
pos[layers[i][j]] = j;
}
}
vector<vector<int>> pref(n + 1);
for (int i = n; i >= 0; --i) {
if (!layers[i].empty()) {
pref[i].reserve(layers[i].size() + 1);
pref[i].push_back(0);
for (int x : layers[i]) {
pref[i].push_back(pref[i].back() + sz[x]);
}
}
}
int cl = n;
while (layers[cl].empty()) {
--cl;
}
const int Inf = 1e9;
for (int i = 0; i <= m; ++i) {
fill(dp[i], dp[i] + pref[cl].back() + 1, Inf);
}
for (int i = 0; i < layers[cl].size(); ++i) {
dp[0][pref[cl][i]] = a[layers[cl][i]];
}
for (int lx = cl - 1; lx >= 0; --lx) {
for (int i = 0; i <= m; ++i) {
fill(ndp[i], ndp[i] + pref[lx].back() + 1, Inf);
}
for (int i = 0; i < layers[lx].size(); ++i) {
int v = layers[lx][i];
if (a[v] != -1) {
ndp[0][pref[lx][i]] = a[v];
} else {
int start_v = pref[lx][i];
int l_ch = l[v], r_ch = r[v];
int start_l = pref[lx + 1][pos[l_ch]];
int start_r = pref[lx + 1][pos[r_ch]];
auto min_eq = [&](int l_op, int r_op, int sw) {
int v_op = l_op + r_op + sw;
if (v_op > m) {
return;
}
if (!sw) {
for (int k = 0; k < sz[l_ch]; ++k) {
buff[k] = dp[l_op][start_l + k];
}
for (int k = 0; k < sz[r_ch]; ++k) {
buff[sz[l_ch] + k] = dp[r_op][start_r + k];
}
} else {
for (int k = 0; k < sz[r_ch]; ++k) {
buff[k] = dp[r_op][start_r + k];
}
for (int k = 0; k < sz[l_ch]; ++k) {
buff[sz[r_ch] + k] = dp[l_op][start_l + k];
}
}
bool is_better = true;
for (int k = 0; k < sz[l_ch] + sz[r_ch]; ++k) {
if (buff[k] > ndp[v_op][start_v + k]) {
is_better = false;
break;
}
if (buff[k] < ndp[v_op][start_v + k]) {
break;
}
}
if (is_better) {
for (int k = 0; k < sz[l_ch] + sz[r_ch]; ++k) {
ndp[v_op][start_v + k] = buff[k];
}
}
};
for (int go_l = 0; go_l <= min(m, sz[l_ch] + 1); ++go_l) {
for (int go_r = 0; go_r <= min(m, sz[r_ch] + 1); ++go_r) {
for (int sw = 0; sw <= 1; ++sw) {
min_eq(go_l, go_r, sw);
}
}
}
}
}
swap(dp, ndp);
}
for (int i = 0; i < sz[0]; ++i) {
cout << dp[m][i] << " ";
}
cout << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 5492kb
input:
3 0 1 2 3 2 1 2 2
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5512kb
input:
7 1 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
2 4 3 1
result:
ok 4 number(s): "2 4 3 1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 5468kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
1 3 4 2
result:
ok 4 number(s): "1 3 4 2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 5504kb
input:
3 1 1 2 3 2 1 2 2
output:
1 1000000000
result:
wrong answer 1st numbers differ - expected: '2', found: '1'