【lct】bzoj1036 [ZJOI2008]树的统计Count
题意:给你一棵树,点带权,支持三种操作:单点修改;询问链上和;询问链上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的更多相关文章
- [BZOJ1036][ZJOI2008]树的统计Count 解题报告|树链剖分
树链剖分 简单来说就是数据结构在树上的应用.常用的为线段树splay等.(可现在splay还不会敲囧) 重链剖分: 将树上的边分成轻链和重链. 重边为每个节点到它子树最大的儿子的边,其余为轻边. 设( ...
- bzoj1036 [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12646 Solved: 5085 [Subm ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
- bzoj千题计划124:bzoj1036: [ZJOI2008]树的统计Count
http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分板子题 #include<cstdio> #include<iost ...
- [BZOJ1036] [ZJOI2008] 树的统计Count (LCT)
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- bzoj1036 [ZJOI2008]树的统计Count——LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1036 LCT水题! 然而没有1A(咬牙)! 注意值有负数,所以取 max 的话要把作为“哨兵 ...
- BZOJ1036 [ZJOI2008]树的统计Count 树链剖分
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1036 题意概括 一个树,每个节点有一个权值.3种操作. 1:修改某一个节点的权值. 2:询问某两个 ...
- BZOJ1036[ZJOI2008]树的统计Count 题解
题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.有一些操作:1.把结点u的权值改为t:2.询问从点u到点v的路径上的节点的最大权值 3.询问从点u到点v的路径上的节点的权值和 ...
- bzoj1036 zjoi2008 树的统计 count
填坑= =第一道裸树剖 #include<cstdio> #include<algorithm> #include<cstring> #include<cst ...
随机推荐
- 2017ACM暑期多校联合训练 - Team 2 1001 HDU 6045 Is Derek lying? (模拟)
题目链接 Problem Description Derek and Alfia are good friends.Derek is Chinese,and Alfia is Austrian.Thi ...
- python mysql参数化查询防sql注入
一.写法 cursor.execute('insert into user (name,password) value (?,?)',(name,password)) 或者 cursor.execut ...
- Hive ORC表的使用
创建普通临时表: create table if not exists test_orc_tmp( name string, gender string, cnt BIGINT )row ...
- Java从零到企业级电商项目实战
欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享各种Java学习资源,面试题,优质文章,以及企业级Java实战项目回复关键字免费领取)回复关键字:"电商项 ...
- inet_addr_onlink
/* 根据指定设备的ip配置块,判断地址a,b是否在同一子网 */ /* --邻居项要求,在同一子网中的两个设备, 至少有一个接口有相同的子网配置, --也就是说对端的in_dev->ifa_l ...
- openjudge-NOI 2.6-2000 最长公共子上升序列
题目链接:http://noi.openjudge.cn/ch0206/2000/ 题解: 裸题,不解释(题目有毒) #include<cstdio> #include<algori ...
- 002利用zabbix监控某个目录大小
近期,因为JMS的消息堆积导致ApacheMQ频率故障(消息没有被消费掉,导致其数据库达到1.2G,JMS此时直接挂掉),很是郁闷!刚好自 己在研究zabbix.既然zabbix如此强大,那么它可以监 ...
- 在ubuntu 上安装pycharm
1.首先在官网下载pycharm并进行提取,将提取的文件夹放在/usr下面(或者任意位置) 2.然后vi /etc/hosts 编辑 将0.0.0.0 account.jetbrains.com添加到 ...
- u-boot引导内核过程
目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...
- Redis安装和客户端cli常见操作
安装Redis $ wget http://download.redis.io/releases/redis-4.0.6.tar.gz $ tar xzf redis-4.0.6.tar.gz $ c ...