bzoj1036 [ZJOI2008]树的统计Count——LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1036
LCT水题!
然而没有1A(咬牙)!
注意值有负数,所以取 max 的话要把作为“哨兵”的 0号点 的 max 赋成很小的值才行。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=;
int n,m,fa[maxn],c[maxn][],rev[maxn],mx[maxn],sum[maxn],w[maxn],sta[maxn],a[maxn],b[maxn],top;
bool isroot(int x){return c[fa[x]][]!=x && c[fa[x]][]!=x;}
void pushup(int x)
{
int ls=c[x][],rs=c[x][];
sum[x]=sum[ls]+sum[rs]+w[x];
mx[x]=x;
if(w[mx[x]]<w[mx[ls]])mx[x]=mx[ls];
if(w[mx[x]]<w[mx[rs]])mx[x]=mx[rs];
}
void reverse(int x)
{
if(rev[x])
{
rev[c[x][]]^=; rev[c[x][]]^=; rev[x]=;
swap(c[x][],c[x][]);
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
void splay(int x)
{
sta[top=]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];
for(;top;top--)reverse(sta[top]);
for(;!isroot(x);rotate(x))
{
int y=fa[x],z=fa[y];
if(isroot(y))continue;
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
}
}
void access(int x)
{
for(int t=;x;splay(x),c[x][]=t,pushup(x),t=x,x=fa[x]);
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=;
}
void link(int x,int y)
{
makeroot(x); fa[x]=y;
}
void query(int x,int y)
{
makeroot(x); access(y); splay(y);
}
void cut(int x,int y)
{
query(x,y); fa[x]=; c[y][]=;
}
void change(int u,int t)
{
makeroot(u); w[u]=t; pushup(u);//makeroot
}
int main()
{
scanf("%d",&n); w[mx[]]=-;//!
for(int i=,x,y;i<n;i++)scanf("%d%d",&a[i],&b[i]);
for(int i=;i<=n;i++)scanf("%d",&w[i]);
for(int i=;i<n;i++)link(a[i],b[i]);
scanf("%d",&m);
char ch[];
for(int i=,x,y;i<=m;i++)
{
scanf("%s",&ch); scanf("%d%d",&x,&y);
if(ch[]=='C')change(x,y);
else
{
query(x,y);
if(ch[]=='M')printf("%d\n",w[mx[y]]);
if(ch[]=='S')printf("%d\n",sum[y]);
}
}
return ;
}
bzoj1036 [ZJOI2008]树的统计Count——LCT的更多相关文章
- [BZOJ1036] [ZJOI2008] 树的统计Count (LCT)
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- [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 ...
- 【lct】bzoj1036 [ZJOI2008]树的统计Count
题意:给你一棵树,点带权,支持三种操作:单点修改:询问链上和:询问链上max. 这里的Query操作用了与上一题不太一样的做法(上一题用那种做法,因为在边带权的情况下换根太困难啦): 先ChangeR ...
- 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 ...
随机推荐
- PHP编译参数configure配置详解(持续更新中)
编译参数-使用 ./configure -h在源代码目录中,该命令可以查看所有编译参数以及对应的英文解释 编译参数-说明 --prefix=/opt/php //指定 php 安装目录--with-a ...
- 杭电 5053 the Sum of Cube(求区间内的立方和)打表法
Description A range is given, the begin and the end are both integers. You should sum the cube of al ...
- CodeForcesGym 100524J Jingles of a String
Jingles of a String Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeFor ...
- NYOJ-769乘数密码,逆元解法;
乘数密码 时间限制:1000 ms | 内存限制:65535 KB 难度:1 -> Link <- 简单代替密码的第二种,比移位密码稍微复杂点,不过鉴于NYOJ,是完全可以 ...
- 1002. A+B for Polynomials (25) (浮点数判0)
This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...
- poj1984并查集的相对偏移
#include<stdio.h>//典型题 #include<math.h> #define N 40010 struct node { int x,y,z; }pre[N] ...
- 【51NOD1806】wangyurzee的树(Prufer编码,容斥原理,组合计数)
题意:有n个点和m条限制,每条限制限制了一个点的度数不能为某个数. 求合法的树的个数模10^9+7 n<=10^6 m<=17 思路:WYZ作业 首先m<=17显然是2^m容斥 枚举 ...
- codeforces 691F(组合数计算)
Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to ...
- codevs——2750 心系南方灾区
2750 心系南方灾区 时间限制: 1 s 空间限制: 2000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 现在我国南方正在承受百年 ...
- docker容器-快速部署Jenkins
1.在本地虚拟机环境.安装CentOS 7,并安装docker容器 2.在docker容器中执行 docker pull jenkinsci/blueocean 3.查看已经下载的Jenkins镜像 ...