QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#504661#9110. Zayin and TreeLavender_Field#AC ✓385ms26740kbC++202.4kb2024-08-04 14:35:342024-08-04 14:35:34

Judging History

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

  • [2024-08-04 14:35:34]
  • 评测
  • 测评结果:AC
  • 用时:385ms
  • 内存:26740kb
  • [2024-08-04 14:35:34]
  • 提交

answer

#include <algorithm>
#include <bit>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>
void upd_min(int &a, int b) { if (a > b) a = b; }
constexpr int N = 1E6;
constexpr int inf = std::numeric_limits<int>::max();
int a[N + 1];
int DFN[N + 1];
int size[N + 1];
int b[N + 1];
constexpr int N_ = 2 * std::bit_ceil(static_cast<unsigned>(N));
int seg[N_], tag[N_];
int g_l, g_r;
int g_v;
void build(int u, int l, int r)
{
    tag[u] = 0;
    if (l == r - 1)
    {
        seg[u] = b[l];
        return;
    }
    int m = (l + r) / 2;
    build(u << 1, l, m);
    build(u << 1 | 1, m, r);
    seg[u] = std::min(seg[u << 1], seg[u << 1 | 1]);
}
void modify(int u, int l, int r)
{
    if (g_l <= l && r <= g_r)
    {
        seg[u] += g_v;
        tag[u] += g_v;
        return;
    }
    int m = (l + r) / 2;
    if (g_l < m)
        modify(u << 1, l, m);
    if (m < g_r)
        modify(u << 1 | 1, m, r);
    seg[u] = std::min(seg[u << 1], seg[u << 1 | 1]) + tag[u];
}
void solve()
{
    int n; std::cin >> n;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];
    std::vector<std::vector<int>> e(n + 1);
    auto add_edge = [&e](int u, int v) {
        e[u].push_back(v);
    };
    for (int i = n; --i; )
    {
        int ui, vi; std::cin >> ui >> vi;
        add_edge(ui, vi);
        add_edge(vi, ui);
    }
    int cnt = 0;
    std::function<void (int, int, int)> DFS = [&](int u, int t, int d) {
        ++cnt;
        DFN[u] = cnt;
        size[u] = 1;
        b[cnt] = d - a[u];
        for (int v : e[u])
            if (v != t)
                DFS(v, u, d + 1), size[u] += size[v];
    };
    DFS(1, 0, 1);
    build(1, 1, n + 1);
    int ans = inf;
    auto modify = [&](int l, int r, int v) {
        g_l = l, g_r = r, g_v = v;
        ::modify(1, 1, n + 1);
    };
    std::function<void (int, int)> DFS_ = [&](int u, int t) {
        upd_min(ans, a[u] + seg[1]);
        for (int v : e[u])
            if (v != t)
            {
                modify(1, n + 1, 1);
                modify(DFN[v], DFN[v] + size[v], -2);
                DFS_(v, u);
                modify(1, n + 1, -1);
                modify(DFN[v], DFN[v] + size[v], 2);
            }
    };
    DFS_(1, 0);
    std::cout << ans << "\n";
}
int main()
{
    std::ios_base::sync_with_stdio(false), std::cin.tie(nullptr);
    int T; std::cin >> T;
    while (T--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 385ms
memory: 26740kb

input:

3009
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
10
5 8 1 0 8 7 5 2 0 4
2 4
3 8
3 9
1 2
1 3
3 6
4 5
5 7
6 10
10
6 8 8 4 8 0 6 6 0 2
7 10
1 7
2 9
2 3
3 4
1 5
1 6
6 8
1 2
10
9 0 4 0 4 6 0 2 0 0
1 5
1 3
1 7
2 6
1 2
1 9
1 4
5 8
7 10
10
8 8 1 2 7 4 8 6 0 8
1 6
1 7
1 5
7 9
1 3
1 2
2 10
3 4
1 8...

output:

0
-1
-6
-6
-7
-6
-7
-4
-3
-7
-5
-6
-5
-4
-6
-3
-4
-7
-4
-4
-6
-6
-6
-5
-4
-5
-6
-6
-7
-7
-5
-7
-6
-6
-7
-6
-5
-5
-4
-6
-6
-5
-6
-6
-6
-6
-3
-6
-3
-6
-4
-6
-7
-6
-7
-6
-6
-5
-7
-6
-4
-7
-3
-5
-5
-6
-4
-5
-7
-6
-5
-5
-4
-3
-5
-3
-4
-2
-6
-5
-7
-4
-5
-5
-7
-7
-4
-6
-5
-4
-6
-5
-5
-6
-3
-6
-7
-7
-7
-6
-...

result:

ok 3009 lines