题意:给你一棵树,点带权,支持三种操作:单点修改;询问链上和;询问链上max。

这里的Query操作用了与上一题不太一样的做法(上一题用那种做法,因为在边带权的情况下换根太困难啦):

  先ChangeRoot(U),然后Access(V),再Splay(V),询问V在辅助树中的左子树。

因为Splay的pushdown用了极为暴力的做法……故而跑得比原来写的链剖线段树慢一倍。不过常数不在意了。

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 30005
int fa[maxn],c[maxn][2],siz[maxn];
bool is_root[maxn],delta[maxn];
int val[maxn],totalval[maxn],maxv[maxn];
void Mark(int x){
if(x){
delta[x]^=1;
}
}
void Maintain(int x)
{
siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
totalval[x]=totalval[c[x][0]]+totalval[c[x][1]]+val[x];
maxv[x]=val[x];
if(c[x][0]){
maxv[x]=max(maxv[x],maxv[c[x][0]]);
}
if(c[x][1]){
maxv[x]=max(maxv[x],maxv[c[x][1]]);
}
}
void pushdown(int x){
if(delta[x]){
Mark(c[x][0]);
Mark(c[x][1]);
swap(c[x][0],c[x][1]);
delta[x]=0;
}
}
void Rotate(int x,bool flag)
{
int y=fa[x];
c[y][!flag]=c[x][flag];
if(c[x][flag]){
fa[c[x][flag]]=y;
}
if(fa[y] && c[fa[y]][c[fa[y]][1]==y]==y){
c[fa[y]][c[fa[y]][1]==y]=x;
}
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
if(is_root[y]){
is_root[y]=0;
is_root[x]=1;
}
Maintain(y);
Maintain(x);
}
stack<int>st;
void Splay(int x)
{
pushdown(x);
if(!x || is_root[x]){
return;
}
int U=x;
while(!is_root[U]){
st.push(U);
U=fa[U];
}
st.push(U);
while(!st.empty()){
pushdown(st.top());
st.pop();
}
int y;
while(y=fa[x],(!is_root[x])){
if(is_root[y]){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
}
void Access(int x){
int y;
Splay(x);
while(fa[x]){
y=fa[x];
Splay(y);
Splay(x);
if(c[y][1]){
is_root[c[y][1]]=1;
}
is_root[x]=0;
c[y][1]=x;
Splay(x);
}
if(c[x][1]){
is_root[c[x][1]]=1;
c[x][1]=0;
}
}
void ChangeRoot(int x){
Access(x);
Splay(x);
Mark(x);
}
int QSum(int U,int V){
ChangeRoot(U);
Access(V);
Splay(V);
return val[V]+totalval[c[V][0]];
}
int QMax(int U,int V){
ChangeRoot(U);
Access(V);
Splay(V);
if(!c[V][0]){
return val[V];
}
else{
return max(val[V],maxv[c[V][0]]);
}
}
int n,m;
int v[maxn<<1],nex[maxn<<1],first[maxn],e;
void AddEdge(int U,int V){
v[++e]=V;
nex[e]=first[U];
first[U]=e;
}
int a[maxn];
void dfs(int U){
siz[U]=1;
is_root[U]=1;
val[U]=totalval[U]=maxv[U]=a[U];
for(int i=first[U];i;i=nex[i]){
if(!siz[v[i]]){
fa[v[i]]=U;
dfs(v[i]);
}
}
}
int main(){
// freopen("bzoj1036.in","r",stdin);
char op[10];
int x,y;
scanf("%d",&n);
for(int i=1;i<n;++i){
scanf("%d%d",&x,&y);
AddEdge(x,y);
AddEdge(y,x);
}
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
dfs(1);
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%s%d%d",op,&x,&y);
if(op[3]=='N'){
Splay(x);
val[x]=y;
Maintain(x);
}
else if(op[3]=='M'){
printf("%d\n",QSum(x,y));
}
else{
printf("%d\n",QMax(x,y));
}
}
return 0;
}

【lct】bzoj1036 [ZJOI2008]树的统计Count的更多相关文章

  1. [BZOJ1036][ZJOI2008]树的统计Count 解题报告|树链剖分

    树链剖分 简单来说就是数据结构在树上的应用.常用的为线段树splay等.(可现在splay还不会敲囧) 重链剖分: 将树上的边分成轻链和重链. 重边为每个节点到它子树最大的儿子的边,其余为轻边. 设( ...

  2. bzoj1036 [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 12646  Solved: 5085 [Subm ...

  3. bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...

  4. bzoj千题计划124:bzoj1036: [ZJOI2008]树的统计Count

    http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分板子题 #include<cstdio> #include<iost ...

  5. [BZOJ1036] [ZJOI2008] 树的统计Count (LCT)

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...

  6. bzoj1036 [ZJOI2008]树的统计Count——LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1036 LCT水题! 然而没有1A(咬牙)! 注意值有负数,所以取 max 的话要把作为“哨兵 ...

  7. BZOJ1036 [ZJOI2008]树的统计Count 树链剖分

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1036 题意概括 一个树,每个节点有一个权值.3种操作. 1:修改某一个节点的权值. 2:询问某两个 ...

  8. BZOJ1036[ZJOI2008]树的统计Count 题解

    题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.有一些操作:1.把结点u的权值改为t:2.询问从点u到点v的路径上的节点的最大权值 3.询问从点u到点v的路径上的节点的权值和 ...

  9. bzoj1036 zjoi2008 树的统计 count

    填坑= =第一道裸树剖 #include<cstdio> #include<algorithm> #include<cstring> #include<cst ...

随机推荐

  1. String类的一些常用操作方法

    package com.liveyc.framework.util; import java.io.UnsupportedEncodingException; import java.net.URLD ...

  2. centos下搭建YII环境

      第一步:安装lnmp wget -c http://soft.vpser.net/lnmp/lnmp1.4-full.tar.gz 下载完lnmp后,进行解压文档 tar zxf lnmp1.4- ...

  3. 浅谈桶排思想及[USACO08DEC]Patting Heads 题解

    一.桶排思想 1.通过构建n个空桶再将待排各个元素分配到每个桶.而此时有可能每个桶的元素数量不一样,可能会出现这样的情况:有的桶没有放任何元素,有的桶只有一个元素,有的桶不止一个元素可能会是2+: 2 ...

  4. 【洛谷 P3203】 [HNOI2010]弹飞绵羊(LCT)

    题目链接 把每个点和能跳到的点连边,于是就构成了一个森林. 查询操作就是该点到根的路径长度,修改操作就相当于删边再重新连边. 显然是\(LCT\)的强项. 查询时\(access(x),splay(x ...

  5. sqlmap的使用方法 ——时光凉春衫薄

    普通注入 Sqlmap -u “http://www.xxxxxx.com/xxxx/xxx/xxx.xxx?xx=xx” --dbs 找到一个sql的注入点 探测他的库名   access的直接探表 ...

  6. malloc原理和内存碎片【转】

    转自:http://www.cnblogs.com/zhaoyl/p/3820852.html 当一个进程发生缺页中断的时候,进程会陷入内核态,执行以下操作: 1.检查要访问的虚拟地址是否合法 2.查 ...

  7. 制作自动化系统安装U盘

    1.挂载CentOS6.6镜像 [root@test88 ~]# mkdir /application/tools -p [root@test88 ~]# cd /application/tools/ ...

  8. 移动端touch滑屏事件

    <script> var windowHeight = $(window).height(), $body = $("body");// console.log($(w ...

  9. swiper (Table切换和动态加载时候出现的问题)

    本文为让心灵-去旅行原创,转载请说明.. 我们在写一个简单的swiper图片轮播的时候很简单,是写死的也就那么几张图片轮播.如果这时候图片和一些东西是后台的,你从js里动态添加到DOM时,这时候你就会 ...

  10. ireport报表制作, 通过节点、产品类型来判断,当该节点审核通过之后,报表相对应的审核意见及签名 显示相对应的内容

    1.代码①  (与本内容相关的代码:7~36)           以下类似 $P{P_XXXX} :均为页面端的传入参数 select so.sale_order_no as sale_order_ ...