【BZOJ 1036】【ZJOI 2008】树的统计Count
http://www.lydsy.com/JudgeOnline/problem.php?id=1036
复习了一下好写好调的lct模板啦啦啦~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 30003;
int n;
struct node *null;
struct node {
node *fa, *ch[2];
int w, sum, ma, rev;
bool pl() {return fa->ch[1] == this;}
bool check() {return fa == null || (fa->ch[0] != this && fa->ch[1] != this);}
void setc(node *r, int c) {ch[c] = r; if (r != null) r->fa = this;}
void count() {
sum = ch[0]->sum + ch[1]->sum + w;
ma = w;
if (ch[0] != null) ma = max(ma, ch[0]->ma);
if (ch[1] != null) ma = max(ma, ch[1]->ma);
}
void push() {if (rev) {swap(ch[0], ch[1]); rev = 0; ch[0]->rev ^= 1; ch[1]->rev ^= 1;}}
} pool[N];
namespace LCT {
void rotate(node *r) {
node *f = r->fa;
int c = r->pl();
if (f->check()) r->fa = f->fa;
else f->fa->setc(r, f->pl());
f->setc(r->ch[c ^ 1], c);
r->setc(f, c ^ 1);
f->count();
}
void update(node *r) {if (!r->check()) update(r->fa); r->push();}
void splay(node *r) {
update(r);
for (; !r->check(); rotate(r))
if (!r->fa->check()) rotate(r->pl() == r->fa->pl() ? r->fa : r);
r->count();
}
node *access(node *r) {
node *y = null;
while (r != null) {
splay(r);
r->ch[1] = y;
y = r; r = r->fa;
}
return y;
}
void changeroot(node *r) {access(r)->rev ^= 1; splay(r);}
void link(node *r, node *t) {changeroot(r); r->fa = t;}
int query_max(node *u, node *v) {changeroot(u); access(v); splay(v); return v->ma;}
int query_sum(node *u, node *v) {changeroot(u); access(v); splay(v); return v->sum;}
int x[N], y[N];
void init() {
null = &pool[0];
null->fa = null->ch[0] = null->ch[1] = null;
null->ma = null->sum = null->rev = null->w = 0;
scanf("%d", &n);
for (int i = 1; i < n; ++i) scanf("%d%d", x + i, y + i);
for (int i = 1; i <= n; ++i) {
pool[i].fa = pool[i].ch[0] = pool[i].ch[1] = null;
pool[i].rev = 0;
scanf("%d", &pool[i].w);
pool[i].ma = pool[i].sum = pool[i].w;
}
for (int i = 1; i < n; ++i) link(&pool[x[i]], &pool[y[i]]);
}
}
int main() {
LCT::init();
int q, u, v;
char s[18];
scanf("%d", &q);
while (q--) {
scanf("%s%d%d", s, &u, &v);
if (s[0] == 'C') {
LCT::splay(&pool[u]);
pool[u].w = pool[u].ma = pool[u].sum = v;
} else {
if (s[1] == 'M') printf("%d\n", LCT::query_max(&pool[u], &pool[v]));
else printf("%d\n", LCT::query_sum(&pool[u], &pool[v]));
}
}
return 0;
}
【BZOJ 1036】【ZJOI 2008】树的统计Count的更多相关文章
- BZOJ 1036: [ZJOI2008]树的统计Count(树链剖分)
树的统计CountDescription一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改 ...
- 【BZOJ 1036】[ZJOI2008]树的统计Count
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 [题意] [题解] 树链剖分入门题; 每一条链维护一个线段树就好; uppest ...
- zjoi 2008 树的统计——树链剖分
比较基础的一道树链剖分的题 大概还是得说说思路 树链剖分是将树剖成很多条链,比较常见的剖法是按儿子的size来剖分,剖分完后对于这课树的询问用线段树维护——比如求路径和的话——随着他们各自的链向上走, ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- 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 ...
- 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 12266 Solved: 4945[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
随机推荐
- 【51NOD-0】1134 最长递增子序列
[算法]动态规划 [题解]经典模型:最长上升子序列(n log n) #include<cstdio> #include<algorithm> #include<cstr ...
- 爬虫--requests讲解
什么是requests? Requests是用Python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全 ...
- chrome最小字体12px如何修改
在html标记样式里加入 <style> html { -webkit-text-size-adjust:none } </style> 这样的方式可以设置chrome字体小于 ...
- 【自己练习】linux常见命令——(六)
菜鸟教程命令大全 http://www.runoob.com/linux/linux-command-manual.html 命令大全: http://man.linuxde.net/ ta ...
- MFC不同工程(解决方案)之间对话框资源的复制与重用方法(转)
原文转自 https://blog.csdn.net/lihui126/article/details/45556687
- Python3安装Celery模块后执行Celery命令报错
1 Python3安装Celery模块后执行Celery命令报错 pip3 install celery # 安装正常,但是执行celery 命令的时候提示没有_ssl模块什么的 手动在Python解 ...
- TCP之非阻塞connect和accept
套接字的默认状态是阻塞的,这就意味着当发出一个不能立即完成的套接字调用时,其进程将被投入睡眠,等待响应操作完成,可能阻塞的套接字调用可分为以下四类: (1) 输入操作,包括read,readv,rec ...
- Linux线程同步
1. 线程同步: 当多个控制线程共享相同的内存时,需要确保每个线程看到一致的数据视图.当某个线程可以修改变量,而其他线程也可以读取或者修改这个变量的时候,就需要对这些线程进行同步,以确保他们在访问变量 ...
- Jquery屏蔽浏览器的F1-F12快捷键,在IE,GOOGLE下测试均无问题
在网上找了找,很多都是js实现的,东找西找,再加上自己的想法也勉强的完成了,直接看代码 <script type="text/javascript" src="Sc ...
- 尽量用const,enum,inline代替define
在读<Effective C++>之前,我确实不知道const,enum,inline会和define扯上什么关系,看完感觉收获很大,记录之. define: 宏定义. 在编译预处理时,对 ...