题意

\(n\) 个节点的树,每个点有点权,\(m\) 次操作,操作分两种,修改一个节点的点权,对于一个 \((u,v)\) ,询问 \(\displaystyle\sum_{i=1}^n\sum_{j=1}^n f(i,j)\) 的值,其中如果路径 \((i,j)\) 与路径 \((u,v)\) 有公共点,\(f(i,j)=w_iw_j\)( \(w_i\) 表示节点 \(i\) 的点权),否则 \(f(i,j)=0\) 。

\(1\leq n,m \leq 10^5​\)

思路

先讲一下用 \(\text{LCT}​\) 维护子树信息的写法(以子树和为例)。

我们需要一个两个数组维护和,\(sum,isum\) 前者表示子树和,后者表示虚儿子的和。

\(\text{push_up}\) 函数需要这么写

void push_up(int x){sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+isum[x]+pw[x];}

在连断虚实边时,要注意 \(isum\) 的变化。

void access(int x)
{
for(int y=0;x;y=x,x=fa[x])
splay(x),isum[x]+=-sum[y]+sum[ch[x][1]],ch[x][1]=y,push_up(x);
//其实这里可以不用push_up(x),因为sum值没有变化
}

\(\text{link}\) 函数这样写,因为需要维护子树和,所以两个点直接转到根。

void link(int x,int y)
{
make_root(x),make_root(y);
fa[x]=y;
isum[y]+=sum[x];
push_up(y);
}

\(\text{cut}​\) 函数同理

void cut(int x,int y)
{
make_root(x),access(y),splay(x);
ch[x][1]=fa[y]=0;
push_up(x);
}

正难则反,我们用总的路径 \((i,j)\) 的答案减去没有公共点的路径 \((i,j)\) 的答案。

总的路径非常好求,就是 \(\displaystyle\sum_{i=1}^n\displaystyle\sum_{j=1}^nw_iw_j= (\sum_{i=1}^n w_i)^2​\) 。

为了求没有交点的路径,我们不妨利用 \(\text{LCT}\) 将这条路径的一个端点拎到根,那对与询问 \((u,v)\) ,答案就是这个式子:

\[(\sum_{i=1}^n w_i)^2-\sum_{p\in \text{Path}(u,v)} \sum_{q\in \text{son}(p)\text{且}q\not\in{\text{Path}(u,v)}}sum_q^2
\]

其中 \(sum_i\) 表示子树和,我们需要用上面的方法去维护它。不难发现,实化路径 \((u,v)\) 后,上面的 \(q\) 其实就是虚儿子的 \(sum\) 平方再求和,那用类似的方法进行维护。然后再对这个东西再求一次路径和即可。最终用总的去减就是最终答案了。

代码

#include<bits/stdc++.h>
#define FOR(i,x,y) for(int i=(x),i##END=(y);i<=i##END;++i)
#define DOR(i,x,y) for(int i=(x),i##END=(y);i>=i##END;--i)
template<typename T,typename _T>inline bool chk_min(T &x,const _T y){return y<x?x=y,1:0;}
template<typename T,typename _T>inline bool chk_max(T &x,const _T y){return x<y?x=y,1:0;}
typedef long long ll;
const int N=1e5+5;
const int P=1e9+7;
int ch[N][2],fa[N];bool rev[N];
int stk[N],tp;
int pw[N],sum[N],isum[N],Sum[N],ans[N];
int n,m; void create(int x,int val)
{
ch[x][0]=ch[x][1]=fa[x]=rev[x]=0;
pw[x]=sum[x]=val;
isum[x]=Sum[x]=ans[x]=0;
}
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void reved(int x)
{
std::swap(ch[x][0],ch[x][1]);
rev[x]^=1;
}
void push_up(int x)
{
sum[x]=((ll)sum[ch[x][0]]+sum[ch[x][1]]+pw[x]+isum[x])%P;
ans[x]=((ll)ans[ch[x][0]]+ans[ch[x][1]]+Sum[x])%P;
}
void push_down(int x)
{
if(rev[x])
{
if(ch[x][0])reved(ch[x][0]);
if(ch[x][1])reved(ch[x][1]);
rev[x]=0;
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],k=(x==ch[y][1]);
if(!isroot(y))ch[z][y==ch[z][1]]=x; fa[x]=z;
ch[y][k]=ch[x][!k]; if(ch[x][!k])fa[ch[x][!k]]=y;
ch[x][!k]=y,fa[y]=x;
push_up(y),push_up(x);
}
void splay(int x)
{
stk[tp=1]=x;
for(int y=x;!isroot(y);y=fa[y])stk[++tp]=fa[y];
while(stk[tp])push_down(stk[tp]),tp--;
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))(x==ch[y][1])==(y==ch[z][1])?rotate(y):rotate(x);
rotate(x);
}
}
void access(int x)
{
for(int y=0;x;y=x,x=fa[x])
{
splay(x);
(isum[x]+=(-(ll)sum[y]+sum[ch[x][1]])%P)%=P;
(Sum[x]+=(-(ll)sum[y]*sum[y]%P+(ll)sum[ch[x][1]]*sum[ch[x][1]])%P)%=P;
ch[x][1]=y;
push_up(x);
}
}
void make_root(int x)
{
access(x),splay(x),reved(x);
}
int get_root(int x)
{
access(x),splay(x);
while(ch[x][0])push_down(x),x=ch[x][0];
splay(x);
return x;
}
void link(int x,int y)
{
make_root(x),make_root(y);
fa[x]=y;
(isum[y]+=sum[x])%=P;
(Sum[y]+=(ll)sum[x]*sum[x]%P)%=P;
push_up(y);
}
void lift(int x,int y)
{
make_root(x),access(y),splay(x);
}
void update(int x,int val)
{
lift(x,x);
pw[x]=val;
push_up(x);
}
int query(int x,int y)
{
lift(x,y);
return (((ll)sum[x]*sum[x]%P-ans[x])%P+P)%P;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
FOR(i,1,n)
{
int x;
scanf("%d",&x);
create(i,x);
}
FOR(i,1,n-1)
{
int u,v;
scanf("%d%d",&u,&v);
link(u,v);
}
while(m--)
{
int op,u,v;
scanf("%d%d%d",&op,&u,&v);
if(op==1)update(u,v);
else if(op==2)printf("%d\n",query(u,v));
}
}
return 0;
}

