模板,也可以用树链剖分+线段树做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板题的更多相关文章

  1. BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)

    BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...

  2. 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 12266  Solved: 4945[Submit ...

  3. BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit ...

  4. BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 14354  Solved: 5802 [Subm ...

  5. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  6. Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11102  Solved: 4490[Submit ...

  7. BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )

    树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...

  8. bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 16294  Solved: 6645[Submit ...

  9. bzoj 1036: [ZJOI2008]树的统计Count (树链剖分+线段树 点权)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 21194  Solved: 8589[Submit ...

随机推荐

  1. Linux Crontab格式说明

    Crontab基本格式: * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4 ...

  2. (七)Spring 配置 c3p0 连接池

    目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...

  3. Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册

    前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...

  4. Codeforces 1247F. Tree Factory

    传送门 正难则反,把链操作成树不好想,那么考虑一下如何把树变成链 每次操作相当于把一个兄弟变成儿子(我把你当兄弟你竟然想把我当儿子.jpg) 注意到每次操作最多只能使树的深度增加 $1$ 因为链的深度 ...

  5. IOS开发copy,nonatomic, retain,weak,strong用法

     readwrite 是可读可写特性;需要生成getter方法和setter方法时  readonly 是只读特性 只会生成getter方法 不会生成setter方法 ;不希望属性在类外改变  ass ...

  6. ubuntu目录结构(转)

    /:根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin:可执行二进制文件的目录,如常用的命令ls. ...

  7. Oracle学习笔记:ASCII码转换(chr和ascii函数)

    今天get到一个骚操作,通过ascii码转换之后来进行互换编码的. select chr(ascii('f') + ascii('m') - ascii('a')) from dual; 有必要对as ...

  8. JS数组判断,方法

    怎么判断一个对象是不是数组? 首先可以用 ES5 提供的 isArray 方法进行判断(注意:Array.isArray是ES 5.1推出的,不支持IE6~8,所以在使用的时候也应注意兼容问题. ) ...

  9. 红黑树和AVL树

    在此之前,我没有了解过红黑树以及AVL tree,真是孤陋寡闻.如果你也在学习的话,我们一起进步. 如果,你很急,那么只看红色加粗即可. 1.红黑树(RB-tree) 红黑树是一种特殊的二叉搜索树,特 ...

  10. Spring Cloud(六)服务网关 zuul 快速入门

    服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...