题目大意:
就是现在给出一个树, 有点权, 两种操作, 第一种修改某个点的点权
第二种询问某两点之间的路径上的点权和
大致思路:
树链剖分水题....
熟练剖分之后用线段树维护查询, 单点更新区间查询
代码如下:
Result : Accepted Memory : 4312 KB Time : 1004 ms
/*
* Author: Gatevin
* Created Time: 2015/9/8 18:55:23
* File Name: Sakura_Chiyo.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define maxn 30010
int top[maxn];
int grandson[maxn];
int dep[maxn];
int siz[maxn];
int belong[maxn];
int father[maxn];
int Q[maxn];
int cnt;
int hson[maxn];
int n;
struct Edge
{
int u, v, nex;
Edge(int _u, int _v, int _nex)
{
u = _u, v = _v, nex = _nex;
}
Edge(){}
};
Edge edge[maxn << 1];
int tot;
int head[maxn];
void add_Edge(int x, int y)
{
edge[++tot] = Edge(x, y, head[x]);
head[x] = tot;
}
bool vis[maxn];
int id[maxn];
int antiID[maxn];
void split()
{
cnt = 0;
int l = 0, r = 1;
dep[Q[r] = 1] = 1;
father[r] = -1;
while(l < r)
{
int x = Q[++l];
if(head[x] == -1) continue;
for(int j = head[x]; j + 1; j = edge[j].nex)
{
int y = edge[j].v;
if(y == father[x]) continue;
dep[Q[++r] = y] = dep[x] + 1;
father[y] = x;
}
}
for(int i = n; i; i--)
{
int x = Q[i], p = -1;
siz[x] = 1;
if(head[x] == -1) continue;
for(int j = head[x]; j + 1; j = edge[j].nex)
{
int y = edge[j].v;
if(y == father[x]) continue;
siz[x] += siz[y];
if(p == -1 || (p > 0 && siz[y] > siz[p]))
p = y;
}
if(p == -1)
{
hson[x] = -1;
grandson[++cnt] = x;
belong[top[cnt] = x] = cnt;
}
else
{
hson[x] = p;
belong[x] = belong[p];
top[belong[x]] = x;
}
}
int idx = 0;
memset(vis, 0, sizeof(vis));
for(int i = n; i; i--)
{
int x = Q[i];
if(vis[x]) continue;
vis[x] = 1;
id[x] = ++idx;
antiID[idx] = x;
while(father[x] != -1 && belong[father[x]] == belong[x] && !vis[father[x]])
{
x = father[x];
id[x] = ++idx;
antiID[idx] = x;
vis[x] = 1;
}
}
return;
}
int w[maxn];
struct Segment_Tree
{
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
int sum[maxn << 2];
void pushUp(int rt)
{
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
return;
}
void build(int l, int r, int rt)
{
if(l == r)
{
sum[rt] = w[antiID[l]];
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushUp(rt);
}
void update(int l, int r, int rt, int pos, int value)
{
if(l == r)
{
sum[rt] = value;
return;
}
int mid = (l + r) >> 1;
if(mid >= pos) update(lson, pos, value);
else update(rson, pos, value);
pushUp(rt);
}
int query(int l, int r, int rt, int L, int R)
{
if(l >= L && r <= R)
return sum[rt];
int mid = (l + r) >> 1;
int ret = 0;
if(mid >= L) ret += query(lson, L, R);
if(mid + 1 <= R) ret += query(rson, L, R);
return ret;
}
};
Segment_Tree st;
int answer(int x, int y)
{
int ans = 0;
while(top[belong[x]] != top[belong[y]])
{
if(dep[top[belong[x]]] < dep[top[belong[y]]])
swap(x, y);
ans += st.query(1, n, 1, id[x], id[top[belong[x]]]);
x = father[top[belong[x]]];
}
if(dep[x] < dep[y]) swap(x, y);
ans += st.query(1, n, 1, id[x], id[y]);
return ans;
}
int main()
{
int T;
scanf("%d", &T);
for(int cas = 1; cas <= T; cas++)
{
printf("Case %d:\n", cas);
memset(head, -1, sizeof(head));
tot = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", w + i);
int u, v, w;
for(int i = 1; i < n; i++)
{
scanf("%d %d", &u, &v);
u++, v++;
add_Edge(u, v);
add_Edge(v, u);
}
split();
st.build(1, n, 1);
int p, op;
scanf("%d", &p);
while(p--)
{
scanf("%d", &op);
switch(op)
{
case 1: scanf("%d %d", &u, &w);
st.update(1, n, 1, id[++u], w);
break;
case 0: scanf("%d %d", &u, &v);
printf("%d\n", answer(++u, ++v));
break;
}
}
}
return 0;
}