题解:

树链剖分是显然的

问题在于求树链的并

比较简单的方法是

用线段树打标记覆盖,查询标记区间大小

Qlog^2n

代码:

#include <bits/stdc++.h>
using namespace std;
#define IL inline
#define rint register int
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
const int N=3e5;
const int N1=N*;
const int INF=1e9;
int n,l,cnt,dfn[N],size[N],fa[N],top[N],son[N],head[N],dep[N];
struct re{
int a,b;
}a[N*];
void arr(int x,int y)
{
a[++l].a=head[x];
a[l].b=y;
head[x]=l;
}
void dfs(int x,int y)
{
fa[x]=y; size[x]=;
int u=head[x];
dep[x]=dep[y]+;
while (u)
{
int v=a[u].b;
if (v!=y)
{
dfs(v,x);
if (size[v]>size[son[x]]) son[x]=v;
size[x]+=size[v];
}
u=a[u].a;
}
}
void dfs2(int x,int y)
{
dfn[x]=++cnt; top[x]=y;
if (!son[x]) return;
dfs2(son[x],y);
int u=head[x];
while (u)
{
int v=a[u].b;
if (v!=fa[x]&&v!=son[x]) dfs2(v,v);
u=a[u].a;
}
}
struct sgt{ int sum[N1],sum1[N1],lazy[N1],lazy1[N1];
#define mid ((h+t)/2)
IL void clear()
{
lazy1[]=-; sum1[]=;
}
IL int query()
{
return sum1[];
}
IL void down(int x,int h,int t)
{
if (lazy[x])
{
sum[x*]+=(mid-h+)*lazy[x];
sum[x*+]+=(t-mid)*lazy[x];
lazy[x*]+=lazy[x]; lazy[x*+]+=lazy[x];
lazy[x]=;
}
if (lazy1[x])
{
lazy1[x*]=lazy1[x*+]=lazy1[x];
if (lazy1[x]==) sum1[x*]=sum[x*],sum1[x*+]=sum[x*+];
else sum1[x*]=sum1[x*+]=;
lazy1[x]=;
}
}
IL void updata(int x)
{
sum[x]=sum[x*]+sum[x*+];
sum1[x]=sum1[x*]+sum1[x*+];
}
void change(int x,int h,int t,int h1,int t1,int k)
{
if (h1<=h&&t<=t1)
{
lazy[x]+=k; sum[x]+=(t-h+)*k; return;
}
down(x,h,t);
if (h1<=mid) change(x*,h,mid,h1,t1,k);
if (mid<t1) change(x*+,mid+,t,h1,t1,k);
updata(x);
}
void push(int x,int h,int t,int h1,int t1)
{
if (h1<=h&&t<=t1)
{
lazy1[x]=; sum1[x]=sum[x]; return;
}
down(x,h,t);
if (h1<=mid) push(x*,h,mid,h1,t1);
if (mid<t1) push(x*+,mid+,t,h1,t1);
updata(x);
}
}S;
void change(int x,int y)
{
int kk=dfn[x];
S.change(,,n,kk,kk+size[x]-,y);
}
void query(int x,int y)
{
int f1=top[x],f2=top[y];
while (top[x]!=top[y])
{
if (dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
S.push(,,n,dfn[f1],dfn[x]);
x=fa[f1]; f1=top[x];
}
if (dep[x]<dep[y]) swap(x,y);
S.push(,,n,dfn[y],dfn[x]);
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
ios::sync_with_stdio(false);
cin>>n;
rep(i,,n-)
{
int x,y; cin>>x>>y;
arr(x,y); arr(y,x);
}
dfs(,);
dfs2(,);
int m;
cin>>m;
rep(i,,m)
{
int kk,x,y,p;
cin>>kk;
if (!kk)
{
cin>>x>>y;
change(x,y);
} else
{
cin>>p;
rep(j,,p)
{
cin>>x>>y;
query(x,y);
}
int ans=S.query();
if (ans<) ans+=<<;
cout<<ans<<endl;
S.clear();
}
}
return ;
}

【bzoj3589】动态树 树链剖分+树链的并的更多相关文章

  1. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  2. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  3. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

  4. 4.12 省选模拟赛 LCA on tree 树链剖分 树状数组 分析答案变化量

    LINK:duoxiao OJ LCA on Tree 题目: 一道树链剖分+树状数组的神题. (直接nQ的暴力有50. 其实对于树随机的时候不难想到一个算法 对于x的修改 暴力修改到根. 对于儿子的 ...

  5. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  6. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  7. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  8. bzoj1146整体二分+树链剖分+树状数组

    其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...

  9. HDU 5044 (树链剖分+树状数组+点/边改查)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...

随机推荐

  1. Unity3D动态生成多边形

    来自https://blog.csdn.net/qq_14903317/article/details/69668521 自由绘制多边形   https://blog.csdn.net/lei_710 ...

  2. linux系统常用运维命令

    目录/文件处理命令 mkdir dirname         创建文件夹 mkdir -p /tmp/a/b         递归创建目录 rm -rf dirname         删除目录及内 ...

  3. 线性回归,逻辑回归,神经网络,SVM的总结

    目录 线性回归,逻辑回归,神经网络,SVM的总结 线性回归,逻辑回归,神经网络,SVM的总结 详细的学习笔记. markdown的公式编辑手册. 回归的含义: 回归就是指根据之前的数据预测一个准确的输 ...

  4. CSRFGuard工具介绍

    理解CSRFGuard的基础:http://www.runoob.com/jsp/jsp-tutorial.html 1:您需要做的第一件事是将OWASP.CSRFARGAD.JAR库复制到类路径中. ...

  5. T-SQL ORDER BY子句 排序方式

    MS SQL Server ORDER BY子句用于根据一个或多个列以升序或降序对数据进行排序. 默认情况下,一些数据库排序查询结果按升序排列. 语法 以下是ORDER BY子句的基本语法. SELE ...

  6. Light Oj 1003

    题意 : 给你m个二元关系, 问是否可以确定各个节点的先后关系: 思路: 拓扑排序, 判断是否有环: #include<bits/stdc++.h> using namespace std ...

  7. java8 常用函数式接口

    public static void main(String[] args) { // TODO Auto-generated method stub //函数式接口 Function<Inte ...

  8. Node.js的那些坑——如何让异步并发方法同步顺序执行(for循环+异步操作)

    1 前言 nodejs的回调,有时候真的是让人又爱又恨的,当需要用for循环把数据依次存入数据库,但是如果使用正常的for循环,永远都是最后一次值的记录,根本不符合要求. 解决此方案有几种,例如闭包( ...

  9. Day7--------------IP地址配置

    ifconfig 查看网卡 ifconfig eth0 查看网卡eth0 配置网络地址: 临时配置: ifconfig eth0 192.168.10.100 netmask 255.255.255. ...

  10. sql拼接显示table的多个列

    SELECT DeptName AS text,CONVERT(VARCHAR(10),ID)+','+DeptCode+','+ISNULL(Remark,'') AS tags,'' AS hre ...