HDU 5405 Sometimes Naive(动态树)的更多相关文章

  1. 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权.        要求支持两种操作: 操作1:更改某个节点的 ...

  2. HDU 5405 Sometimes Naive 树链剖分+bit*****

    Sometimes Naive Problem Description   Rhason Cheung had a naive problem, and asked Teacher Mai for h ...

  3. HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...

  4. HDU 2475 BOX 动态树 Link-Cut Tree

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem De ...

  5. hdu 5398 动态树LCT

    GCD Tree Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  6. hdu 5002 (动态树lct)

    Tree Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  7. hdu 5314 动态树

    Happy King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tot ...

  8. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

  9. HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

    lca的做法还是非常明显的.简单粗暴, 只是不是正解.假设树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的.预计就是放在splay上剖分一 ...

随机推荐

  1. 矩阵取数问题(dp,高精)

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n×mn \times mn×m的矩阵,矩阵中的每个元素ai,ja_{i,j}ai,j​均为非负整数.游戏规则如下: 每次取数时须从每行各取走 ...

  2. js获取谷歌浏览器版本 和 js分辨不同浏览器

    // 获取谷歌版本 function getChromeVersion() { var arr = navigator.userAgent.split(' '); var chromeVersion ...

  3. WebH

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  4. [Database.System.Concepts(6th.Edition.2010)].Abraham.Silberschatz. Ch8学习笔记

    Database Ch8.relational design 8.1 features of good design 8.1.1 larger alternatives why design is g ...

  5. post body 传输参数

    postman 示例: 请求地址:http://member-system-api.dd01.work/api/inApp 设置headers头:Content-Type       applicat ...

  6. 6.1-uC/OS-III软件定时器

    1.软件定时器是 uC/OS 操作系统的一个内核对象,软件定时器是基于时钟节拍和系统管理创建的软件性定时器,理论上可以创建无限多个,但精准度肯定比硬件定时稍逊一筹. 2.软件定时器启动之后是由软件定时 ...

  7. Cartographer源码阅读(9):图优化的前端——闭环检测

    约束计算 闭环检测的策略:搜索闭环,通过匹配检测是否是闭环,采用了分支定界法. 前已经述及PoseGraph的内容,此处继续.位姿图类定义了pose_graph::ConstraintBuilder ...

  8. solr6.5.1搜索引擎的部署

    目录结构如下: 6.5.1版本的solr已经集成有jetty服务器(在server目录下),所以可以直接启动solr应用. 1.java环境配置好(这里不再累赘). 2.打开cmd,路径切换到bin目 ...

  9. 关于 ionic3 tabs 导航ico 点击 页面返回顶部

    类似微信 双击 页面返回顶部功能,ionic3 中有一个 Content. 将 import { Content } from 'ionic-angular'; 放入想要实现此功能的 ts中. 实例化 ...

  10. 家庭记账本之微信小程序(八)

    寒假总结 寒假充满着腥风血雨,不过在努力下还是完成了寒假的任务,虽说没有出去找活干,毕竟在寒假这段时间不怎么好找,但是我在自己家的店里帮这父母工作了一段时间,也算是颇有收获,在短暂的学习后也算勉强完成 ...