标签:
1 6 5 0 1 1 2 0 3 3 4 5 3 7 -5 100 20 -5 -7 1 1 1 3 0 2 -1 1 1 1 5
Case #1: 102 27 2 20
线段树+dfs序
1 //2016.8.11 2 #pragma comment(linker, "/STACK:1024000000, 1024000000") 3 #include<iostream> 4 #include<cstdio> 5 #include<vector> 6 #include<algorithm> 7 #define N 100005 8 #define lson (id<<1) 9 #define rson ((id<<1)|1) 10 #define mid ((l+r)>>1) 11 12 using namespace std; 13 14 struct node 15 { 16 long long max, lazy; 17 }tree[N*5]; 18 19 vector<int> v[N]; 20 long long w[N], val[N];//val[i]表示从根节点走到i节点的价值和 21 int in[N], out[N], flag;//in记录查询区间的左端,out记录查询区间的右端 22 23 24 //线段树模板 25 //************************************************************************************ 26 void push_up(int id) 27 { 28 tree[id].max = max(tree[lson].max, tree[rson].max); 29 return; 30 } 31 32 void build_tree(int id, int l, int r) 33 { 34 if(l == r) 35 { 36 tree[id].max = val[l]; 37 tree[id].lazy = 0; 38 return ; 39 } 40 build_tree(lson, l, mid); 41 build_tree(rson, mid+1, r); 42 push_up(id); 43 tree[id].lazy = 0; 44 return ; 45 } 46 47 void push_down(int id, int l, int r) 48 { 49 if(tree[id].lazy) 50 { 51 tree[lson].lazy += tree[id].lazy; 52 tree[rson].lazy += tree[id].lazy; 53 tree[lson].max += tree[id].lazy; 54 tree[rson].max += tree[id].lazy; 55 tree[id].lazy = 0; 56 } 57 return ; 58 } 59 60 void ins(int id, int l, int r, int ql, int qr, int tt) 61 { 62 if(ql<=l&&r<=qr) 63 { 64 tree[id].max += tt; 65 tree[id].lazy += tt; 66 return; 67 } 68 if(tree[id].lazy) push_down(id, l, r); 69 if(ql<=mid) 70 ins(lson, l, mid, ql, qr, tt); 71 if(mid+1<=qr) 72 ins(rson, mid+1, r, ql, qr, tt); 73 push_up(id); 74 return; 75 } 76 77 long long query(int id, int l, int r, int ql, int qr) 78 { 79 if(ql<=l && r<=qr) 80 { 81 return tree[id].max; 82 } 83 if(tree[id].lazy) 84 push_down(id, l, r); 85 long long maxnum = -9999999999999; 86 if(ql<=mid) maxnum = max(maxnum, query(lson, l, mid, ql, qr)); 87 if(mid+1<=qr) maxnum = max(maxnum, query(rson, mid+1, r, ql, qr)); 88 return maxnum; 89 } 90 //************************************************************************************ 91 92 void dfs(int a, int fa, long long wight) 93 { 94 in[a] = ++flag; 95 for(int i = 0; i < v[a].size(); i++) 96 { 97 int b = v[a][i]; 98 if(b==fa)continue; 99 dfs(b, a, w[b]+wight); 100 } 101 out[a] = flag; 102 val[in[a]] = wight; 103 } 104 105 int main() 106 { 107 int T, n, m, a, b, cmd; 108 cin>>T; 109 for(int kase = 1; kase <= T; kase++) 110 { 111 printf("Case #%d:\n", kase); 112 cin>>n>>m; 113 for(int i = 0; i < N; i++) 114 v[i].clear(); 115 for(int i = 0; i < n-1; i++) 116 { 117 scanf("%d%d", &a, &b); 118 a++; b++; 119 v[a].push_back(b); 120 v[b].push_back(a); 121 } 122 for(int i = 1 ; i <= n; i++) 123 scanf("%lld", &w[i]); 124 flag = 0; 125 dfs(1, 0, w[1]); 126 for(int i = 1; i <= n; i++) 127 cout<<in[i]<<" "<<out[i]<<endl; 128 build_tree(1, 1, n); 129 while(m--) 130 { 131 scanf("%d", &cmd); 132 if(cmd == 0) 133 { 134 scanf("%d%d", &a, &b);a++; 135 ins(1, 1, n, in[a], out[a], b-w[a]); 136 w[a] = b; 137 }else 138 { 139 scanf("%d", &a); a++; 140 long long ans = query(1, 1, n, in[a], out[a]); 141 cout<<ans<<endl; 142 } 143 } 144 } 145 146 return 0; 147 }
标签:
原文地址:http://www.cnblogs.com/Penn000/p/5763114.html