【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( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
随机推荐
- 【BZOJ1975】【SDOI2010】魔法猪学院 [A*搜索]
魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description iPig在假期来到了传说中的魔法猪 ...
- 【BZOJ】2705: [SDOI2012]Longge的问题
[题意]给定n,求∑gcd(i,n),(1<=i<=n),n<=2^32 [算法]数论(欧拉函数,gcd) [题解]批量求gcd的题目常常可以反过来枚举gcd的值. 记f(g)为gc ...
- 【BZOJ】1455 罗马游戏
[算法]可并堆(左偏树) #include<cstdio> #include<algorithm> using namespace std; ; int l[maxn],r[m ...
- 人人都能掌握的Java服务端性能优化方案
作为一个Java后端开发,我们写出的大部分代码都决定着用户的使用体验.如果我们的后端代码性能不好,那么用户在访问我们的网站时就要浪费一些时间等待服务器的响应.这就可能导致用户投诉甚至用户的流失. 关于 ...
- PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)
题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...
- Codeforces Round #482 (Div. 2) B题
题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...
- httpd -v command not found
使用 find / -name "apachectl"查找文件目录下执行 ./apachectl -v
- hdu 1548 A strange lift (dijkstra算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几 ...
- 将资源文件夹中的文件通过流的方式写入到应用的File文件夹中
//1.在Files文件夹中创建同名的数据库文件 File files = getFilesDir(); File file = new File(files, DBName); if(file.ex ...
- aircrack加reaver破解带有wps的wifi
最近心血来潮,想把小区里的无线信号测试个遍.基于目前大多数路由器都支持wps,想必各位基友们都知道aircrack和reaver这 两个工具,实属破解pin码,杀人越货,居家旅行之必备良药.像以前跑r ...