QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270294#7875. Queue Sortingucup-team206#RE 0ms0kbC++172.7kb2023-11-30 18:19:382023-11-30 18:19:38

Judging History

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

  • [2023-11-30 18:19:38]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-11-30 18:19:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 10;
const int S = N * 40;
namespace Seg {
    int mx[S], mxi[S], tg[S], c[S][2], tot;
    void SetAdd(int v, int val) {
        mxi[v] += val;
        mx[v] += val; 
        tg[v] += val;
    }
    inline void PushDown(int v) {
        if (tg[v]) {
            if (c[v][0]) SetAdd(c[v][0], tg[v]);
            if (c[v][1]) SetAdd(c[v][1], tg[v]);
            tg[v] = 0;
        }
    }
    inline void PushUp(int v) {
        mx[v] = max(mx[c[v][0]], mx[c[v][1]]);
        mxi[v] = max(mxi[c[v][0]], mxi[c[v][1]]);
    }
    inline int Ask(int v, int w, int l = 1, int r = 1e9) {
        if (!v) return -1e15;
        if (l == r) return mxi[v];
        int mid = (l + r) >> 1;
        PushDown(v);
        if (w <= mid) return max(Ask(c[v][0], w, l, mid), mx[c[v][1]] + w);
        else return max(mxi[c[v][0]], Ask(c[v][1], w, mid + 1, r));
    }
    inline void Insert(int &v, int w, int val, int l = 1, int r = 1e9) {
        if (!v) v = ++tot;
        if (l == r) {
            mx[v] = val;
            mxi[v] = val + l;
            return;
        }
        PushDown(v);
        int mid = (l + r) >> 1;
        if (w <= mid) Insert(c[v][0], w, val, l, mid);
        else Insert(c[v][1], w, val, mid + 1, r);
        PushUp(v);
    }
    inline void Merge(int &v, int v2, int &upd, int l = 1, int r = 1e9) {
        if (!v2) return;
        if (!v) { v = v2; return; }
        if (l == r) {
            mx[v] = max(mx[v], mx[v2]);
            mxi[v] = max(mxi[v], mxi[v2]);
            return;
        }
        PushDown(v); PushDown(v2);
        int mid = (l + r) >> 1;
        upd = max(upd, mxi[c[v][0]] + mx[c[v2][1]]);
        upd = max(upd, mxi[c[v2][0]] + mx[c[v][1]]);
        Merge(c[v][0], c[v2][0], upd, l, mid);
        Merge(c[v][1], c[v2][1], upd, mid + 1, r);
        PushUp(v);
    }
}
int n, w[N], f[N];
int rt[N];
vector<int> g[N];
inline void Dfs(int x, int fa) {
    int sum_son = 0;
    f[x] = 0;
    int val = -1e15;
    for (auto y : g[x]) if (y != fa) {
        Dfs(y, x);
        sum_son += f[y];
        f[x] = max(f[x], sum_son + Seg::Ask(rt[y], w[x]));
        Seg::Merge(rt[x], rt[y], val);
    }
    f[x] = max(f[x], val + sum_son);
    f[x] = max(f[x], sum_son);
    Seg::Insert(rt[x], w[x], 0);
    Seg::SetAdd(rt[x], sum_son - f[x]);
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> w[i];
    for (int i = 1, x, y; i < n; ++i) {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    Seg::mxi[0] = Seg::mx[0] = -1e15;
    Dfs(1, 0);
    cout << f[1] << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

4
1 1 1 1

output:


result: