QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#659333#8938. Crawling on a TreeHunsterRE 1ms4068kbC++202.3kb2024-10-19 19:41:012024-10-19 19:41:02

Judging History

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

  • [2024-10-19 19:41:02]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:4068kb
  • [2024-10-19 19:41:01]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;

constexpr int inf32 = (int)(1e9) + 9;
constexpr LL inf64 = (LL)(1e18) + 18;

constexpr int N = 10004;

int n, m, c[N];
std::vector<std::tuple<int, int, int>> tree[N];
struct Vec2 : std::pair<int, LL> { using std::pair<int, LL>::pair; };
Vec2 operator+(Vec2 a, Vec2 b) { return {a.first + b.first, a.second + b.second}; }
Vec2 operator-(Vec2 a, Vec2 b) { return {a.first - b.first, a.second - b.second}; }
__int128 cross(Vec2 a, Vec2 b) { return __int128(a.first) * b.second - __int128(a.second) * b.first; }
std::vector<Vec2> h[N];
std::vector<Vec2> merge(std::vector<Vec2> a, std::vector<Vec2> b) {
    if (!a.size() || !b.size()) return {};
    std::vector<Vec2> res = {a[0] + b[0]};
    for (int i = 1, j = 1; i < a.size() || j < b.size(); ) {
        if (i == a.size()) {
            res.push_back(res.back() + b[j] - b[j - 1]);
            j++;
            continue;
        }
        if (j == b.size()) {
            res.push_back(res.back() + a[i] - a[i - 1]);
            i++;
            continue;
        }
        auto x = a[i] - a[i - 1], y = b[j] - b[j - 1];
        if (cross(x, y) > 0) {
            res.push_back(res.back() + a[i] - a[i - 1]);
            i++;
        }
        else {
            res.push_back(res.back() + b[j] - b[j - 1]);
            j++;
        }
    }
    return res;
}

void dfs(int u, int p) {
    int t = 0;
    h[u].resize(m + 1);
    for (int i = 0; i <= m; i++) h[u][i] = {i, 0};
    for (auto [v, l, k] : tree[u]) {
        if (v == p) continue;
        dfs(v, u);
        std::vector<Vec2> _h;
        for (auto [x, y] : h[v]) {
            int w = 2 * std::max(c[v], x) - x;
            if (w <= k) _h.push_back({x, y + 1ll * l * w});
        }
        std::swap(h[u], _h);
        h[u] = merge(h[u], h[v]);
        c[u] = std::max(c[u], c[v]);
    }
}

LL ans[N];

int main() {
    std::cin >> n >> m;
    for (int i = 1; i < n; i++) {
        int x, y, l, k;
        std::cin >> x >> y >> l >> k;
        tree[x].push_back({y, l, k});
        tree[y].push_back({x, l, k});
    }
    for (int i = 2; i <= n; i++) std::cin >> c[i];
    dfs(1, 0);
    std::fill_n(ans, m + 1, -1);
    for (auto [x, y] : h[1]) if (x >= c[1]) ans[x] = y;
    for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3656kb

input:

4 2
1 2 3 2
2 3 2 1
2 4 5 1
1 1 1

output:

-1
13

result:

ok 2 number(s): "-1 13"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3944kb

input:

4 2
1 2 3 2
2 3 2 1
2 4 5 1
2 2 2

output:

-1
-1

result:

ok 2 number(s): "-1 -1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3768kb

input:

2 1
2 1 1 1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3708kb

input:

2 50
2 1 1 1
50

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok 50 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 4068kb

input:

2 50
2 1 1 50
50

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
50

result:

ok 50 numbers

Test #6:

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

input:

2 50
1 2 1 100000
50

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
50

result:

ok 50 numbers

Test #7:

score: -100
Runtime Error

input:

50 1
1 2 85524 58896
2 3 9137 9819
3 4 3036 88987
4 5 78909 15766
5 6 76067 34996
6 7 64247 63701
7 8 14 9384
8 9 37698 35418
9 10 51427 91691
10 11 39818 89351
11 12 47887 64083
12 13 43836 44135
13 14 22561 83803
14 15 52617 97413
15 16 41869 83810
16 17 35783 18642
17 18 5514 34601
18 19 50448 49...

output:


result: