#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 30005;
struct node{
int v, nxt;
}e[N << 2];
int tot, cnt, n, q;
int head[N];
int sz[N];
int son[N];
int pre[N];
int top[N];
int pos[N];
int dep[N];
int w[N];
void init()
{
cnt = tot = 0;
sz[0] = dep[1] = 0;
memset( head, -1, sizeof( head ) );
}
void add( int u, int v )
{
e[cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void dfs( int u )
{
sz[u] = 1;
son[u] = 0;
for( int i = head[u]; ~i; i = e[i].nxt )
{
int v = e[i].v;
if( v == pre[u] )
continue;
dep[v] = dep[u] + 1;
pre[v] = u;
dfs( v );
sz[u] += sz[v];
if( sz[v] > sz[son[u]] )
son[u] = v;
}
}
void rebuild( int now, int anc )
{
pos[now] = ++tot;
top[now] = anc;
if( son[now] )
rebuild( son[now], anc );
for( int i = head[now]; ~i; i = e[i].nxt )
{
int v = e[i].v;
if( v != son[now] && v != pre[now] )
rebuild( v, v );
}
}
struct seg{
int l, r;
int maxx, sum;
}tr[N << 2];
void pushup( int rt )
{
tr[rt].sum = tr[rt<<1].sum + tr[rt<<1|1].sum;
tr[rt].maxx = max( tr[rt<<1].maxx, tr[rt<<1|1].maxx );
}
void build( int l, int r, int rt )
{
tr[rt].l = l;
tr[rt].r = r;
if( l == r )
{
tr[rt].maxx = tr[rt].sum = w[l];
return;
}
int mid = ( l + r ) >> 1;
build( lson );
build( rson );
pushup( rt );
}
void update( int pos, int v, int rt )
{
if( tr[rt].l == tr[rt].r && tr[rt].l == pos )
{
tr[rt].sum = tr[rt].maxx = v;
return;
}
int mid = ( tr[rt].l + tr[rt].r ) >> 1;
if( pos <= mid )
update( pos, v, rt << 1 );
else
update( pos, v, rt << 1 | 1 );
pushup( rt );
}
int get_sum( int l, int r, int rt )
{
if( l <= tr[rt].l && tr[rt].r <= r )
{
return tr[rt].sum;
}
int mid = ( tr[rt].l + tr[rt].r ) >> 1;
if( r <= mid )
return get_sum( l, r, rt << 1 );
else if( l > mid )
return get_sum( l, r, rt << 1 | 1 );
else
return ( get_sum( lson ) + get_sum( rson ) );
}
int get_max( int l, int r, int rt )
{
if( l <= tr[rt].l && tr[rt].r <= r )
return tr[rt].maxx;
int mid = ( tr[rt].l + tr[rt].r ) >> 1;
if( r <= mid )
return get_max( l, r, rt << 1 );
else if( l > mid )
return get_max( l, r, rt << 1 | 1 );
else
return max( get_max( lson ), get_max( rson ) );
}
int query_sum( int x, int y )
{
int res = 0;
while( top[x] != top[y] )
{
int f1 = top[x], f2 = top[y];
if( dep[f1] > dep[f2] )
{
res += get_sum( pos[f1], pos[x], 1 );
x = pre[f1];
}
else
{
res += get_sum( pos[f2], pos[y], 1 );
y = pre[f2];
}
}
if( dep[x] < dep[y] )
return res + get_sum( pos[x], pos[y], 1 );
else
return res + get_sum( pos[y], pos[x], 1 );
}
int query_max( int x, int y )
{
int res = -inf;
while( top[x] != top[y] )
{
int f1 = top[x], f2 = top[y];
if( dep[f1] > dep[f2] )
{
res = max( res, get_max( pos[f1], pos[x], 1) );
x = pre[f1];
}
else
{
res = max( res, get_max( pos[f2], pos[y], 1 ) );
y = pre[f2];
}
}
if( dep[x] < dep[y] )
return max( res, get_max( pos[x], pos[y], 1 ) );
else
return max( res, get_max( pos[y], pos[x], 1 ) );
}
int main()
{
scanf("%d", &n);
init();
int u, v;
for( int i = 1; i < n; ++i )
{
scanf("%d%d", &u, &v);
add( u, v );
add( v, u );
}
dfs( 1 );
rebuild( 1, 1 );
for( int i = 1; i <= n; ++i )
{
int x;
scanf("%d", &x);
w[pos[i]] = x;
}
build( 1, n, 1 );
scanf("%d", &q);
while( q-- )
{
char op[10];
scanf("%s %d%d", op, &u, &v);
if( op[1] == 'H' )
update( pos[u], v, 1 );
else if( op[1] == 'S' )
printf("%d\n", query_sum( u, v ) );
else
printf("%d\n", query_max( u, v ) );
}
}
bzoj 1036 [ZJOI2008]树的统计Count 树链剖分点权果题
最新推荐文章于 2018-12-16 12:42:01 发布