2018.10.27 bzoj1984: 月下“毛景树”(树链剖分)
传送门
唉蒟蒻又退化了,这道sb题居然做了20min,最后发现是updcovupdcovupdcov写成了updaddupdaddupdadd我还能说什么233233233
就是让你转边权为点权之后,支持树上路径覆盖,单点覆盖,路径加,求路径最大。
直接乱码一发居然AAA了?
贴代码了(稍微有点长):
#include<bits/stdc++.h>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
const int N=1e5+5;
int n,m,first[N],top[N],dep[N],fa[N],siz[N],hson[N],pred[N],num[N],a[N],cnt=0,tot=0;
struct edge{int v,next,w;}e[N<<1];
inline void add(int u,int v,int w){e[++cnt].v=v,e[cnt].w=w,e[cnt].next=first[u],first[u]=cnt;}
void dfs1(int p){
siz[p]=1;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(v==fa[p])continue;
fa[v]=p,dep[v]=dep[p]+1,a[v]=e[i].w,dfs1(v),siz[p]+=siz[v];
if(siz[v]>siz[hson[p]])hson[p]=v;
}
}
void dfs2(int p,int tp){
top[p]=tp,pred[num[p]=++tot]=p;
if(!hson[p])return;
dfs2(hson[p],tp);
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(v!=fa[p]&&v!=hson[p])dfs2(v,v);
}
}
struct Node{int l,r,mx,cov,add;}T[N<<2];
inline void pushup(int p){T[p].mx=max(T[lc].mx,T[rc].mx);}
inline void pushadd(int p,int v){T[p].mx+=v,T[p].add+=v;}
inline void pushcov(int p,int v){T[p].mx=v,T[p].cov=v,T[p].add=0;}
inline void pushdown(int p){
if(~T[p].cov)pushcov(lc,T[p].cov),pushcov(rc,T[p].cov),T[p].cov=-1;
if(T[p].add)pushadd(lc,T[p].add),pushadd(rc,T[p].add),T[p].add=0;
}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r,T[p].cov=-1;
if(l==r){T[p].mx=a[pred[l]];return;}
build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void updadd(int p,int ql,int qr,int v){
if(ql>T[p].r||qr<T[p].l)return;
if(ql<=T[p].l&&T[p].r<=qr)return pushadd(p,v);
pushdown(p);
if(qr<=mid)updadd(lc,ql,qr,v);
else if(ql>mid)updadd(rc,ql,qr,v);
else updadd(lc,ql,mid,v),updadd(rc,mid+1,qr,v);
pushup(p);
}
inline void updcov(int p,int ql,int qr,int v){
if(ql>T[p].r||qr<T[p].l)return;
if(ql<=T[p].l&&T[p].r<=qr)return pushcov(p,v);
pushdown(p);
if(qr<=mid)updcov(lc,ql,qr,v);
else if(ql>mid)updcov(rc,ql,qr,v);
else updcov(lc,ql,mid,v),updcov(rc,mid+1,qr,v);
pushup(p);
}
inline int query(int p,int ql,int qr){
if(ql>T[p].r||qr<T[p].l)return 0;
if(ql<=T[p].l&&T[p].r<=qr)return T[p].mx;
pushdown(p);
if(qr<=mid)return query(lc,ql,qr);
if(ql>mid)return query(rc,ql,qr);
return max(query(lc,ql,mid),query(rc,mid+1,qr));
}
inline void changeadd(int x,int y,int v){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
updadd(1,num[top[x]],num[x],v),x=fa[top[x]];
}
if(dep[x]<dep[y])swap(x,y);
updadd(1,num[y]+1,num[x],v);
}
inline void changecov(int x,int y,int v){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
updcov(1,num[top[x]],num[x],v),x=fa[top[x]];
}
if(dep[x]<dep[y])swap(x,y);
updcov(1,num[y]+1,num[x],v);
}
inline int ask(int x,int y){
int ret=0;
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
ret=max(ret,query(1,num[top[x]],num[x])),x=fa[top[x]];
}
if(dep[x]<dep[y])swap(x,y);
return max(ret,query(1,num[y]+1,num[x]));
}
int main(){
n=read();
for(int u,v,w,i=1;i<n;++i)u=read(),v=read(),w=read(),add(u,v,w),add(v,u,w);
dfs1(1),dfs2(1,1),build(1,1,n);
char s[10];
while(1){
scanf("%s",s);
if(s[0]=='S')break;
int x=read(),y=read();
if(s[0]=='M')printf("%d\n",ask(x,y));
if(s[0]=='C'&&s[1]=='o')changecov(x,y,read());
if(s[0]=='A')changeadd(x,y,read());
if(s[0]=='C'&&s[1]=='h'){
int u=e[x*2].v,v=e[x*2-1].v;
if(dep[u]>dep[v])swap(u,v);
updcov(1,num[v],num[v],y);
}
}
return 0;
}
2018.10.27 bzoj1984: 月下“毛景树”(树链剖分)的更多相关文章
- BZOJ1984: 月下“毛景树”
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 713 Solved: 245[Submit][Status] Descri ...
- [bzoj1984]月下“毛景树”
Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园.毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的" ...
- [BZOJ1984]月下“毛景树”解题报告|树链剖分
Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的“毛景树” ...
- 【树链剖分】【分块】【最近公共祖先】【块状树】bzoj1984 月下“毛景树”
裸题,但是因为权在边上,所以要先把边权放到这条边的子节点上,然后进行链更新/查询的时候不能更新/查询其lca. #include<cstdio> #include<cmath> ...
- 【BZOJ1984】月下“毛景树” 树链剖分+线段树
[BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...
- 【BZOJ-1984】月下“毛景树” 树链剖分
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1314 Solved: 416[Submit][Status][Discu ...
- 树剖+线段树||树链剖分||BZOJ1984||Luogu4315||月下“毛景树”
题面:月下“毛景树” 题解:是道很裸的树剖,但处理的细节有点多(其实是自己线段树没学好).用一个Dfs把边权下移到点权,用E数组记录哪些边被用到了:前三个更新的操作都可以合并起来,可以发现a到b节点间 ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- Bzoj 1984: 月下“毛景树” 树链剖分
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1282 Solved: 410[Submit][Status][Discu ...
随机推荐
- POJ-2386.Lakecounting(DFS求连通块)
本题是一道连通块的入门题,用来练手,后续还会更新连通块的题目. 本题大意:一个n * m 的陆地上面有很多水洼,让你统计水洼的个数并输出. 本题思路:按照顺序遍历陆地,如果发现水洼就将它的八连块都进行 ...
- TOJ 3850: String Function Encoding
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3850 时间限制(普通/Java): ...
- Shell教程 之printf命令
上一章节我们学习了 Shell 的 echo 命令,本章节我们来学习 Shell 的另一个输出命令 printf. printf 命令模仿 C 程序库(library)里的 printf() 程序. ...
- Date 时间 日期 常用方法函数
转载自https://www.cnblogs.com/lcngu/p/5154834.html 一.java.util.Date对象用来表示时间,基本方法如下: Date mDate = new Da ...
- 最近读jdk源码一些基础的总结(有待后续深入)
第一点:java.lang 1.Object类,hashCode()方法,equals()方法,clone()方法,toString()方法,notify()和notifyAll()方法,wait() ...
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- Ubuntu虚拟机全屏问题
方法一:修改分辨率 xrandr查看能用的 xrandr -s 1920x1200改变. 方法二:安装新vmtools apt-get install open-vm-tools apt-get in ...
- java 異常抛出 throw 與 return
package 異常; public class TestException { public TestException() { } boolean test ...
- GUI界面操作-实现简单的记事本
wxPython编写界面程序的基本流程: 1.import wx #导入wxPython的包 2.class App(wx.App) #子类化一个应用程序类 3.def onInit(self ...
- tableView与导航栏的偏移问题
//方法1.关闭自动布局 self.automaticallyAdjustsScrollViewInsets = NO; //方法2.改变相对于scrollview的位置 //contentInset ...