首页 > 代码库 > Codeforces 384E 线段树+dfs序

Codeforces 384E 线段树+dfs序

题目链接:点击打开链接

题意:

给定n个点,m个询问的无向树(1为根)

下面n个数表示每个点的权值

下面n-1行给出树

操作1:x点权值+v, x的第 i & 1 的儿子-v, 第 !(i&1) 的儿子+v

操作2:询问x点权值

dfs把树转成序列

根据深度把点分成2组

分别用线段树维护。。

然后Y一下

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
#define ll __int64
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define N 201000
ll n ,m;
inline ll Mid(ll a,ll b){return (a+b)>>1;}
struct Edge{
    ll from, to, nex;
}edge[N<<1];
ll head[N], edgenum;
void add(ll u,ll v){
    Edge E = {u,v,head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
ll in[N], out[N], fa[N], Time, dep[N], V[N];
void dfs(ll u, ll father, ll deep){
    fa[u] = father;
    dep[u] = deep;
    in[u] = ++Time;

    for(ll i = head[u];~i; i = edge[i].nex){
        ll v = edge[i].to;      if(v==father)continue;
        dfs(v,u,deep+1);
    }
    out[u] = Time;
}
struct node{
    struct E{
        ll l, r, val, lazy;
    }t[N<<2];
    void push_down(ll id){
        if(t[id].l==t[id].r || t[id].lazy == 0)return ;
        t[L(id)].val += t[id].lazy;
        t[R(id)].val += t[id].lazy;
        t[L(id)].lazy+=t[id].lazy;
        t[R(id)].lazy+=t[id].lazy;
        t[id].lazy = 0;
    }
    void build(ll l, ll r, ll id){
        t[id].l = l; t[id].r = r;
        t[id].val = 0;
        t[id].lazy = 0;
        if(l==r)return;
        ll mid = Mid(l,r);
        build(l,mid,L(id));build(mid+1,r,R(id));
    }
    void update(ll l, ll r,ll val,ll id){
        push_down(id);
        if(l == t[id].l && t[id].r == r) { t[id].val += val; t[id].lazy = val; return ;}
        
        ll mid = Mid(t[id].l, t[id].r);
        if(mid<l)update(l,r,val,R(id));
        else if(r<=mid)update(l,r,val,L(id));
        else {
            update(l,mid,val,L(id));
            update(mid+1,r,val,R(id));
        }
    }
    ll query(ll l, ll r, ll id){
        push_down(id);
        if(l == t[id].l && t[id].r == r)return t[id].val;
        
        ll mid = Mid(t[id].l, t[id].r);
        if(mid<l)return query(l,r,R(id));
        else if(r<=mid)return query(l,r,L(id));
        return query(l,mid,L(id))+query(mid+1,r,R(id));
    }
}tree[2];
ll query(ll u){
    ll ans = tree[dep[u]&1].query(in[u],in[u],1);
    return ans;
}
void init(){Time = 0; memset(head, -1, sizeof head); edgenum = 0;}
int main(){  
    ll i, j, u, v;
    while(cin>>n>>m) {
        init();
        for(i=1;i<=n;i++)cin>>V[i];
        for(i=1;i<n;i++){
            cin>>u>>v;
            add(u,v);
            add(v,u);
        }
        dfs(1,-1,1);
        tree[0].build(1,n,1);   
        tree[1].build(1,n,1);
        while(m--){
            cin>>u;
            if(u==1) {
                cin>>u>>v;
                tree[dep[u]&1].update(in[u],out[u],v,1);
                if(in[u]<out[u])
                    tree[!(dep[u]&1)].update(in[u]+1,out[u],-v,1);
            }
            else {
                cin>>u;
                cout<<query(u)+V[u]<<endl;
            }
        }
    }
    return 0;  
}