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. I2C实时时钟rx-8025板卡实际应用

    rx-8025是片外I2C实时时钟,其应用于9260板卡方法如下.总体思想是配置内核添加驱动(I2C驱动,内核已提供的很完备),板级文件添加设备,添加设备文件以应用程序操作. 1. 配置内核 1)I2 ...

  2. LinkedHashMap 和 LRU算法实现

    个人觉得LinkedHashMap 存在的意义就是为了实现 LRU 算法. public class LinkedHashMap<K,V> extends HashMap<K,V&g ...

  3. PAT 解题报告 1048. Find Coins (25)

    1048. Find Coins (25) Eva loves to collect coins from all over the universe, including some other pl ...

  4. 前端新手分析 AJAX执行顺序,数据走向

    我是一名前端的newer 在刚学习AJAX和eJS的时候,对于顺序上面有很大迷惑,现在稍微清楚了一点, 理解不对的地方,还请各位大牛帮助给我指导一下. 总的 服务器和客户端的顺序   一. 除了必要的 ...

  5. CentOS 7 自动更新时间日期

    # ntpdate time.nist.gov 或 # rdate -s time.nist.gov 下列服务器可用 time-nw.nist.gov

  6. Java基础(5):试用Array类对数据进行操作(Sort和toString方法)

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等 1. 排序 语法:  Arrays.sort(数 ...

  7. C++之路进阶——bzoj3524(Couriers)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  8. Android Handler简单使用

    package com.example.myhandlertest3; import android.os.Bundle; import android.os.Handler; import andr ...

  9. 阿里 drds 分布式数据库分节点查询

    mybatis 模式下,xml 中写法 <select id="selectFailDetailOneNode" resultMap="BaseResultMap& ...

  10. java post请求

    package com.jfbank.loan.intf.util; import java.io.IOException;import java.util.ArrayList;import java ...