裸题,但是因为权在边上,所以要先把边权放到这条边的子节点上,然后进行链更新/查询的时候不能更新/查询其lca。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 100001
#define BN 320
#define INF 2147483647
int fa[N],dep[N],siz[N],son[N],Num[N],tot,top[N],a[N],bw[N],n,A[N],B[N],bs[N];
int en,first[N],next[N<<1],v[N<<1],sz,NOT;
void AddEdge(const int &U,const int &V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
void dfs(int U,int Fa,int d)
{
fa[U]=Fa;
dep[U]=d;
siz[U]=1;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
dfs(v[i],U,d+1);
siz[U]+=siz[v[i]];
if(siz[v[i]]>siz[son[U]])
son[U]=v[i];
}
}
void dfs2(int U)
{
if(son[U])
{
top[son[U]]=top[U];
Num[son[U]]=++tot;
dfs2(son[U]);
}
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&v[i]!=son[U])
{
top[v[i]]=v[i];
Num[v[i]]=++tot;
dfs2(v[i]);
}
}
int SIZ[N],TOP[N];
void dfs3(int U)
{
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
if(SIZ[TOP[U]]<sz)
{
TOP[v[i]]=TOP[U];
++SIZ[TOP[U]];
}
dfs3(v[i]);
}
}
int lca(int U,int V)
{
while(U!=V)
{
if(TOP[U]!=TOP[V])
{
if(dep[TOP[U]]<dep[TOP[V]])
swap(U,V);
U=fa[TOP[U]];
}
else
{
if(dep[U]<dep[V])
swap(U,V);
U=fa[U];
}
}
return U;
}
int sum=1,num[N],l[BN],r[BN],maxv[BN],cov[BN],add[BN];
void makeblock()
{
for(;sum*sz<n;++sum)
{
l[sum]=r[sum-1]+1;
r[sum]=sum*sz;
maxv[sum]=-INF;
cov[sum]=INF;
for(int i=l[sum];i<=r[sum];++i)
{
num[i]=sum;
maxv[sum]=max(maxv[sum],a[i]);
}
}
l[sum]=r[sum-1]+1;
r[sum]=n;
maxv[sum]=-INF;
cov[sum]=INF;
for(int i=l[sum];i<=r[sum];++i)
{
num[i]=sum;
maxv[sum]=max(maxv[sum],a[i]);
}
}
void work_cov(const int &x,const int &y,const int &v)
{
for(int i=x;i<=y;++i)
a[i]=v;
}
void work_add(const int &x,const int &y,const int &v)
{
for(int i=x;i<=y;++i)
a[i]+=v;
}
void pushdown(const int &bl)
{
if(cov[bl]!=INF)
{
work_cov(l[bl],r[bl],cov[bl]);
cov[bl]=INF;
}
if(add[bl])
{
work_add(l[bl],r[bl],add[bl]);
add[bl]=0;
}
}
void calc(const int &bl)
{
maxv[bl]=-INF;
for(int i=l[bl];i<=r[bl];++i)
maxv[bl]=max(maxv[bl],a[i]);
}
void Update0(const int &x,const int &y)
{
int pl=Num[bs[x]];
pushdown(num[pl]);
a[pl]=y;
calc(num[pl]);
}
void Cov(const int &x,const int &y,const int &v)
{
pushdown(num[x]);
pushdown(num[y]);
if(num[x]==num[y])
{
work_cov(x,y,v);
calc(num[x]);
}
else
{
work_cov(x,r[num[x]],v);
calc(num[x]);
work_cov(l[num[y]],y,v);
calc(num[y]);
for(int i=num[x]+1;i<num[y];++i)
{
add[i]=0;
cov[i]=maxv[i]=v;
}
}
}
void Add(const int &x,const int &y,const int &v)
{
pushdown(num[x]);
pushdown(num[y]);
if(num[x]==num[y])
{
work_add(x,y,v);
calc(num[x]);
}
else
{
work_add(x,r[num[x]],v);
calc(num[x]);
work_add(l[num[y]],y,v);
calc(num[y]);
for(int i=num[x]+1;i<num[y];++i)
{
add[i]+=v;
maxv[i]+=v;
}
}
}
int Query(const int &x,const int &y)
{
int res=-INF;
pushdown(num[x]);
pushdown(num[y]);
if(num[x]==num[y])
for(int i=x;i<=y;++i)
res=max(res,a[i]);
else
{
for(int i=x;i<=r[num[x]];++i)
res=max(res,a[i]);
for(int i=l[num[y]];i<=y;++i)
res=max(res,a[i]);
for(int i=num[x]+1;i<num[y];++i)
res=max(res,maxv[i]);
}
return res;
}
void Change_cov(const int &L,const int &R,const int &W)
{
if(L==R&&Num[NOT]==L) return;
if(Num[NOT]==L) Cov(L+1,R,W);
else if(Num[NOT]==R) Cov(L,R-1,W);
else if(Num[NOT]>L&&Num[NOT]<R)
{
Cov(L,Num[NOT]-1,W);
Cov(Num[NOT]+1,R,W);
}
else Cov(L,R,W);
}
void Update_cov(int U,int V,const int &W)
{
int f1=top[U],f2=top[V];
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
Change_cov(Num[f1],Num[U],W);
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
Change_cov(Num[U],Num[V],W);
}
void Change_add(const int &L,const int &R,const int &W)
{
if(L==R&&Num[NOT]==L) return;
if(Num[NOT]==L) Add(L+1,R,W);
else if(Num[NOT]==R) Add(L,R-1,W);
else if(Num[NOT]>L&&Num[NOT]<R)
{
Add(L,Num[NOT]-1,W);
Add(Num[NOT]+1,R,W);
}
else Add(L,R,W);
}
void Update_add(int U,int V,const int &W)
{
int f1=top[U],f2=top[V];
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
Change_add(Num[f1],Num[U],W);
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
Change_add(Num[U],Num[V],W);
}
int Talk(const int &L,const int &R)
{
if(L==R&&Num[NOT]==L) return (-INF);
if(Num[NOT]==L) return Query(L+1,R);
if(Num[NOT]==R) return Query(L,R-1);
if(Num[NOT]>L&&Num[NOT]<R) return max(Query(L,Num[NOT]-1),Query(Num[NOT]+1,R));
return Query(L,R);
}
int Query_max(int U,int V)
{
int f1=top[U],f2=top[V],res=-INF;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
res=max(res,Talk(Num[f1],Num[U]));
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
return max(res,Talk(Num[U],Num[V]));
}
//int cnt,LL[N],RR[N];
//void dfs4(int U)
//{
// LL[U]=++cnt;
// for(int i=first[U];i;i=next[i])
// if(v[i]!=fa[U])
// dfs4(v[i]);
// RR[U]=cnt;
//}
//int Init(const int &x,const int &y)
//{
// if(y>=LL[x]&&y<=RR[x]) return x;
// if(x>=LL[y]&&x<=RR[y]) return y;
// return lca(x,y);
//}
int main()
{
// freopen("bzoj1984.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<n;++i)
{
scanf("%d%d%d",&A[i],&B[i],&bw[i]);
AddEdge(A[i],B[i]);
AddEdge(B[i],A[i]);
}
top[1]=1;
Num[1]=++tot;
dfs(1,0,1);
dfs2(1);
sz=sqrt(n); if(!sz) sz=1;
for(int i=1;i<=n;i++)
{
SIZ[i]=1;
TOP[i]=i;
}
dfs3(1);
for(int i=1;i<n;++i)
{
if(fa[A[i]]==B[i])
bs[i]=A[i];
else
bs[i]=B[i];
a[Num[bs[i]]]=bw[i];
}
a[Num[1]]=-INF;
makeblock();
// dfs4(1);
char op[7];
int x,y,z;
while(1)
{
scanf("%s",op);
if(op[0]=='S') break;
scanf("%d%d",&x,&y);
if(op[0]=='C'&&op[1]=='h') Update0(x,y);
else if(op[0]=='C'&&op[1]=='o')
{
NOT=lca(x,y);
scanf("%d",&z);
Update_cov(x,y,z);
}
else if(op[0]=='A')
{
NOT=lca(x,y);
scanf("%d",&z);
Update_add(x,y,z);
}
else
{
NOT=lca(x,y);
printf("%d\n",Query_max(x,y));
}
}
return 0;
}

