【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( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
随机推荐
- Windows下基于python3使用word2vec训练中文维基百科语料(三)
对前两篇获取到的词向量模型进行使用: 代码如下: import gensim model = gensim.models.Word2Vec.load('wiki.zh.text.model') fla ...
- perl6中的hash定义(1)
,,,); say %hash; , b => ); say %hash2; my %hash3 = (:name('root'), :host('localost')); say %hash3 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- CentOS在ssh下远程重装系统
CentOS在ssh下远程重装系统 http://www.zxsdw.com/index.php/archives/913/ 国外VPS服务器一般都有控制面板,有很多种系统可自行安装,但国内有些IDC ...
- 自定义shell开头PS1
vim /etc/profile export PS1="flag:\W \u\$" \h是主机名,并不全,域 \W是当前所在目录名 \u 是当前shell用户名
- uoj#35 后缀排序(后缀数组模版)
#include<bits/stdc++.h> #define N 100005 using namespace std; char s[N]; int a[N],c[N],t1[N],t ...
- HTML5API(2)
四.文件API 1.概述 H5允许JS有条件的读取客户端文件 允许读取的文件:1.待上传的文件2.拖进浏览器的文件 多文件上传设置属性multiple 过滤上传文件类型 设置accept属性 acce ...
- yum安装的Apache的各种配置文件的位置
//配置文件 /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/proxy_ajp.conf / ...
- CentOS安装按进程实时统计流量情况工具NetHogs笔记
CentOS安装按进程实时统计流量情况工具NetHogs笔记 一.概述 NetHogs是一款开源.免费的,终端下的网络流量监控工具,它可监控Linux的进程或应用程序的网络流量.NetHogs只能实时 ...
- RabbitMQ 实践及使用
目录 - 1. RabbitMQ的安装 - 1.1 配置好 epel - 1.2 安装 RPM包 - 1.3 创建用户设置权限- 2. RabbitMQ组件- 3. RabbitMQ ...