http://www.lydsy.com/JudgeOnline/problem.php?id=1036

题意:中文题意。

思路:也是普通的树链剖分。唯一注意的点是在change函数中

     while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
if(type == ) ans = max(ans, query(, , tim, tid[top[u]], tid[u], type));
else ans += query(, , tim, tid[top[u]], tid[u], type);
u = fa[top[u]];
}

这里的dep比较的是节点的top节点的深度,而不是直接比较节点的深度。因为这里WA了好久。只能说还未完全理解透细节。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 30010
#define INF 10000000000
#define lson rt<<1, l, m
#define rson rt<<1|1, m + 1, r struct node
{
int v, nxt, w;
}edge[N*];
int top[N], tid[N], fa[N], son[N], siz[N], dep[N], rak[N], w[N], tim;
int head[N], tot;
struct T
{
long long sum;
long long ma;
}tree[N<<]; void init()
{
memset(head, -, sizeof(head));
memset(son, -, sizeof(son));
tot = tim = ;
} void add(int u, int v)
{
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} void dfs1(int u, int f, int d)
{
siz[u] = ;
dep[u] = d;
fa[u] = f;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == f) continue;
dfs1(v, u, d + );
siz[u] += siz[v];
if(son[u] == - || siz[son[u]] < siz[v]) son[u] = v;
}
} void dfs2(int u, int tp)
{
top[u] = tp;
tid[u] = ++tim;
rak[tim] = u;
if(son[u] == -) return ;
dfs2(son[u], tp);
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v != fa[u] && v != son[u]) dfs2(v, v);
}
} void pushup(int rt)
{
tree[rt].sum = tree[rt<<].sum + tree[rt<<|].sum;
tree[rt].ma = max(tree[rt<<].ma, tree[rt<<|].ma);
} void build(int rt, int l, int r)
{
tree[rt].sum = ;
tree[rt].ma = ;
if(l == r) {
tree[rt].sum = w[rak[l]];
tree[rt].ma = w[rak[l]];
return ;
}
int m = (l + r) >> ;
build(lson); build(rson);
pushup(rt);
} void update(int rt, int l, int r, int id, int val)
{
if(l == r && l == id) {
tree[rt].sum = val;
tree[rt].ma = val;
return ;
}
int m = (l + r) >> ;
if(id <= m) update(lson, id, val);
else update(rson, id, val);
pushup(rt);
} long long query(int rt, int l, int r, int L, int R, int type)
{
long long ans = ;
if(type == ) ans = -INF;
if(L <= l && r <= R) {
if(type == ) ans = max(ans, tree[rt].ma);
else ans += tree[rt].sum;
return ans;
}
int m = (l + r) >> ;
if(L <= m) {
if(type == ) ans = max(ans, query(lson, L, R, type));
else ans += query(lson, L, R, type);
}
if(m < R) {
if(type == ) ans = max(ans, query(rson, L, R, type));
else ans += query(rson, L, R, type);
}
return ans;
} long long change(int u, int v, int type)
{
long long ans = ;
if(type == ) ans = -INF;
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
if(type == ) ans = max(ans, query(, , tim, tid[top[u]], tid[u], type));
else ans += query(, , tim, tid[top[u]], tid[u], type);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
if(type == ) ans = max(ans, query(, , tim, tid[u], tid[v], type));
else ans += query(, , tim, tid[u], tid[v], type);
return ans;
} int main()
{
int n, q;
scanf("%d", &n);
init();
for(int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
for(int i = ; i <= n; i++) scanf("%d", &w[i]);
dfs1(, , );
dfs2(, );
build(, , tim);
// for(int i = 1; i <= n; i++) printf("tid[%d] : %d\n", i, tid[i]);
scanf("%d", &q);
while(q--) {
char s[];
int a, b;
scanf("%s%d%d", s, &a, &b);
if(s[] == 'C') {
update(, , tim, tid[a], b);
} else {
int type = ;
if(s[] == 'S') type = ;
printf("%lld\n", change(a, b, type));
}
}
return ;
}

BZOJ 1036:树的统计Count(树链剖分)的更多相关文章

  1. BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit ...

  2. Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11102  Solved: 4490[Submit ...

  3. BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )

    树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...

  4. bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 16294  Solved: 6645[Submit ...

  5. BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14982  Solved: 6081[Submit ...

  6. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)

    [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14968  Solved: 6079[Submit][Stat ...

  7. 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分

    [BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...

  8. bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...

  9. Cogs 1688. [ZJOI2008]树的统计Count(树链剖分+线段树||LCT)

    [ZJOI2008]树的统计Count ★★★ 输入文件:bzoj_1036.in 输出文件:bzoj_1036.out 简单对比 时间限制:5 s 内存限制:162 MB [题目描述] 一棵树上有n ...

  10. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...

随机推荐

  1. 文件传输基础——Java IO流

    一.文件的编码 package com.study.io; /** * 测试文件编码 */ public class EncodeDemo { /** * @param args * @throws ...

  2. 维护计划生成的SSIS包存储在哪

    首先理解导入导出包的基本概念:http://technet.microsoft.com/zh-cn/library/ms141772(v=SQL.110).aspx包既可以保存在SQL Server ...

  3. Json.Net Demo2

    新建一个空的Web项目,名称JsonServer,该网页实现Ajax数据请求和响应. 添加Newtonsoft.Json.dll的Dll引用,添加JQuery API文件,目录结构如下: 新建一个Pe ...

  4. C++ 基础复习 1

    1. 友元 友元的作用是,友元函数内部可以直接访问外围类的private的字段或方法.通俗的理解就是解决了访问权限的问题. 1) 有点像java的内部类,但是只能在外围类中声明,定义(实现)部分要写在 ...

  5. MVC 登录认证与授权及读取登录错误码

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    最近在自学MVC,遇到的问题很多,索性一点点总结下 ...

  6. Troubleshooting JDK

    收集整理下JDK自带的关于 Troubleshooting 的文档 Java 2 Platform, Standard Edition 5.0 Troubleshooting and Diagnost ...

  7. 监控linux服务器网卡流量

    监控linux服务器网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:众所周知,我们安装zabbix服务器 ...

  8. SQL in查询

    --sal为员工工资 select * from emp;

  9. poj: 1004

    简单题 #include <iostream> #include <stdio.h> #include <string.h> #include <stack& ...

  10. asp,asp.net 以表格输出excel,数据默认科学计数的解决办法

    关键字:  style="vnd.ms-excel.numberformat:@" 问题:在用table仿excel生成中经常遇到类似于身份证的长整数类型excel默认当成科学计数 ...