树链剖分X2
1.ZJOI树的统计
板子题 因为初始化没打改了几个小时 改到双腿软着出的机房(身体素质感人
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define maxn 300000
#define ls x<<1
#define rs x<<1|1
using namespace std;
,v[maxn],top[maxn],son[maxn],fa[maxn],de[maxn],w[maxn],z,tree1[maxn],tree2[maxn],sz[maxn];
];
struct eg
{
int to;
int nxt;
}b[maxn];
void link(int x,int y)
{
b[++tot].nxt=head[x];
b[tot].to=y;
head[x]=tot;
}
void dfs1(int x,int f)
{
sz[x]=,fa[x]=f;
;i=b[i].nxt)
{
int t=b[i].to;
if (t==f) continue;
de[t]=de[x]+;
dfs1(t,x);
sz[x]+=sz[t];
||sz[son[x]]<sz[t]) son[x]=t;
}
}
void dfs2(int x,int tp)
{
top[x]=tp,w[x]=++z;
) dfs2(son[x],tp);
else return ;
;i=b[i].nxt)
{
int t=b[i].to;
if (t!=fa[x]&&t!=son[x]) dfs2(t,t);
}
}
void build(int x,int l,int r)
{
tree2[x]=,tree1[x]=-;
if (l==r) return ;
;
build (ls,l,mid);
build (rs,mid+,r);
}
void updata(int x,int l,int r,int d,int y)
{
if (l==r&&l==d)
{
tree1[x]=y;
tree2[x]=y;
return ;
}
;
if (d<=mid)updata(ls,l,mid,d,y);
,r,d,y);
tree1[x]=max(tree1[ls],tree1[rs]);
tree2[x]=tree2[ls]+tree2[rs];
}
int qsum(int x,int l,int r,int ll,int rr)
{
if (ll==l&&r==rr) return tree2[x];
;
,r,ll,rr);
else if (rr<=mid) return qsum(ls,l,mid,ll,rr);
,r,mid+,rr);
}
int qmax(int x,int l,int r,int ll,int rr)
{
if (ll==l&&r==rr) return tree1[x];
;
if (rr<=mid) return qmax(ls,l,mid,ll,rr);
,r,ll,rr);
,r,mid+,rr));
}
int find(int x,int y,int flag)
{
)
{
;
int f1=top[x],f2=top[y];
while(f1!=f2)
{
if(de[f1]<de[f2]) swap(f1,f2),swap(x,y);
ans=max(ans,qmax(,,z,w[f1],w[x]));
x=fa[f1],f1=top[x];
}
if (de[x]>de[y]) swap(x,y);
,,z,w[x],w[y]));
}
)
{
;
int f1=top[x],f2=top[y];
while (f1!=f2)
{
if(de[f1]<de[f2]) swap(f1,f2),swap(x,y);
ans+=qsum(,,z,w[f1],w[x]);
x=fa[f1],f1=top[x];
}
if (de[x]>de[y]) swap(x,y);
,,z,w[x],w[y]);
}
}
int main()
{
memset(head, -, sizeof(head));
memset(son, -, sizeof(son));
scanf ("%d",&n);
;i<n;++i)
{
int x,y;
scanf ("%d%d",&x,&y);
link(x,y);
link(y,x);
}
;i<=n;++i)
scanf ("%d",&v[i]);
de[]=;
dfs1(,);
dfs2(,);
build(,,z);
;i<=n;++i) updata(,,z,w[i],v[i]);
int q;
scanf("%d",&q);
;i<=q;++i)
{
scanf ("%s",s);
int x,y;
scanf ("%d%d",&x,&y);
]=='C')
updata(,,z,w[x],y);
]=='M')
printf());
]=='S')
printf());
}
;
}
2.HAOI树上操作
这一次是被数组大小给困住了 好不容易反应过来开大了tree的大小结果我没有开大lazy的??(黑人问号
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 100002
#define LL long long
#define ls x<<1
#define rs x<<1|1
using namespace std;
,df=;
*N],to[*N],nxt[*N],dfn[N];
int sz[N],tp[N],son[N],de[N],fa[N],ed[N];
LL tree[*N],v[N],lazy[*N];
void link(int x,int y)
{
nxt[++tot]=head[x];
to[tot]=y;
head[x]=tot;
}
void dfs(int x)
{
sz[x]=;
for (int i=head[x];i;i=nxt[i])
{
int t=to[i];
if (t==fa[x]) continue;
de[t]=de[x]+;
fa[t]=x;
dfs(t);
sz[x]+=sz[t];
if (sz[t]>sz[son[x]]) son[x]=t;
}
}
void dfs1(int x,int top)
{
dfn[x]=++df,tp[x]=top;
if (son[x]) dfs1(son[x],top);
for (int i=head[x];i;i=nxt[i])
{
int t=to[i];
if (t==fa[x]||t==son[x]) continue;
dfs1(t,t);
}
ed[x]=df;
}
void down (int x,int l,int r)
{
;
tree[ls]+=lazy[x]*(mid-l+);
tree[rs]+=lazy[x]*(r-mid);
lazy[ls]+=lazy[x];
lazy[rs]+=lazy[x];
lazy[x]=;
}
void update(int x,int l,int r,int ll,int rr,LL w)
{
if (l!=r) down(x,l,r);
if (ll<=l&&r<=rr)
{
tree[x]+=w*(r-l+);
lazy[x]=w;
return ;
}
;
if (ll<=mid) update(ls,l,mid,ll,rr,w);
,r,ll,rr,w);
tree[x]=tree[ls]+tree[rs];
}
LL query(int x,int l,int r,int ll,int rr)
{
if (l!=r) down(x,l,r);
if (ll<=l&&r<=rr) return tree[x];
;
LL ans=;
if (ll<=mid) ans+=query(ls,l,mid,ll,rr);
,r,ll,rr);
return ans;
}
LL lca(int x)
{
LL ans=;
)
{
ans+=query(,,n,dfn[tp[x]],dfn[x]);
x=fa[tp[x]];
}
ans+=query(,,n,,dfn[x]);
return ans;
}
int main()
{
scanf ("%d%d",&n,&m);
;i<=n;++i) scanf ("%lld",&v[i]);
;i<n;++i)
{
int x,y;
scanf ("%d%d",&x,&y);
link(x,y);
link(y,x);
}
de[]=;
dfs(),dfs1(,);
;i<=n;++i) update(,,n,dfn[i],dfn[i],v[i]);
;i<=m;++i)
{
//cout<<query(1,1,n,dfn[2],dfn[2])<<"**"<<endl;
;
scanf ("%d",&fl);
)
{
int x,a;
scanf ("%d%d",&x,&a);
update(,,n,dfn[x],dfn[x],a);
}
)
{
int x,a;
scanf ("%d%d",&x,&a);
update(,,n,dfn[x],ed[x],a);
}
)
{
int x;
scanf ("%d",&x);
printf("%lld\n",lca(x));
}
}
;
}
最后再带上第一次在考试中(又是基本没学的状态下考的)遇到的树剖题
qtree系列中的一道
然而这个是强行lca做的 效果好像还可以啊
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 50000
using namespace std;
,he[maxn],nxt[maxn],to[maxn],di[maxn],de[maxn],dis[maxn];
bool vis[maxn];
];
];
void link(int x,int y,int dis)
{
nxt[++tot]=he[x];
to[tot]=y;
di[tot]=dis;
he[x]=tot;
}
void dfs(int u)
{
vis[u]=;
for (int i=he[u];i;i=nxt[i])
{
int t=to[i];
if (vis[t]) continue;
f[t][]=u;
de[t]=de[u]+;
dis[t]=di[i];
dfs(t);
}
}
void ff()
{
;i<=));++i)
;j<=n;++j)
f[j][i]=f[f[j][i-]][i-];
}
int lca(int x,int y)
{
;
if (de[x]<de[y]) swap(x,y);
int q=de[x]-de[y];
,yy;
;(<<i)<=n;++i)
<<i)) x=f[x][i];
])
ans=max(ans,dis[i]);
//cout<<ans<<"&&"<<endl;
if (x==y) return ans;
xx=x,yy=y;
));i>=;i--)
{
if (f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
}
//cout<<x<<" "<<y<<"**"<<endl;
];i=f[i][])
ans=max(ans,dis[i]);
];i=f[i][])
ans=max(ans,dis[i]);
return ans;
}
int main()
{
scanf ("%d",&n);
;i<n;++i)
{
int x,y,r;
scanf ("%d%d%d",&x,&y,&r);
link(x,y,r);
link(y,x,r);
}
de[]=;
dfs();
ff();
)
{
scanf ("%s",s);
]=='D') break;
]=='C')
{
int x,y;
scanf("%d%d",&x,&y);
*x],yy=to[*x-];
//cout<<xx<<" "<<yy<<"&&"<<endl;
]==yy) dis[xx]=y;
]==xx) dis[yy]=y;
}
]=='Q')
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",lca(x,y));
}
}
;
}
树链剖分X2的更多相关文章
- 【BZOJ-3553】三叉神经树 树链剖分
3553: [Shoi2014]三叉神经树 Time Limit: 160 Sec Memory Limit: 256 MBSubmit: 347 Solved: 112[Submit][Stat ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
D. Happy Tree Party Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- 3553: [Shoi2014]三叉神经树(树链剖分)
这道题特别恶心,首先我们可以发现更改的就是出现连续的一或二,那么就用线段树+树链剖分找到这个范围 想到是不难想,就是打起来恶心罢了= = CODE: #include<cstdio> #i ...
- D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】
Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- codevs 1228 苹果树 树链剖分讲解
题目:codevs 1228 苹果树 链接:http://codevs.cn/problem/1228/ 看了这么多树链剖分的解释,几个小时后总算把树链剖分弄懂了. 树链剖分的功能:快速修改,查询树上 ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
随机推荐
- window.location.href跳转至空白页
现象:window.location.href = "XXX"调到了空白页,但是将XXX在窗口地址栏输入就会可以访问到. 原因:就是XXX前缀没有加上"http://&q ...
- 树莓派远程桌面配置-开机自启SSH
必须先安装tightvncserver sudo apt-get install tightvncserver 再安装xrdp服务. sudo apt-get install xrdp 如果开着防火墙 ...
- Java基础回顾(3)
数组:用一种数据类型的集合 ★数组元素下标从0开始. 数组的复制.扩容: ①.System.arraycopy(源数组, 源数组的初始下标, 目标数组, 目标数 ...
- poj2485 highwaysC语言编写
/*HighwaysTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 33595Accepted: 15194DescriptionTh ...
- 自己动手实现mvc框架
用过springmvc的可能都知道,要集成springmvc需要在web.xml中加入一个跟随web容器启动的DispatcherServlet,然后由该servlet初始化一些东西,然后所有的web ...
- TensorFlow —— Demo
import tensorflow as tf g = tf.Graph() # 创建一个Graph对象 在模型中有两个"全局"风格的Variable对象:global_step ...
- web前端素材整理汇总
最近一直搞前端开发,整理下前端用的一些常用素材,分享给大家 框架类 Vue:https://cn.vuejs.org/ iview:https://www.iviewui.com/ 插件类 Jquer ...
- BigDecimal工具类处理精度计算
/** * Created by My_coder on 2017-07-27. * 加减乘除计算工具类 */ public class BigDecimalUtil { private BigDec ...
- OI大佬博客集
郭家宝:byvoid 早年大牛,题解更新到2014年,其博客内容非常丰富,不止题解 hzwer:hzwer 从OI到ACM,一直在更新 陈立杰:WJMZBMR 无需解释,不过貌似只更到2015 顾森 ...
- django同时查询两张表的数据,合并检索对象返回
原始需求: 1.一篇文章内容分N个版块,每篇文章的版块数量不同. 2.有个文章搜索功能,需要同时搜索标题和内容. 实现思路: 1.由于每篇文章的内容版块数量不同,因此将每个文章的标题和内容分开存入2张 ...