标签:
先附上题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> using namespace std; typedef long long LL; const int maxn = 100000 + 100; int n, m; vector<int> tree[maxn]; int node_weight[maxn]; LL num[maxn]; int l[maxn], r[maxn], nlr; struct Segment { int l, r; LL x; LL lazy; }seg[3*maxn]; void build(int rt, int l, int r) { seg[rt].l = l; seg[rt].r = r; if(l == r) { seg[rt].x = num[l]; seg[rt].lazy = 0; return ; } build(2*rt, l, (l+r)/2); build(2*rt+1, (l+r)/2+1, r); seg[rt].x = max(seg[2*rt].x, seg[2*rt+1].x); seg[rt].lazy = 0; } void push_down(int rt) { if(seg[rt].lazy != 0){ int chl = 2*rt; seg[chl].x += seg[rt].lazy; seg[chl].lazy += seg[rt].lazy; int chr = 2*rt + 1; seg[chr].x += seg[rt].lazy; seg[chr].lazy += seg[rt].lazy; seg[rt].lazy = 0; } } void push_up(int rt) { seg[rt].x = max(seg[2*rt].x, seg[2*rt+1].x); } LL query(int rt, int l, int r) { //查询区间l - r的最大值 if(seg[rt].l == l && seg[rt].r == r) { return seg[rt].x; } push_down(rt); int mid = (seg[rt].l + seg[rt].r)/2; if(r <= mid) return query(2*rt, l, r); else if(l > mid) return query(2*rt+1, l, r); else { LL v1 = query(2*rt, l, mid); LL v2 = query(2*rt+1, mid+1, r); return max(v1, v2); } } void update(int rt, int l, int r, int c) { // 区间l-r增加C if(seg[rt].l == l && seg[rt].r == r) { seg[rt].x += c; seg[rt].lazy += c; return ; } push_down(rt); int mid = (seg[rt].l + seg[rt].r)/2; if(r <= mid) update(2*rt, l, r, c); else if(l > mid) update(2*rt+1, l, r, c); else { update(2*rt, l, mid, c); update(2*rt+1, mid+1, r, c); } push_up(rt); } void dfs(int pre, int u, int &k, LL sum) { l[u] = 2*maxn; int son = 0; sum += node_weight[u]; for(int i=0; i<tree[u].size(); i++) { int v = tree[u][i]; if(v == pre) continue; son++; dfs(u, v, k, sum); l[u] = min(l[u], l[v]); } num[k] = sum; r[u] = k++; // printf("%d ", u); //////////////////////////// if(son == 0) l[u] = r[u]; } int main() { int T; scanf("%d", &T); int kase = 0; while(T--) { scanf("%d%d", &n, &m); for(int i=0; i<n; i++) tree[i].clear(); for(int i=0; i<n-1; i++) { int u, v; scanf("%d%d", &u, &v); tree[u].push_back(v); tree[v].push_back(u); } for(int i=0; i<n; i++) scanf("%d", &node_weight[i]); nlr = 0; dfs(-1, 0, nlr, 0); // printf("\n"); ////////////////////////////////// // for(int i=0; i<n; i++) // printf("l[%d] = %d, r[%d] = %d\n", i, l[i], i, r[i]); build(1, 0, nlr-1); printf("Case #%d:\n", ++kase); for(int i=0; i<m; i++) { int op; scanf("%d", &op); if(op == 0) { int x, y; scanf("%d%d", &x, &y); update(1, l[x], r[x], y-node_weight[x]); node_weight[x] = y; }else{ int x; scanf("%d", &x); LL res = query(1, l[x], r[x]); cout<<res<<endl; } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/xingxing1024/p/5550529.html