HYSBZ 1036 树的统计Count (水题树链剖分)
题意:中文题。
析:就是直接维护一个最大值和一个和,用线段树维护即可,这个题很简单,但是我卡了一晚上,就是在定位的时候,位置直接反过来了,但是样例全过了。。。真是。。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int to, next;
};
Edge edge[maxn<<1];
int head[maxn], cnt, fa[maxn];
int dep[maxn], top[maxn], num[maxn];
int son[maxn], p[maxn], pos, a[maxn], fp[maxn];
int sum[maxn<<2], maxv[maxn<<2]; void init(){
memset(son, -1, sizeof son);
cnt = 0; pos = 1;
memset(head, -1, sizeof head);
} void add_edge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} void dfs1(int u, int f, int d){
dep[u] = d; fa[u] = f;
num[u] = 1;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == f) continue;
dfs1(v, u, d+1);
num[u] += num[v];
if(son[u] == -1 || num[v] > num[son[u]]) son[u] = v;
}
} void dfs2(int u, int sp){
top[u] = sp; p[u] = pos++;
fp[p[u]] = u;
if(son[u] == -1) return ;
dfs2(son[u], sp);
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} void push_up(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
maxv[rt] = max(maxv[rt<<1], maxv[rt<<1|1]);
} void build(int l, int r, int rt){
if(l == r){
sum[rt] = maxv[rt] = a[fp[l]];
return ;
}
int m = l + r >> 1;
build(lson);
build(rson);
push_up(rt);
} void update(int M, int val, int l, int r, int rt){
if(l == r){
sum[rt] = maxv[rt] = val;
return ;
}
int m = l + r >> 1;
if(M <= m) update(M, val, lson);
else update(M, val, rson);
push_up(rt);
} int queryMax(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return maxv[rt];
int m = l + r >> 1;
int ans = -INF;
if(L <= m) ans = queryMax(L, R, lson);
if(R > m) ans = max(ans, queryMax(L, R, rson));
return ans;
} int querySum(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return sum[rt];
int m = l + r >> 1;
int ans = 0;
if(L <= m) ans = querySum(L, R, lson);
if(R > m) ans += querySum(L, R, rson);;
return ans;
} int solve(int u, int v, bool ok){
int f1 = top[u], f2 = top[v];
int ans = -INF;
if(ok) ans = 0;
while(f1 != f2){
if(dep[f1] < dep[f2]){
swap(f1, f2);
swap(u, v);
}
if(ok) ans += querySum(p[f1], p[u], 1, n, 1);
else ans = max(ans, queryMax(p[f1], p[u], 1, n, 1));
u = fa[f1];
f1 = top[u];
}
if(dep[u] > dep[v]) swap(u, v);
if(ok) return ans + querySum(p[u], p[v], 1, n, 1);
return max(ans, queryMax(p[u], p[v], 1, n, 1));
} int main(){
while(scanf("%d", &n) == 1){
init();
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
dfs1(1, 0, 0);
dfs2(1, 1);
build(1, n, 1);
scanf("%d", &m);
while(m--){
char op[10];
int u, v;
scanf("%s %d %d", op, &u, &v);
if(op[0] == 'C') update(p[u], v, 1, n, 1);
else printf("%d\n", solve(u, v, op[1] == 'S'));
}
}
return 0;
}
HYSBZ 1036 树的统计Count (水题树链剖分)的更多相关文章
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- [BZOJ1036][ZJOI2008]树的统计Count 解题报告|树链剖分
树链剖分 简单来说就是数据结构在树上的应用.常用的为线段树splay等.(可现在splay还不会敲囧) 重链剖分: 将树上的边分成轻链和重链. 重边为每个节点到它子树最大的儿子的边,其余为轻边. 设( ...
- 【BZOJ】1036: [ZJOI2008]树的统计Count(lct/树链剖分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1036 lct: (ps:为嘛我的那么慢T_T,不知道排到哪了..难道别人都是树剖吗...看来有必要学 ...
- BZOJ 1036 [ZJOI2008]树的统计Count(动态树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题意:一棵树,每个节点有一个权值.三种操作:(1)修改某个节点的权值:(2)输出某两 ...
- Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[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 ...
- 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7496 Solved: 3078[Submit] ...
随机推荐
- 从如何优化SQL入手,提高数据仓库的ETL效率
1 引言数据仓库建设中的ETL(Extract, Transform, Load)是数据抽取.转换和装载到模型的过程,整个过程基本是通过控制用SQL语句编写的存储过程和函数的方式来实现对 ...
- 浅谈FPGA的选型
工欲善其事必先利其器,开发FPGA的第一步,当然是选择一片符合设计需求的芯片. 器件特色 选片第一个关注的应该是FPGA器件的专用资源. 例如是否需要高速接口,如果需要的话,需要多少个通道,各个通道需 ...
- DHCP(六)
DHCP报文: DHCP共有八种报文,分别为DHCP Discover.DHCP Offer.DHCP Request.DHCP ACK.DHCP NAK.DHCP Release.DHCP Decl ...
- 杂项:UN-标准通用置标语言
ylbtech-杂项:标准通用置标语言 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 ...
- 配置MapReduce插件时,弹窗报错org/apache/hadoop/eclipse/preferences/MapReducePreferencePage : Unsupported major.minor version 51.0(Hadoop2.7.3集群部署)
原因: hadoop-eclipse-plugin-2.7.3.jar 编译的jdk版本和eclipse启动使用的jdk版本不一致导致. 解决方案一: 修改myeclipse.ini文件即可解决. ...
- Unity Shader入门教程(三)自制光照模型
光照模型的概念目前还不明晰,因为笔者也是一个初学者,所以请小心对待笔者介绍的内容.笔者认为光照模型是规定光照算法的模型,比如说前面提到的Lambert光照模型,规定了材质表面的光线的表达式为 环境光+ ...
- xunsearch搜索使用
目录 如何开始搜索? 典型处理 快捷操作 搜索中的串接操作 构建搜索语句 如何开始搜索? <?php // 引入 require_once './sdk/xs/lib/XS.php'; // 创 ...
- python学习(十六) 测试
测试驱动开发. 16.1 先测试,后编码 16.1.1 精确的需求说明 16.1.2 为改变而计划 16.1.3 测试的4个步骤 16.2 测试工具 16.2.1 doctest 16.2.2 uni ...
- mac下自定义伪协议配置
之前查了很多资料,最近也在挖掘研究这方面的漏洞. windows的很简单,在注册表配置就好了,但是mac os 是unix的,没有注册表这么一说. 但是发现腾讯等配置了自定义等协议,例如:tencen ...
- [原创]Java项目统一UTC时间方案
Java项目统一UTC时间方案 作者:Gods_巨蚁 引言 近期团队的个别项目在进行框架升级后,部分时间值存在8小时误差,原因是错误的将数据库中的时间数据理解成了UTC时间(旧版本认为是北京时间) 考 ...