1 条题解

  • 1
    @ 2023-7-23 21:08:12
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 100010;
    const ll INF = 1e18;
    inline int LD(int o) { return o << 1; }
    inline int RD(int o) { return o << 1 | 1; }
    struct Node
    {
        int l, r;
        ll maxv, add;
    } t[N << 2];
    ll a[N];
    void pushup(Node &o, Node &ld, Node &rd)
    {
        o.maxv = max(ld.maxv, rd.maxv);
    }
    void pushdown(Node &o, Node &ld, Node &rd)
    {
        ld.add += o.add;
        ld.maxv += o.add;
        rd.add += o.add;
        rd.maxv += o.add;
        o.add = 0;
    }
    void build(int o, int l, int r)
    {
        t[o].l = l;
        t[o].r = r;
        t[o].add = 0;
        if (l == r)
        {
            t[o].maxv = a[l];
            return;
        }
        int mid = (l + r) >> 1;
        build(LD(o), l, mid);
        build(RD(o), mid + 1, r);
        pushup(t[o], t[LD(o)], t[RD(o)]);
    }
    void update(int o, int l, int r, ll v)
    {
        if (l <= t[o].l && t[o].r <= r)
        {
            t[o].add += v;
            t[o].maxv += v;
            return;
        }
        pushdown(t[o], t[LD(o)], t[RD(o)]);
        int mid = (t[o].l + t[o].r) >> 1;
        if (mid >= l)
            update(LD(o), l, r, v);
        if (mid < r)
            update(RD(o), l, r, v);
        pushup(t[o], t[LD(o)], t[RD(o)]);
    }
    ll query(int o, int l, int r)
    {
        if (l <= t[o].l && t[o].r <= r)
            return t[o].maxv;
        pushdown(t[o], t[LD(o)], t[RD(o)]);
        ll ans = -INF;
        int mid = (t[o].l + t[o].r) >> 1;
        if (mid >= l)
            ans = max(ans, query(LD(o), l, r));
        if (mid < r)
            ans = max(ans, query(RD(o), l, r));
        pushup(t[o], t[LD(o)], t[RD(o)]);
        return ans;
    }
    int T, n, m;
    struct point
    {
        int in,out;
    } p[100005];
    vector<vector<int> > vec(100005);
    ll orgnum[100005];
    ll pre[100005];
    int tim = 0;
    ll ss = 0;
    void dfs(int now, int fa)
    {
        tim++;
        p[now].in = tim;
        ss += orgnum[now];
        a[tim] = ss;
        for (int i = 0; i < vec[now].size(); i++)
    	{
            int to=vec[now][i];
            if(to==fa)
                continue;
            dfs(to,now);
        }
        ss-=orgnum[now];
        p[now].out=tim;
    }
    int main()
    {
        scanf("%d",&T);
        for (int tc = 1; tc <= T; tc++)
        {
            scanf("%d%d",&n,&m);
            for(int i=0;i<=n;i++){
                pre[i]=0;
                vec[i].clear();
            }
            for (int i = 1; i < n; i++)
            {
                int aa, bb;
                scanf("%d%d", &aa, &bb);
                vec[aa].push_back(bb);
                vec[bb].push_back(aa);
            }
            for(int i=0;i<n;i++){
                scanf("%lld",&orgnum[i]);
            }
            tim=0;
            ss=0;
            dfs(0,0);
            
            build(1, 1, n);
            printf("Case #%d:\n",tc);
            for (int i = 1; i <= m; i++)
            {
                int x,y;ll z;
                scanf("%d",&x);
                if (x == 1)
                {
                    scanf("%d",&y);
                    printf("%lld\n",query(1,p[y].in,p[y].out));
                }
                if (x == 0){
                    scanf("%d %lld",&y,&z);
                    ll d=z-orgnum[y];
                    orgnum[y]=z;
                    update(1,p[y].in,p[y].out,d);
                }
            }
        }
    }
    
    • 1

    信息

    ID
    316
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    (无)
    递交数
    63
    已通过
    24
    上传者