传送门

题意简述:

捉迷藏强化版(带有边权,可以为负数)


思路:好吧这次我们不用点分树,我们用听起来更屌的链分治

直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog条重链的。

然后用上链分治的常规套路:分是否在链上面的信息讨论,并将整条链的值全部统计在链顶,这样修改的时候沿着链往上跳修改logloglog次即可。

那么针对这道题可以怎么瞎搞维护呢?

考虑对每个点构建一个大根堆hih_ihi​来维护它的子树去掉重儿子所在子树一位的点到它的距离的最值。

显然这个是可以从它的轻儿子们的答案转移过来的。

然后怎么维护整棵子树的最值呢?

对于每条重链我们开一棵线段树,把它们从上到下映射成一段连续的从左到右的区间,这样链顶对应最左端点,链底对应最右端点。

我们的线段树节点维护到区间最左端的最大值,到最右端的最大值,区间中相距最大的距离。

这个转移神似最大子段和,大家自己yyyyyy吧。

细节较多,详细的可以看代码:

#include<bits/stdc++.h>
#define ri register int
#define fi first
#define se second
using namespace std;
inline int read(){
	int ans=0,w=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ~w?ans:-ans;
}
typedef pair<int,int> pii;
const int N=1e5+5,inf=1<<28;
int n,m,siz[N],top[N],num[N],pred[N],bot[N],fa[N],hson[N],f[N],a[N],dis[N],tot=0;
bool col[N];
vector<pii>e[N];
struct deletable_heap{
	priority_queue<int>a,b;
	inline void push(const int&x){a.push(x);}
	inline void del(const int&x){b.push(x);}
	inline int top(){while(b.size()&&a.top()==b.top())a.pop(),b.pop();return a.top();}
	inline void pop(){while(b.size()&&a.top()==b.top())a.pop(),b.pop();a.pop();}
	inline int size(){return a.size()-b.size();}
	inline int stop(){int tmp=top(),ret;return pop(),ret=top(),push(tmp),ret;}
	inline void f(int x,int&a,int&b,int&c){
		pii ret=pii(-inf,-inf);
		if(!col[x])ret.fi=ret.se=0;
		if(size())ret.fi=max(ret.fi,top());
		if(size()>1)ret.se=max(ret.se,stop());
		a=b=ret.fi,c=!col[x]?max(ret.fi,ret.fi+ret.se):ret.fi+ret.se;
	}
}h[N],Ans;
inline int max(const int&a,const int&b){return a>b?a:b;}
inline int max(const int&a,const int&b,const int&c){return max(max(a,b),c);}
namespace SGT{
	#define lc (p<<1)
	#define rc (p<<1|1)
	#define mid (T[p].l+T[p].r>>1)
	struct Node{int l,r,sum,ls,rs,ms;}T[N<<2];
	inline Node operator+(const Node&a,const Node&b){
		Node ret;
		ret.l=a.l,ret.r=b.r;
		int tmp=dis[pred[b.l]]-dis[pred[a.r]];
		ret.sum=a.sum+tmp+b.sum;
		ret.ls=max(a.ls,a.sum+tmp+b.ls);
		ret.rs=max(b.rs,b.sum+tmp+a.rs);
		ret.ms=max(a.ms,b.ms,a.rs+tmp+b.ls);
		return ret;
	}
	inline void Set(int p){
		int k=pred[T[p].l];
		T[p].sum=0,h[k].f(k,T[p].ls,T[p].rs,T[p].ms);
	}
	inline void pushup(int p){T[p]=T[lc]+T[rc];}
	inline void build(int p,int l,int r){
		T[p].l=l,T[p].r=r;
		if(l==r)return;
		build(lc,l,mid),build(rc,mid+1,r);
	}
	inline void update(int p,int k){
		if(T[p].l==T[p].r)return Set(p);
		update(k<=mid?lc:rc,k),pushup(p);
	}
	inline Node query(int p,int ql,int qr){
		if(ql<=T[p].l&&T[p].r<=qr)return T[p];
		if(qr<=mid)return query(lc,ql,qr);
		if(ql>mid)return query(rc,ql,qr);
		return query(lc,ql,qr)+query(rc,ql,qr);
	}
	#undef lc
	#undef rc
	#undef mid
}
void dfs1(int p){
	siz[p]=1;
	for(ri i=0,v;i<e[p].size();++i){
		if((v=e[p][i].fi)==fa[p])continue;
		fa[v]=p,dis[v]=dis[p]+e[p][i].se,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,bot[tp]=p;
	if(!hson[p])return;
	dfs2(hson[p],tp);
	for(ri i=0,v;i<e[p].size();++i){
		if((v=e[p][i].fi)==fa[p]||v==hson[p])continue;
		dfs2(v,v);
	}
}
int dfs3(int p){
	for(ri x=p;x;x=hson[x]){
		for(ri i=0,v;i<e[x].size();++i){
			if((v=e[x][i].fi)==hson[x]||v==fa[x])continue;
			h[x].push(dfs3(v)+e[x][i].se);
		}
		SGT::update(1,num[x]);
	}
	SGT::Node tmp=SGT::query(1,num[p],num[bot[p]]);
	return Ans.push(tmp.ms),tmp.ls;
}
inline void update(int p){
	while(p){
		int ft=fa[top[p]],tp=top[p];
		SGT::Node tmp=SGT::query(1,num[top[p]],num[bot[top[p]]]);
		Ans.del(tmp.ms);
		if(ft)h[ft].del(tmp.ls+dis[tp]-dis[ft]);
		SGT::update(1,num[p]);
		tmp=SGT::query(1,num[top[p]],num[bot[top[p]]]);
		Ans.push(tmp.ms);
		if(ft)h[ft].push(tmp.ls+dis[tp]-dis[ft]);
		p=ft;
	}
}
char s[2];
int main(){
	n=read();
	for(ri i=1,u,v,w;i<n;++i){
		u=read(),v=read(),w=read();
		e[u].push_back(pii(v,w));
		e[v].push_back(pii(u,w));
	}
	dfs1(1),dfs2(1,1);
	SGT::build(1,1,n);
	dfs3(1);
	for(ri all=n,tt=read();tt;--tt){
		scanf("%s",s);
		if(s[0]=='A'){
			if(all>1)cout<<Ans.top()<<'\n';
			else if(all==1)puts("0");
			else puts("They have disappeared.");
		}
		else{
			int x=read();
			col[x]^=1;
			if(col[x])--all;
			else ++all;
			update(x);

		}
	}
	return 0;
}

2019.02.16 spoj Query on a tree IV(链分治)的更多相关文章

  1. 2019.02.17 spoj Query on a tree VII(链分治)

    传送门 跟QTREE6QTREE6QTREE6神似,改成了求连通块里的最大值. 于是我们对每条链开一个heapheapheap维护一下即可. MDMDMD终于1A1A1A链分治了. 代码: #incl ...

  2. 2019.02.17 spoj Query on a tree VI(链分治)

    传送门 题意简述:给你一棵nnn个黑白点的树,支持改一个点的颜色,询问跟某个点颜色相同的连通块大小. 思路: 还是链分治 233 记fi,0/1f_{i,0/1}fi,0/1​表示iii的所有颜色为0 ...

  3. 2019.02.17 spoj Query on a tree V(链分治)

    传送门 题意简述: 给你一棵nnn个黑白点的树,初始全是黑点. 现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离. 思路: 考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于 ...

  4. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  5. SPOJ QTREE4 SPOJ Query on a tree IV

    You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3. ...

  6. SPOJ QTREE4 - Query on a tree IV 树分治

    题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...

  7. SPOJ - QTREE4 Query on a tree IV 边分治

    题目传送门 题意:有一棵数,每个节点有颜色,黑色或者白色,树边有边权,现在有2个操作,1修改某个点的颜色, 2询问2个白点的之前的路径权值最大和是多少. 题解: 边分治思路. 1.重构图. 因为边分治 ...

  8. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...

  9. spoj 375 Query on a tree (树链剖分)

    Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...

随机推荐

  1. vue路由的钩子函数和跳转

    首页可以控制导航跳转,beforeEach,afterEach等,一般用于页面title的修改.一些需要登录才能调整页面的重定向功能. beforeEach主要有3个参数to,from,next. t ...

  2. TCP‘三次握手’和‘四次挥手’(通俗易懂)

      概述 我们都知道 TCP 是 可靠的数据传输协议,UDP是不可靠传输,那么TCP它是怎么保证可靠传输的呢?那我们就不得不提 TCP 的三次握手和四次挥手. 三次握手 下图为三次握手的流程图 下面通 ...

  3. Pycharm:使用笔记

    1.快捷键笔记 CTRL+D:复制当前行 CTRL+/:注释选中行 CTRL+ALT+L:自动format,自动进行标准格式化 ALT + 7:查看当前文件的类和方法概览 Ctrl + Y    删除 ...

  4. leetcode347

    public class Solution { public IList<int> TopKFrequent(int[] nums, int k) { var dic = new Dict ...

  5. ---dd io测试

    下面是一个简单测试,虽然不够准确但是简单立即可行, 当前目录的IO写读测试: (写) dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync ( ...

  6. mysql实用函数

    1.  group_concat(); 可以将选择的字段列数据,分组以逗号分隔成一串.实用方便.select id,group_concat(distinct name) from ttt group ...

  7. python装饰器补漏

    以前写过一篇装饰器文章,觉得少了点东西,今天特来补上,也就是带参数的装饰器,上篇文章写的不严谨 def logger(logs=""): def outer(f): def inn ...

  8. jquery 中后代遍历之children、find区别

    jquery 中children.find区别 首先看一段HTML代码,如下: <table id="tb"> <tr> <td>0</t ...

  9. Apache 修改端口号

    默认是80端口,可在httpd.conf文件中修改以下2个地方的端口号为预定的即可,例如修改为8080端口 把httpd.conf 中Listen 80 改成Listen 8080 把ServerNa ...

  10. 使用pandas进行数据预处理01

    数据预处理有四种技术:数据合并,数据清洗,数据标准化,以及数据转换. 数据合并技术:(1)横向或纵向堆叠合数据 (2)主键合并数据 (3)重叠合并数据 1.堆叠合并数据: 堆叠就是简单的把两个表拼接在 ...