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 ...
随机推荐
- linux arping-通过发送ARP协议报文测试网络
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 arping命令是用于发送arp请求到一个相邻主机的工具,arping使用arp数据包,通过ping命令检查设备上的硬件地址.能 ...
- 等待某(N)个线程执行完再执行某个线程的几种方法(Thread.join(),CountDownLatch,CyclicBarrier,Semaphore)
1.main线程中先调用threadA.join() ,再调用threadB.join()实现A->B->main线程的执行顺序 调用threadA.join()时,main线程会挂起,等 ...
- 将一个list中的元素的某一属性取出来单独放到一个list里面
有很多时候我们会遇到这样的场景,就是要将一个list中的某一个元素中的某一属性单独拿出来放在一个新的list里面,这中时候,我们就可以用以下的方法来进行实现: List<DTO> item ...
- LeetCode(60) Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...
- 洛谷 4932 洛谷10月月赛II T1 浏览器
[题解] x xor y的结果在二进制下有奇数个1,等价于x与y在二进制下的1的个数之和为奇数,因为x xor y减少的1的个数一定是偶数(两个数这一位都为1,xor的结果为0,减少了2个1) 那么答 ...
- 集训第五周动态规划 D题 LCS
Description In a few months the European Currency Union will become a reality. However, to join the ...
- 如何在matlab里安装libsvm包
有时我们需要用到SVR(支持向量回归)方法,而 matlab 自带的svm工具箱不能做回归分析,于是有了安装libsvm包的打算. 中间遇到一些困难,比如找不到编译器等等,经过一下午和一晚上的努力,在 ...
- Leetcode 204计数质数
计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 比计算少n中素数的个数. 素数又称质 ...
- Node.js & module system
Node.js & module system Node.js v10.9.0 Documentation https://nodejs.org/api/modules.html#module ...
- Thinkphp5.0 的请求方式
Thinkphp5.0 的请求方式 方法一(使用框架提供的助手函数): public function index(){ $request = request(); dump($request); } ...