题意:给你一棵树,点带权,支持三种操作:单点修改;询问链上和;询问链上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. 源自人脑的神奇算法 -- 读《How to make your own neural network》有感

    最近读到了一本很好的关于机器学习-深度学习的书值得推荐下并特意做了这个学习总结. 为什么推荐 在我认为好书(计算机类)的评判有几个标准: 试图以通俗的语言阐述,并在引入任何新概念的时候都讲述来龙去脉, ...

  2. ES6 中 Array.from() 不能得到的数组元素是 undefined 或是空数组

    本文地址:http://www.cnblogs.com/veinyin/p/7944072.html  正确格式 let json = { '0': 'Waaaa~', '1': 'Hello,', ...

  3. 【洛谷 P4291】 [HAOI2008]排名系统(Splay,Trie)

    题目链接 不是双倍经验我会去\(debug\)一上午? 一开始我是用的\(map+string\),跑的太慢了,T了4个点. 后来我手写了\(string\),重载了小于号,依然用的\(map\),T ...

  4. 【总结】前端必须收藏的CSS3动效库!!!

    现在的网站和App的设计中越来越重视用户体验,而优秀的动效则能使你的应用更具交互性,从而吸引更多用户的使用. 如果你对CSS3中定义动效还不熟练,或希望采用更加简单直接的方式在你的应用中引入动效的话, ...

  5. 47、求1+2+3+...+n

    一.题目 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 二.解法 public class Solut ...

  6. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  7. Tslib触摸屏官网【转】

    转自:https://github.com/kergoth/tslib C library for filtering touchscreen events tslib consists of the ...

  8. javascript反混淆之packed混淆(一)

    javascript反混淆之packed混淆(一) 什么是JavaScript反混淆,在理解这个概念前我们先来看下什么是代码混淆,代码混淆,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理 ...

  9. Django项目上传到AWS服务器上

    EC2是亚马逊(Amazon.com)提供的弹性云计算服务:Apache是一个跨平台的Web服务器端软件,可以使Python.PHP.Perl等语言编写的程序运行在服务器上:Django是一个Web程 ...

  10. 用于启动 Windows Phone 8 内置应用的 URI 方案

    本主题列出了可用于启动内置应用的 URI 方案.许多内置于 Windows Phone 的应用,都可以通过调用 LaunchUriAsync(Uri) 和传入一个使用与要启动应用相关的方案的 URI, ...