BZOJ3589动态树
**错误改了一上午。
先做熟练泼粪
k<=5,因此我们可以模拟这个过程,在线段树上把标记建出来然后pushup时候更新就好了。
By:大奕哥
#include<bits/stdc++.h>
using namespace std;
const int N=;
struct tree{
int l,lz,ll;long long s,ret;
}t[N<<];
int head[N],cnt,n,id,bel[N],pos[N],size[N],d[N],f[N],son[N],ed[N];
struct node{
int to,nex;
}e[N<<];
void add(int x,int y)
{
e[++cnt].to=y;e[cnt].nex=head[x];head[x]=cnt;
}
void dfs(int x)
{
size[x]=;
for(int i=head[x];i;i=e[i].nex)
{
int y=e[i].to;
if(y==f[x])continue;
d[y]=d[x]+;f[y]=x;
dfs(y);
if(size[y]>size[son[x]])son[x]=y;
size[x]+=size[y];
}
return;
}
void dfs2(int x,int chain)
{
pos[x]=++id;bel[x]=chain;
if(son[x])dfs2(son[x],chain);
for(int i=head[x];i;i=e[i].nex)
{
int y=e[i].to;
if(y==f[x]||y==son[x])continue;
dfs2(y,y);
}
ed[x]=id;
return;
}
void update(int x,int z)
{
t[x].lz+=z;
t[x].s+=z*t[x].l;
}
void update2(int x,int z)
{
t[x].ll=z;
t[x].ret=t[x].s*z;
}
void pushdown(int x)
{
if(t[x].lz)
{
update(x<<,t[x].lz);
update(x<<|,t[x].lz);
t[x].lz=;
}
if(t[x].ll!=-)
{
update2(x<<,t[x].ll);
update2(x<<|,t[x].ll);
t[x].ll=-;
}
return;
}
void pushup(int x)
{
t[x].s=t[x<<].s+t[x<<|].s;
t[x].ret=t[x<<].ret+t[x<<|].ret;
}
void build(int x,int l,int r)
{
t[x].ll=-;
if(l==r){
t[x].l=;return;
}
int mid=l+r>>;
t[x].l=r-l+;
build(x<<,l,mid);build(x<<|,mid+,r);
}
void paint(int x,int l,int r,int L,int R,int w)
{
if(t[x].ll==w)return;
if(l==L&&r==R){
update2(x,w);
return;
}
pushdown(x);
int mid=l+r>>;
if(mid<L)paint(x<<|,mid+,r,L,R,w);
else if(mid>=R)paint(x<<,l,mid,L,R,w);
else paint(x<<,l,mid,L,mid,w),paint(x<<|,mid+,r,mid+,R,w);
pushup(x);
return;
}
void change(int x,int l,int r,int L,int R,int c)
{
if(l==L&&r==R)
{
update(x,c);
return;
}
pushdown(x);
int mid=l+r>>;
if(mid<L)change(x<<|,mid+,r,L,R,c);
else if(mid>=R)change(x<<,l,mid,L,R,c);
else change(x<<,l,mid,L,mid,c),change(x<<|,mid+,r,mid+,R,c);
pushup(x);
}
void color(int x,int y,int c)
{
while(bel[x]!=bel[y])
{
if(d[bel[x]]<d[bel[y]])swap(x,y);
paint(,,n,pos[bel[x]],pos[x],c);
x=f[bel[x]];
}
if(d[x]<d[y])swap(x,y);
paint(,,n,pos[y],pos[x],c);
return;
}
int main()
{
int x,y,q,ff,k;
scanf("%d",&n);
for(int i=;i<n;++i)
{
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs();dfs2(,);
build(,,n);
scanf("%d",&q);
for(int i=;i<=q;++i)
{
scanf("%d",&ff);
if(ff==)
{
scanf("%d%d",&x,&y);
change(,,n,pos[x],ed[x],y);
}
else{
scanf("%d",&k);
for(int j=;j<=k;++j)
{
scanf("%d%d",&x,&y);
color(x,y,);
}
printf("%d\n",t[].ret&((1ll<<)-));
update2(,);
}
}
return ;
}
BZOJ3589动态树的更多相关文章
- bzoj3589 动态树 求链并 容斥
bzoj3589 动态树 链接 bzoj 思路 求链并. 发现只有最多5条链子,可以容斥. 链交求法:链顶是两条链顶深度大的那个,链底是两个链底的\(lca\) 如果链底深度小于链顶,就说明两条链没有 ...
- [树链剖分]BZOJ3589动态树
题目描述 别忘了这是一棵动态树, 每时每刻都是动态的. 小明要求你在这棵树上维护两种事件 事件0: 这棵树长出了一些果子, 即某个子树中的每个节点都会长出K个果子. 事件1: 小明希望你求出几条树枝上 ...
- BZOJ3589 动态树(树链剖分+容斥原理)
显然容斥后转化为求树链的交.这个题非常良心的保证了查询的路径都是到祖先的,求交就很休闲了. #include<iostream> #include<cstdio> #inclu ...
- BZOJ3589 : 动态树
对于既要支持子树修改又要支持链查询, 需要树链剖分 然后求出DFS序,DFS的时候先DFS重儿子, 然后子树是1个区间,链是$O(\log n)$个区间 这道题对于查询若干条链的并: 由于K<= ...
- bzoj千题计划214:bzoj3589: 动态树
http://www.lydsy.com/JudgeOnline/problem.php?id=3589 树链剖分 用线段数维护扫描线的方式来写,标记只打不下传 #include<cstdio& ...
- BZOJ3589 动态树[树剖/暴力/容斥]
操作0,显然直接线段树解决. 操作1,瓶颈在于重叠的链只算一次.在线段树上来看,如果一个区间被覆盖了,那么只算这个区间,子树里面也就不管了. 考虑对节点打标记来表示是否覆盖.但是,如果统一打完之后,并 ...
- bzoj3589 动态树 树链剖分+容斥
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3589 题解 事件 \(0\) 不需要说,直接做就可以了. 事件 \(1\) 的话,考虑如果直接 ...
- 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)
3589: 动态树 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 405 Solved: 137[Submit][Status][Discuss] ...
- 【BZOJ3589】动态树 树链剖分+线段树
Description 别忘了这是一棵动态树, 每时每刻都是动态的. 小明要求你在这棵树上维护两种事件 事件0: 这棵树长出了一些果子, 即某个子树中的每个节点都会长出K个果子. 事件1: 小明希望你 ...
随机推荐
- Linux SSH Backdoor分析排查
1.SSH后门分类 SSH后门方式有以下几种 软链接 SSH Server wrapper SSH Keylogger 2.软链接 利用方法 [root@helen]# ln -sf /usr/sbi ...
- FPGA设计方法检查表
-----------------------摘自<FPGA软件测试与评价技术> 中国电子信息产业发展研究院 | 编著------------------------------- 文本格 ...
- openjudge-NOI 2.5-1756 八皇后
题目链接:http://noi.openjudge.cn/ch0205/1756/ 题解: 上一道题稍作改动…… #include<cstdio> #include<algorith ...
- python网络编程-Select\Poll\Epoll异步IO
首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...
- js权威指南---学习笔记01
1.当函数赋值给对象的属性时,就变为了方法:2.被零整除不报错,只会返回无穷大(Infinity)或者负无穷大.例外:零除以零等于非数字(NaN).3.NaN与任何值都不相等! 4.Javascrip ...
- 虚拟机 ubuntu 16.04
下载地址:https://www.ubuntu.com/download/desktop 使用虚拟机直接安装
- No.5 selenium学习之路之多窗口句柄
多窗口相关操作 获取当前句柄 c_handle = driver.current_window_handle 获取所有句柄 all_handle = driver.window_handles 切换到 ...
- 获取随机字符串的方法 GetRandomString
方法1:推荐方便. System.Hash 单元 Memo1.Lines.Add(THash.GetRandomString(50)); 方法二(自己写的): function TStrApi.Sui ...
- CSDN博客专家申请成功
又一个值得纪念的日子,上周六申请CSDN博客专家,今天中午审批通过.使用CSDN好几年了,从未想到能把博客一步步的写到这个地步. 曾经,写过一段博客,只是为了记录和分享.中间由于工作的变动和繁忙中断了 ...
- jenkins Error performing command: git ls-remote -h
Jenkins新建项目中源码管理使用Git时遇到如下问题: Failed to connect to repository : Error performing command: git ls-rem ...