链剖裸题。。。你值得一做~

用线段树多维护一个mx,少写一个tag

#include<cstdio>
#include<iostream>
#define ll long long
#define R register ll
#define ls (tr<<1)
#define rs (tr<<1|1)
using namespace std;
const int N=;
inline ll g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,cnt,num,mod;
int vr[N<<],nxt[N<<],fir[N],dfn[N],pre[N],son[N],d[N],sz[N],top[N],rw[N],w[N];
ll sum[N<<],mx[N<<];
inline void add(int u,int v) {vr[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;}
void dfs(int u) { sz[u]=; R mx=;
for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
if(d[v]) continue; pre[v]=u; d[v]=d[u]+;
dfs(v); sz[u]+=sz[v]; if(sz[v]>mx) son[u]=v,mx=sz[v];
}
}
void dfs_(int u,int tp) {
top[u]=tp,dfn[u]=++num,rw[num]=u;
if(son[u]) dfs_(son[u],tp);
for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
if(v!=pre[u]&&v!=son[u]) dfs_(v,v);
}
}
inline void build(int tr,int l,int r) {
if(l==r) {mx[tr]=sum[tr]=w[rw[l]]; return ;}
R md=(l+r)>>; build(ls,l,md),build(rs,md+,r);
sum[tr]=sum[ls]+sum[rs],mx[tr]=max(mx[ls],mx[rs]);
}
inline ll queryS(int tr,int l,int r,int LL,int RR) {
if(LL<=l&&r<=RR) return sum[tr]; R md=(l+r)>>,ret=;
if(LL<=md) ret+=queryS(ls,l,md,LL,RR); if(RR>md) ret+=queryS(rs,md+,r,LL,RR);
return ret;
}
inline ll queryM(int tr,int l,int r,int LL,int RR) {
if(LL<=l&&r<=RR) return mx[tr]; R md=(l+r)>>,ret=-;
if(LL<=md) ret=max(queryM(ls,l,md,LL,RR),ret); if(RR>md) ret=max(queryM(rs,md+,r,LL,RR),ret);
return ret;
}
inline void update(int tr,int l,int r,int pos,ll inc) {
if(l==r) {mx[tr]=sum[tr]=inc; return ;}
R md=(l+r)>>; if(pos<=md) update(ls,l,md,pos,inc); else update(rs,md+,r,pos,inc);
sum[tr]=sum[ls]+sum[rs],mx[tr]=max(mx[ls],mx[rs]);
}
inline ll queryTS(int u,int v) { R ret=;
while(top[u]!=top[v]) {
if(d[top[u]]<d[top[v]]) swap(u,v);
ret+=queryS(,,n,dfn[top[u]],dfn[u]);
u=pre[top[u]];
} if(dfn[u]>dfn[v]) swap(u,v); ret+=queryS(,,n,dfn[u],dfn[v]);
return ret;
}
inline ll queryTM(int u,int v) { R ret=-;
while(top[u]!=top[v]) {
if(d[top[u]]<d[top[v]]) swap(u,v);
ret=max(queryM(,,n,dfn[top[u]],dfn[u]),ret);
u=pre[top[u]];
} if(dfn[u]>dfn[v]) swap(u,v); ret=max(queryM(,,n,dfn[u],dfn[v]),ret);
return ret;
}
inline void print(int tr,int l,int r) {
if(l==r) {printf("%lld %lld ",sum[tr],mx[tr]); return ;} R md=(l+r)>>;
print(ls,l,md),print(rs,md+,r);
}
signed main() {
n=g(); for(R i=,u,v;i<n;++i) u=g(),v=g(),add(u,v),add(v,u);
for(R i=;i<=n;++i) w[i]=g();
d[]=top[]=pre[]=; dfs(),dfs_(,); build(,,n); m=g();
for(R i=;i<=m;++i) { register char ch;
while(!isalpha(ch=getchar())); ch=getchar(); R u=g(),v=g();
if(ch=='H') update(,,n,dfn[u],v);
else if(ch=='M') printf("%lld\n",queryTM(u,v));
else printf("%lld\n",queryTS(u,v));
}
}

想想就有些后怕,自己调了一个半小时,结果发现update没有写dfn[u],而写的u。。。

2019.04.19

BZOJ 1036 && Luogu P2590 [ZJOI2008]树的统计 树链剖分的更多相关文章

  1. BZOJ 1036: [ZJOI2008]树的统计Count-树链剖分(点权)(单点更新、路径节点最值、路径求和)模板,超级认真写了注释啊啊啊

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

  2. 树的统计Count---树链剖分

    NEFU专项训练十和十一——树链剖分 Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t ...

  3. luoguP2590 [ZJOI2008]树的统计(树链剖分)

    luogu P2590 [ZJOI2008]树的统计 题目 #include<iostream> #include<cstdlib> #include<cstdio> ...

  4. [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)

    [BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...

  5. Luogu P2590 [ZJOI2008]树的统计

    最近在学树剖,看到了这题就做了 [ZJOI2008]树的统计 思路 从题面可以知道,这题是树剖题(要求的和模板没什么区别呀喂 就是在普通的树剖上加了一个最大值 所以可以知道就是树剖+特殊的线段树 线段 ...

  6. [luogu P2590 ZJOI2008] 树的统计 (树链剖分)

    题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u ...

  7. BZOJ 1036 树的统计-树链剖分

    [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 12904 Solved: 5191[Submit][Status ...

  8. 洛谷P2590 [ZJOI2008] 树的统计 [树链剖分]

    题目传送门 树的统计 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t ...

  9. [ZJOI2008]树的统计——树链剖分

    本题是一个树链剖分裸题,由于比较菜,老是RE,后来发现是因为使用了全局变量. /************************************************************ ...

随机推荐

  1. JAVA- JDBC之DBHelper

    package com.myit.util; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.D ...

  2. python to 可执行文件

    cx_Freeze for Windows, Linux, and Mac OS X (Python 2.7, 3.x) pyinstaller for Windows, Linux, and Mac ...

  3. PHPexcel把数据库数据直接转化为excel表格

    运行文件  index.php <?php$dir =dirname(__FILE__); //获取当前文件的路径require $dir.'/Classes/phpexcel.php'; // ...

  4. struts2的通配符与动态方法调用

    1.Action标签中的method属性 我们知道action默认的执行的方法是execute方法,但是一个action只执行一个方法我们觉得有点浪费,我们希望在一个action中实现同一模块的不同功 ...

  5. AtCoder Beginner Contest 106 2018/08/18

    A - Garden Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement There is a ...

  6. bzoj 3522: Hotel dfs

    题目大意 在无边权树上求三个点,使两两点的距离等.求方案数\((n\leq 5000)\) 题解 我们知道三个点在树上的关系只有两种 三点共链 三点不共连 (这不是废话吗) 我们发现三点共链肯定不满足 ...

  7. 【Lintcode】136.Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. Django简单快速实现PUT、DELETE方法

    使用django的小伙伴们应该都知道我们是无法开心的处理PUT跟DELETE的 $.ajax({ url: 'XXX', type: 'PUT', dataType: 'json', data: { ...

  9. MangoDB篇章(1)

    关系型数据库遵循ACID规则 事务(transaction)4个特性:原子性(A).一致性(C).独立性(I).持久性(D) : 分布式系统(distributed system): 由多台计算机和通 ...

  10. stm32之开发入门

    一.开发环境配置 在开发stm32应用之前,我们需要先配置好开发环境. 首先从keil官网下载keil MDK-ARM软件包(v5版本与v4版本不同,v5版本需要下载额外的stm32芯片包)和芯片包( ...