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. AWE、加载计数器错误

    错误#1 16:28 2012-7-25数据库服务器A想开启下sql server 2000的AWE.结果发现在查询分析器中执行RECONFIGURE时报错.运行的语句为: sp_configure ...

  2. CString + UINT Error:有多个运算符"+="与这些操作数匹配

    在OnChar中,参数UINT nChar 有一个CString str,现在执行 str += nChar报错:Error:有多个运算符"+="与这些操作数匹配 解决办法:把UI ...

  3. Windows下MongoDB安装与PHP扩展

    MongoDB是什么就不再累述了,下面只写MongoDB安装与PHP扩展的方法. 一,安装准备 MongoDB 如果网速慢,可以到MongoDB中文社区的百度网盘下载,密码3gun.(根据你的操作系统 ...

  4. MySQL-profiling的使用

    分析SQL执行带来的开销是优化SQL的重要手段.在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析.该参数可以在全局和session级别来设置.对于全局级别则作用于整个MySQL ...

  5. iOS中scrollview是否要回弹

    1. @property(nonatomic) BOOL bounces //当滚动到内容边缘是否发生反弹,default is YES.2. @property(nonatomic) BOOL al ...

  6. Lintcode: Wood Cut

    Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...

  7. 20145207《Java程序设计》第三周学习总结

    前言 24号回来的,书看的差不多了,博客一直没写,求老师原谅呀!!!!!哈哈哈哈.博客我从今天开始补,对着书,一天最多能弄个两篇毕竟写这个东西挺费心思德,当然我做事慢也有关系.但是我会尽快的.老实讲, ...

  8. [转] 多线程 《深入浅出 Java Concurrency》目录

    http://ifeve.com/java-concurrency-thread-directory/ synchronized使用的内置锁和ReentrantLock这种显式锁在java6以后性能没 ...

  9. HDU 4718 The LCIS on the Tree(树链剖分)

    Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i < ...

  10. mesos概述

    mesos解决的问题 不同的分布式运算框架(spark,hadoop,ES,MPI,Cassandra,etc.)中的不同任务往往需要的资源(内存,CPU,网络IO等)不同,它们运行在同一个集群中,会 ...