SPOJ QTREE4 Query on a tree IV ——动态点分治
【题目分析】
同bzoj1095
然后WA掉了。
发现有负权边,只好把rmq的方式改掉。
然后T了。
需要进行底(ka)层(chang)优(shu)化。
然后还是T
下午又交就A了。
【代码】
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxe 200005
#define maxn 100005
#define inf (0x3f3f3f3f)
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
int h[maxe],to[maxe],ne[maxe],en=0,ban[maxn],n,m,rt,now,dst[maxn],Tree_rt,col[maxn],ans=-1,x,aim;
int b[maxn<<2],cnt=0,tag=0,pos[maxn],c[maxn<<2][20],_log[maxn<<2],w[maxe],d[maxn<<2][20],dep[maxn]; void add(int a,int b,int c) {to[en]=b;ne[en]=h[a];w[en]=c;h[a]=en++;}
struct Heap{
priority_queue <int> heap,del;
void Ins(int x){heap.push(x);}
void Del(int x){del.push(x);}
void Pop(){while (del.size()&&del.top()==heap.top()) del.pop(),heap.pop(); heap.pop();}
int Top(){while (del.size()&&del.top()==heap.top()) del.pop(),heap.pop(); return heap.top();}
int S_Top(){int tmp=Top(),ret;Pop();ret=Top();Ins(tmp);return ret; }
int Size(){return heap.size()-del.size();}
}s1[maxn],s2[maxn],lst; int mx[maxn],siz[maxn],size,fa[maxn];
char opt[11]; void dfs(int o,int fa)
{
if (!tag)b[++cnt]=o,pos[o]=cnt;
siz[o]=1;mx[o]=0;
for (int i=h[o];i>=0;i=ne[i])
if (!ban[to[i]]&&to[i]!=fa)
{
if (!tag) dep[to[i]]=dep[o]+1;
dfs(to[i],o);
if (!tag) b[++cnt]=o;
siz[o]+=siz[to[i]];
if (siz[to[i]]>mx[o]) mx[o]=siz[to[i]];
}
} void dfs_rt(int o,int fa)
{
if (max(mx[o],size-siz[o])<now) rt=o,now=max(mx[o],size-siz[o]);
for (int i=h[o];i>=0;i=ne[i])
if (!ban[to[i]]&&to[i]!=fa) dfs_rt(to[i],o);
} int lca(int a,int b)
{
int pa=pos[a],pb=pos[b];
if (pa>pb) swap(pa,pb);
int l=_log[pb-pa+1];
if (dep[d[pa][l]]<dep[d[pb-(1<<l)+1][l]]) return dst[d[pa][l]];
else return dst[d[pb-(1<<l)+1][l]];
} int dist(int a,int b)
{
if (a==0) a=Tree_rt;
if (b==0) b=Tree_rt;
return dst[a]+dst[b]-2*lca(a,b);
} void dfs_add(int o,int fa)
{
s1[rt].Ins((dist(o,aim)));
for (int i=h[o];i>=0;i=ne[i])
if (!ban[to[i]]&&to[i]!=fa)
dfs_add(to[i],o);
} void Divide(int o,int fat)
{
int root;
dfs(o,-1); size=siz[o]; now=inf;
dfs_rt(o,-1); root=rt;
if (fat) fa[root]=fat;
ban[root]=1;
aim=fat;
dfs_add(root,-1);
s2[fat].Ins(s1[root].Top());
if (!col[root]) s2[root].Ins(0);
for (int i=h[root];i>=0;i=ne[i])
if (!ban[to[i]]) Divide(to[i],root);
if (s2[root].Size()>=2)
{
int tmp=s2[root].Top()+s2[root].S_Top();
lst.Ins(tmp);
}
} void dfs_dist(int o,int fat)
{
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fat) dst[to[i]]=dst[o]+w[i],dfs_dist(to[i],o);
} void Delete(int o)
{
if (s2[o].Size()>=2) lst.Del(s2[o].Top()+s2[o].S_Top());
s2[o].Del(0);
if (s2[o].Size()>=2)
{
int tmp=s2[o].Top()+s2[o].S_Top(); lst.Ins(tmp);
}
if (s2[o].Size()>0) ans=max(ans,0);
int now=o;
while (now)
{
int fat=fa[now];
if (s2[fat].Size()>=2)lst.Del(s2[fat].Top()+s2[fat].S_Top());
int tmp=s1[now].Top();
s1[now].Del(dist(o,fat));
if (s1[now].Size()==0||tmp!=s1[now].Top())
{
s2[fat].Del(tmp);
if (s1[now].Size()>0) s2[fat].Ins(s1[now].Top());
}
if (s2[fat].Size()>=2)
{
int tmp=s2[fat].Top()+s2[fat].S_Top();
lst.Ins(tmp);
}
if (s2[fat].Size()>0) ans=max(ans,0);
now=fat;
}
} void Insert(int o)
{
if (s2[o].Size()>=2)
{
lst.Del(s2[o].Top()+s2[o].S_Top());
}
s2[o].Ins(0);
if (s2[o].Size()>=2)
{
int tmp=s2[o].Top()+s2[o].S_Top();lst.Ins(tmp);
}
if (s2[o].Size()>0) ans=max(ans,0);
int now=o;
while (now)
{
int fat=fa[now];
if (s2[fat].Size()>=2) lst.Del(s2[fat].Top()+s2[fat].S_Top());
int tmp,flag=0;
if (s1[now].Size()==0) flag=1;
else tmp=s1[now].Top();
s1[now].Ins(dist(o,fat));
if (flag||tmp!=s1[now].Top())
{
if (!flag) s2[fat].Del(tmp);
s2[fat].Ins(s1[now].Top());
}
if (s2[fat].Size()>=2)
{
int tmp=s2[fat].Top()+s2[fat].S_Top();
lst.Ins(tmp);
}
if (s2[fat].Size()>0) ans=max(ans,0);
now=fat;
}
} int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} int main()
{
memset(h,-1,sizeof h);
n=Getint();
F(i,1,n-1)
{
int a,b,c;
a=Getint();b=Getint();c=Getint();
add(a,b,c);add(b,a,c);
}
tag=1;
dfs(1,-1);
now=inf; size=siz[1]; dfs_rt(1,-1); dfs_dist(rt,-1);
tag=0;
dfs(rt,-1);
tag=1;
F(i,2,cnt) _log[i]=_log[i>>1]+1;
F(i,1,cnt) d[i][0]=b[i];
F(j,1,_log[cnt])
for (int i=1;i+(1<<j)-1<=cnt;++i)
{
if (dep[d[i][j-1]]<dep[d[i+(1<<j-1)][j-1]]) d[i][j]=d[i][j-1];
else d[i][j]=d[i+(1<<j-1)][j-1];
}
Tree_rt=rt;
Divide(1,0);
scanf("%d",&m);
F(i,1,m)
{
scanf("%s",opt);
switch(opt[0])
{
case 'A': if (lst.Size()) ans=max(ans,lst.Top());printf("%d\n",ans); break;
case 'C': x=Getint();ans=-1; if (!col[x]) Delete(x); else Insert(x); col[x]^=1; break;
}
}
}
SPOJ QTREE4 Query on a tree IV ——动态点分治的更多相关文章
- SPOJ QTREE4 - Query on a tree IV
You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3. ...
- SPOJ QTREE4 - Query on a tree IV 树分治
题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...
- SPOJ - QTREE4 Query on a tree IV 边分治
题目传送门 题意:有一棵数,每个节点有颜色,黑色或者白色,树边有边权,现在有2个操作,1修改某个点的颜色, 2询问2个白点的之前的路径权值最大和是多少. 题解: 边分治思路. 1.重构图. 因为边分治 ...
- SPOJ QTREE Query on a tree V ——动态点分治
[题目分析] QTREE4的弱化版本 建立出分治树,每个节点的堆表示到改点的最近白点距离. 然后分治树上一直向上,取min即可. 正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补. ...
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- 洛谷 P2056 [ZJOI2007]捉迷藏 || bzoj 1095: [ZJOI2007]Hide 捉迷藏 || 洛谷 P4115 Qtree4 || SP2666 QTREE4 - Query on a tree IV
意识到一点:在进行点分治时,每一个点都会作为某一级重心出现,且任意一点只作为重心恰好一次.因此原树上任意一个节点都会出现在点分树上,且是恰好一次 https://www.cnblogs.com/zzq ...
- 2019.02.16 spoj Query on a tree IV(链分治)
传送门 题意简述: 捉迷藏强化版(带有边权,可以为负数) 思路:好吧这次我们不用点分树,我们用听起来更屌的链分治. 直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog ...
- SP2666 QTREE4 - Query on a tree IV(LCT)
题意翻译 你被给定一棵n个点的带边权的树(边权可以为负),点从1到n编号.每个点可能有两种颜色:黑或白.我们定义dist(a,b)为点a至点b路径上的权值之和. 一开始所有的点都是白色的. 要求作以下 ...
- 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. ...
随机推荐
- azure 创建redhat镜像帮助
为 Azure 准备基于 Red Hat 的虚拟机 从 Hyper-V 管理器准备基于 Red Hat 的虚拟机 先决条件 本部分假定你已经从 Red Hat 网站获取 ISO 文件并将 RHEL 映 ...
- C语言中的fprintf函数详解
fprintf 功能 传送格式化输出到一个文件中 用法 #include stdio.h int fprintf( FILE *stream, const char *format,...); f ...
- LR中下载文件的脚本
#include "web_api.h" Action(){ int iflen; //文件大小 long lfbody; //响应数据内容大小 web_url("xxx ...
- hdu 3555 Bomb 炸弹(数位DP,入门)
题意: 给一个数字n,求从1~n中有多少个数是含有49的,比如49,149,1490等都是含49的. 思路: 2^64也顶多是十进制的20多位,那么按十进制位来分析更简单.如果能计算k位十进制数中分别 ...
- COGS 2084. Asm.Def的基本算法
★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “有句美国俗语说,如果走起来像鸭子,叫起来像 ...
- mvc工作总结
MVC的页面跳转方式(放在一般类): filterContext.Result = new RedirectResult("controller/action"); filterC ...
- 校内选拔I题题解 构造题 Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) ——D
http://codeforces.com/contest/574/problem/D Bear and Blocks time limit per test 1 second memory limi ...
- scss引入的问题
导入.sass或.scss文件 css有一个不太常用的特性,即@import 导入功能,它允许在一个css文件中导入其他css文件.然而,结果是只有执行到@import 规则时,浏览器才会去下载其他c ...
- OpenWrite方法打开现有文件并进行写入
实现效果: 知识运用: File类的OpenWrite方法 //实现打开现有文件以进行写入 public static FileStream OpenWrite (string path) Encod ...
- Python基础篇 -- 字典
字典 dict. 以 {} 表示, 每一项用逗号隔开, 内部元素用 key: value的形式来保存数据 例子: dict.{"JJ":"林俊杰"," ...