【树链剖分】【分块】【最近公共祖先】【块状树】bzoj1984 月下“毛景树”的更多相关文章

  1. BZOJ1984: 月下“毛景树”

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 713  Solved: 245[Submit][Status] Descri ...

  2. [BZOJ1984]月下“毛景树”解题报告|树链剖分

    Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的“毛景树” ...

  3. [bzoj1984]月下“毛景树”

    Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园.毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的" ...

  4. 2018.10.27 bzoj1984: 月下“毛景树”(树链剖分)

    传送门 唉蒟蒻又退化了,这道sb题居然做了20min,最后发现是updcovupdcovupdcov写成了updaddupdaddupdadd我还能说什么233233233 就是让你转边权为点权之后, ...

  5. BZOJ 1984: 月下“毛景树” [树链剖分 边权]

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1728  Solved: 531[Submit][Status][Discu ...

  6. 【BZOJ-1984】月下“毛景树” 树链剖分

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1314  Solved: 416[Submit][Status][Discu ...

  7. Bzoj 1984: 月下“毛景树” 树链剖分

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1282  Solved: 410[Submit][Status][Discu ...

  8. P4315 月下“毛景树”(树链剖分)

    P4315 月下"毛景树"(树链剖分) 题面 简述: 边权转点权(在dfs1处转换) 把一条边权赋值在深度更深的上 需要实现对单边权的染色 , 路径边权的染色 , 路径边权的增加 ...

  9. 【BZOJ1984】月下“毛景树” 树链剖分+线段树

    [BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...

随机推荐

  1. Ubuntu修改grub启动顺序和启动时间

    sudo gedit /boot/grub/grub.cfg,输入密码,在弹出的文件中找到set default = "0",想要改为第N项默认就把0改成N-1 看到启动界面是第几 ...

  2. JavaScript 被忽视的细节

    语句/表达式 换个角度理解语句(statemaents)和表达式(expressions):表达式不会改变程序的运行状态,而语句会.还有一种叫做表达式语句,可以理解为表达式和语句的交集,如({a:1} ...

  3. PRINT_TABLE 列以行形式显示

    在sqlplus,如果列比较多,往往会显示不够清晰,这时如果能把查询语句行转列就明了多了,在网上看到print_table存储过程:里面所设置的日期格式,可根据自己习惯修改 CREATE OR REP ...

  4. 移动端去掉a标签点击时出现的背景

    之前做移动端的Portal时,手机上测试,点击a标签总是出现一个背景框 在CSS中添加 -webkit-tap-highlight-color: rgba(0, 0, 0, 0);就可以了 a:act ...

  5. 关于跨域策略文件crossdomain.xml文件--配置实例

    转载自:http://bbs.phpchina.com/blog-52440-191623.html 我一直不太明白crossdomain.xml文件是干嘛用的,今天总算比较清楚的知道了一下. 这是F ...

  6. maven工程开启jetty调试

    转摘自:http://czj4451.iteye.com/blog/1942437 准备工作: a. 在pom.xml中配置jetty插件: <plugins> <plugin> ...

  7. Spring学习--xml 中 Bean 的自动装配

    Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...

  8. javascript中Date使用总结(转)

    //全局函数 Date //Date 类的静态方法 Date.parse Date.UTC //Date 对象的建立方法 new Date() new Date(毫秒数) new Date(标准时间格 ...

  9. 100个Swift必备Tips(第二版)

    100个Swift必备Tips(第二版) 新年第一天,给大家一本电子书,希望新的一年里,步步高升. GitHub

  10. Csharp 非安全代码

    using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { c ...