BZOJ 1036 [ZJOI2008]树的统计Count 动态维护树上求和与求最大值 LCT板题
模板,也可以用树链剖分+线段树做O(nlog2)O(nlog^2)O(nlog2)
用LCT做O(nlog)O(nlog)O(nlog)在乘上一个大于30的常数…然后LCT比树剖慢一倍…
CODE
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
template<typename T>inline void read(T &num) {
char ch; int flg = 1;
while((ch=getchar())<'0'||ch>'9')if(ch=='-')flg=-flg;
for(num=0;ch>='0'&&ch<='9';num=num*10+ch-'0',ch=getchar());
num*=flg;
}
const int MAXN = 30005;
int n, q, u[MAXN], v[MAXN];
namespace LCT {
#define ls ch[x][0]
#define rs ch[x][1]
int ch[MAXN][2], fa[MAXN];
LL w[MAXN], sum[MAXN], mx[MAXN];
bool rev[MAXN];
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } //判断是否为根
inline bool get(int x) { return x == ch[fa[x]][1]; }
inline void upd(int x) { //上传
sum[x] = sum[ls] + sum[rs] + w[x];
mx[x] = max(w[x], max(mx[ls], mx[rs]));
}
inline void rot(int x) {
int y = fa[x], z = fa[y], l = get(x), r = l^1;
if(!isr(y)) ch[z][get(y)] = x;
fa[ch[x][r]] = y; fa[y] = x; fa[x] = z;
ch[y][l] = ch[x][r]; ch[x][r] = y;
upd(y), upd(x);
}
inline void mt(int x) { if(rev[x]) rev[x] ^= 1, rev[ls] ^= 1, rev[rs] ^= 1, swap(ls, rs); } //下传
void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
inline void splay(int x) {
mtpath(x);
for(; !isr(x); rot(x))
if(!isr(fa[x])) rot(get(x)==get(fa[x])?fa[x]:x);
}
inline int access(int x) { int y=0;
for(; x; x=fa[y=x]) splay(x), ch[x][1]=y, upd(x);
return y;
}
inline void bert(int x) { access(x), splay(x), rev[x] ^= 1; } //换根
inline int sert(int x) { //找根
access(x), splay(x);
for(; ch[x][0]; x=ch[x][0]);
return x;
}
inline void link(int x, int y) {
bert(x);
if(sert(y) == x) return;
fa[x] = y;
}
inline void cut(int x, int y) {
bert(x), access(y), splay(y);
if(sert(y) != x || fa[x] != y || ch[x][1] != 0) return;
fa[x] = ch[y][0] = 0; upd(y);
}
inline void modify(int x, int val) {
splay(x), w[x] = val, upd(x);
}
inline int split(int x, int y) {
bert(x), access(y), splay(y);
return y;
}
inline int querymax(int x, int y) {
split(x, y); return mx[y];
}
inline int querysum(int x, int y) {
split(x, y); return sum[y];
}
}
using namespace LCT;
int main () {
read(n); mx[0] = -0x7f7f7f7f; //把0设成-无穷,因为upd的时候会访问到
for(int i = 1; i < n; ++i) read(u[i]), read(v[i]);
for(int i = 1; i <= n; ++i) read(w[i]);
for(int i = 1; i < n; ++i) link(u[i], v[i]);
read(q);
char s[10]; int x, y;
while(q--) {
scanf("%s", s); read(x), read(y);
if(s[1] == 'M') printf("%d\n", querymax(x, y));
else if(s[1] == 'S') printf("%d\n", querysum(x, y));
else modify(x, y);
}
}
BZOJ 1036 [ZJOI2008]树的统计Count 动态维护树上求和与求最大值 LCT板题的更多相关文章
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 12266 Solved: 4945[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 14354 Solved: 5802 [Subm ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 11102 Solved: 4490[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
- bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 16294 Solved: 6645[Submit ...
- bzoj 1036: [ZJOI2008]树的统计Count (树链剖分+线段树 点权)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 21194 Solved: 8589[Submit ...
随机推荐
- 《Brennan's Guide to Inline Assembly》学习笔记
原文见Brennan's Guide to Inline Assembly. AT&T语法 vs Intel语法 DJGPP是基于GCC的,因此它使用AT&T/UNIT语法,这和Int ...
- HTML5页面如何在手机端浏览器调用相机、相册功能
最近在做一个公司的保险信息处理系统项目,开发微信端浏览器访问的HTML5的页面,页面中有一个<input id="input" type="file"/& ...
- 【转贴】linux 终端报Message from syslogd
linux 终端报Message from syslogd xiao9873341760人评论8537人阅读2017-03-27 14:19:31 https://blog.51cto.com/xia ...
- Scala当中什么是RDD(Resilient Distributed Datasets)弹性分布式数据集
RDD(Resilient Distributed Datasets)弹性分布式数据集.你不好理解的话,可以把RDD就可以看成是一个简单的"动态数组"(比如ArrayList),对 ...
- 下载HTMLTestRunner 地址
通过pip安装 HTMLTestRunne失败 则需要通过手动下载. 下载地址: http://tungwaiyip.info/software/HTMLTestRunner.html 下载后,把H ...
- gitlab安装指南(gitlab-ce-9.4.3-ce.0.el7.x86_64 centos7)
1,安装gitlab wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-9.4.3-ce.0.el7. ...
- 学习GTK+ (1) ——编写helloworld
环境 我使用的是新安装的manjaro 18.1 (kde版),安装新系统后后直接可以开始写代码,不需要安装各种调用的库等. 推荐一个网站,gnome开发者 https://developer.gno ...
- Stanford NLP 课程笔记之计算字符串距离
在自然语言处理任务中,有时候需要计算两个字符串之间的相似度,也可以称作是两者之间的距离,用最小编辑距离表示. 最小编辑距离用{Insertion,Deletion,Substitution}这三种操作 ...
- 正则表达式split匹配多种例如 “】”,“,”两种(页面级中英文切换方案)
在做登陆界面的时候,因为涉及到中英文 因为前后台已经分离,所以前端需要自行设计中英文 做法: 编写两个文件,一个中文文件,一个是英文文件,分别放在对应的目录下面 文件的内容 { "login ...
- JS-闭包练习
首先,第一个输出,因为前置运算,i要先参与输出,然后再自增,所以输出为0 第二个输出,因为f1和f2是不同的函数,不共享i变量,所以输出也为0 第三个输出,因为是f1,共享i,所以i加了1,输出为1 ...