bzoj 1036: [ZJOI2008]树的统计Count (树链剖分)
ps:这道题过的人真多啊
一道树剖的模板题
(好像还可以用lct做, 然而我并不会
代码如下
/**************************************************************
Problem: 1036
User: cminus
Language: C++
Result: Accepted
Time:2380 ms
Memory:4052 kb
****************************************************************/ #include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = ;
#define ls (o << 1)
#define rs (ls + 1) int n, w[N];
int fa[N], size[N], hs[N], dep[N], top[N], pos[N], tot;
int maxn[N*], sum[N*], L, R;
vector < int > edge[N]; inline void dfs1(int u, int d, int f){
dep[u] = d; fa[u] = f; size[u] = ; hs[u] = -;
int tmp = ;
for (int i = ; i < edge[u].size(); i++){
int &v = edge[u][i];
if (v == f) continue;
dfs1(v, d + , u);
if (size[v] > tmp)
tmp = size[v], hs[u] = v;
size[u] += size[v];
}
} inline void dfs2(int u, int t){
top[u] = t, pos[u] = ++tot;
if (hs[u] == -) return ;
dfs2(hs[u], t);
for (int i = ; i < edge[u].size(); i++){
int &v = edge[u][i];
if (v != hs[u] && v != fa[u])
dfs2(v, v);
}
} inline int getSum(int o, int l, int r){
if (L <= l && r <= R) return sum[o];
int mid = (l + r) >> , ans = ;
if (L <= mid) ans += getSum(ls, l, mid);
if (R > mid) ans += getSum(rs, mid + , r);
return ans;
} inline int getMax(int o, int l, int r){
if (L <= l && r <= R) return maxn[o];
int mid = l + r >> , ans = -0x3f3f3f3f;
if (L <= mid) ans = max(ans, getMax(ls, l, mid));
if (R > mid) ans = max(ans, getMax(rs, mid + , r));
return ans;
} inline int getMax(int l, int r){
L = l, R = r;
return getMax(, , tot);
} inline int getSum(int l, int r){
L = l, R = r;
return getSum(, , tot);
} inline void queryMax(int u, int v){
int f1 = top[u], f2 = top[v], ans = -0x3f3f3f3f;
while(f1 != f2){
if (dep[f1] < dep[f2])
swap(u, v), swap(f1, f2);
ans = max(ans, getMax(pos[f1], pos[u]));
u = fa[f1]; f1 = top[u];
}
if (dep[u] > dep[v]) swap(u, v);
printf("%d\n", max(ans, getMax(pos[u], pos[v])));
} inline void querySum(int u, int v){
int f1 = top[u], f2 = top[v], ans = ;
while(f1 != f2){
if (dep[f1] < dep[f2])
swap(u, v), swap(f1, f2);
ans += getSum(pos[f1], pos[u]);
u = fa[f1]; f1 = top[u];
}
if (dep[u] > dep[v]) swap(u, v);
printf("%d\n", ans + getSum(pos[u], pos[v]));
} inline void modify(int o, int l, int r, int u, int a){
if(l == r){
sum[o] = maxn[o] = a;
return ;
}
int mid = l + r >> ;
if (u <= mid) modify(ls, l, mid, u, a);
else modify(rs, mid + , r, u, a);
sum[o] = sum[ls] + sum[rs];
maxn[o] = max(maxn[ls], maxn[rs]);
} int main(){
scanf("%d", &n);
for (int i = ; i < n; i++){
int x, y; scanf("%d %d", &x, &y);
edge[x].push_back(y);
edge[y].push_back(x);
}
for (int i = ; i <= n; i++) scanf("%d", &w[i]);
dfs1(, , -); dfs2(, );
for (int i = ; i <= n; i++)
modify(, , tot, pos[i], w[i]);
int q; scanf("%d", &q);
char str[]; int x, y;
while(q--){
scanf("%s%d%d", str, &x, &y);
if (!strcmp(str, "QMAX")) queryMax(x, y);
else if (*str == 'C') modify(, , tot, pos[x], y);
else querySum(x, y);
}
return ;
}
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 树链剖分,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: 14982 Solved: 6081[Submit ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)
[ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14968 Solved: 6079[Submit][Stat ...
- 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分
[BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
- Cogs 1688. [ZJOI2008]树的统计Count(树链剖分+线段树||LCT)
[ZJOI2008]树的统计Count ★★★ 输入文件:bzoj_1036.in 输出文件:bzoj_1036.out 简单对比 时间限制:5 s 内存限制:162 MB [题目描述] 一棵树上有n ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
随机推荐
- MySQL在大数据、高并发场景下的SQL语句优化和"最佳实践"
本文主要针对中小型应用或网站,重点探讨日常程序开发中SQL语句的优化问题,所谓“大数据”.“高并发”仅针对中小型应用而言,专业的数据库运维大神请无视.以下实践为个人在实际开发工作中,针对相对“大数据” ...
- 安装Elasticsearch到Linux(源码)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:Elasticsearch-7.1.0 硬件要求:最低2核4GB 安装过程 1.源码安装JDK ...
- 使用ssh localhost命令,发生异常ssh: connect to host localhost port 22: Connection refused
使用"ssh localhost"命令,失败: 问题分析如下: 出现这个问题是因为Ubuntu默认没有安装openssh-server.检查是否安装了openssh-server, ...
- Python之旅第五天(习题集合)
4天时间,虽然上着班,但是学的东西还是有点多,而且晚上看的比较容易忘,所以今天是习题模式,正好教程也是这么要求的,本来以为时间不长,没想到还是很崩溃啊.不多说,上干货. #关于随机产生验证码同时验证用 ...
- Mysql注入汇总!!!!!!!!!
师傅tpl!!!!! https://xz.aliyun.com/t/7169[对MYSQL注入相关内容及部分Trick的归类小结] https://www.jianshu.com/p/f261125 ...
- vue自学入门-5(vuex state)
vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 最短路-A - 畅通工程续
A - 畅通工程续 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这 ...
- vue 查看dist文件里的结构
场景:优化打包后的代码,提高性能. 1.方式一:report-json. 1.1 package.json文件里加入以下命令, "report": "vue-cli-se ...
- Nginx 配置Websocket
Nginx反向代理配置websocket nginx.org 官网推荐如下的配置,也可以直接看官网:http://nginx.org/en/docs/http/websocket.html http ...