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. 第四篇 Replication:事务复制-订阅服务器

    本篇文章是SQL Server Replication系列的第四篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...

  2. C#线程系列讲座(1):BeginInvoke和EndInvoke方法

    一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...

  3. PostgreSQL index types and index bloating

    warehouse_db=# create table item (item_id integer not null,item_name text,item_price numeric,item_da ...

  4. Leetcode: Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  5. int 与 Integer--话说数组转集合

    话说是自从JDK5后,而这就可以自动进行类型转换了. 当然,区别还是有的,就是对象和“非对象”什么的.可是,今天进行一个测试,出了一个小问题,现将代码贴下: 代码0:先来一个正常点的,用String进 ...

  6. eclipse批量删除断点(转)

    1.首先调出BreakPoints选项卡(Window--show View--Other--BreakPoints). 2.选择BreakPoints选项卡,选择所有断点,点击删除即可. 

  7. 使用javabeen的好处

    什么是javabeen? javaBean在MVC设计模型中是model,又称模型层, 在一般的程序中,我们称它为数据层, 就是用来设置数据的属性和一些行为,然后提供获取属性和设置属性的get/set ...

  8. paper 50 :人脸识别简史与近期进展

    自动人脸识别的经典流程分为三个步骤:人脸检测.面部特征点定位(又称Face Alignment人脸对齐).特征提取与分类器设计.一般而言,狭义的人脸识别指的是"特征提取+分类器"两 ...

  9. spring中的bean后处理器

    package com.process; import org.springframework.beans.BeansException; import org.springframework.bea ...

  10. Logic and Fault simulation

    fault simulation是指对fault circuit的simulation,来locate manufacturing defects并且进行fault diagnosis. logic ...