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...,N. Each edge has an integer value assigned to it(note that the value can be negative). Each node has a color, white or black. We define dist(a, b) as the sum of the value of the edges on the path from node a to node b.
All the nodes are white initially.
We will ask you to perfrom some instructions of the following form:
- C a : change the color of node a.(from black to white or from white to black)
- A : ask for the maximum dist(a, b), both of node a and node b must be white(a can be equal to b). Obviously, as long as there is a white node, the result will alway be non negative.
Input
- In the first line there is an integer N (N <= 100000)
- In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of value c (-1000 <= c <= 1000)
- In the next line, there is an integer Q denotes the number of instructions (Q <= 100000)
- In the next Q lines, each line contains an instruction "C a" or "A"
Output
For each "A" operation, write one integer representing its result. If there is no white node in the tree, you should write "They have disappeared.".
Example
Input:
3
1 2 1
1 3 1
7
A
C 1
A
C 2
A
C 3
A Output:
2
2
0
They have disappeared.
Some new test data cases were added on Apr.29.2008, all the solutions have been rejudged.
树 Link cut tree
查动态点分治时候在别的博客看到一个LCT做Qtree的专题,心生敬仰于是学(chao)了一发。
结论是相比之下还是点分治好写啊,这个状态更新神烦,如果不标准代码比对,我估计得调几个小时吧……
--------
大致就是,把边权迁移到子结点上,先建好LCT。Splay树是二叉树,那么就把当前未激活的边全都扔到set里记录区间答案,然后像平衡树/线段树维护最大子串和那样,记录上边来的最长链,下边来的最长链,左边来的最长链,右边来的最长链……,以及当前结点保存的边长等信息。如果链是从原树的祖先方向来的,维护时候要加那个被迁移的边权,如果是从子孙方向来的,因为下面加过了所以不用再加……
之后花式更新即可。
那个边扔到set里的操作,有人说是传说中的虚边……ダメだ 知らないだ……
n*(logn)^2卡常预警。据说是因为树浅时set多但LCT操作少,而树深时LCT操作多但set小,均摊下来不会超时。
↑不会算复杂度的我只能把这当成玄学
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
const int INF=1e8;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int fir(multiset<int> &s){return s.size()? *s.rbegin():-INF;}
int sec(multiset<int> &s){return s.size()>? *(++s.rbegin()):-INF;}
struct edge{int v,nxt,w;}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;return;
}
int col[mxn],w[mxn];
struct LCT{
multiset<int>chain[mxn],pt[mxn];
int ch[mxn][],fa[mxn];
int len[mxn],L[mxn],R[mxn],mians[mxn],sum[mxn],ans;
bool rev[mxn];
void init(int n){for(int i=;i<=n;i++)L[i]=R[i]=mians[i]=-INF;}
inline bool isroot(int x){return ((ch[fa[x]][]!=x) && (ch[fa[x]][]!=x));}
void pushup(int x){
int lc=ch[x][],rc=ch[x][];
sum[x]=sum[lc]+sum[rc]+len[x];
int cmi=max(w[x],fir(chain[x]));
// printf("tst %d: lc:%d rc:%d sum:%d cmi:%d\n",x,lc,rc,sum[x],cmi);
// printf(" L rc:%d R lc:%d\n",L[rc],R[lc]);
int La=max(cmi,R[lc]+len[x]);//左子树(深度更浅的地方)
int Ra=max(cmi,L[rc]);//右子树
L[x]=max(L[lc],sum[lc]+len[x]+Ra);
R[x]=max(R[rc],sum[rc]+La);
// printf(" L:%d R:%d\n",L[x],R[x]);
mians[x]=max(mians[lc],mians[rc]);
mians[x]=max(mians[x],max(R[lc]+len[x]+Ra,L[rc]+La));
mians[x]=max(mians[x],fir(pt[x]));
mians[x]=max(mians[x],fir(chain[x])+sec(chain[x]));
if(w[x]==)mians[x]=max(w[x],max(mians[x],fir(chain[x])));
return;
}
void rotate(int x){
int y=fa[x],z=fa[y],lc,rc;
if(ch[y][]==x)lc=;else lc=;rc=lc^;
if(!isroot(y))ch[z][ch[z][]==y]=x;
fa[x]=z;fa[y]=x;
ch[y][lc]=ch[x][rc];fa[ch[x][rc]]=y;
ch[x][rc]=y;
pushup(y);
return;
}
// int st[mxn],top;
void Splay(int x){
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if((ch[z][]==y)^(ch[y][]==x))rotate(x);
else rotate(y);
}
rotate(x);
}
pushup(x);
}
void access(int x){
int y=;
for(;x;x=fa[x]){
Splay(x);
if(ch[x][]){
pt[x].insert(mians[ch[x][]]);
chain[x].insert(L[ch[x][]]);
}
if(y){
pt[x].erase(pt[x].find(mians[y]));
chain[x].erase(chain[x].find(L[y]));
}
ch[x][]=y;
pushup(x);
y=x;
}
}
void change(int x){
access(x);Splay(x);
col[x]^=;if(!col[x])w[x]=;else w[x]=-INF;
pushup(x);
ans=mians[x];
}
/* void query(int x){
access(x);Splay(x);
pushup(x);
ans=mians[x];
}*/
}LT;
void DFS(int u,int fa){
for(int i=hd[u];i;i=e[i].nxt){
if(e[i].v==fa)continue;int v=e[i].v;
LT.fa[v]=u;
LT.len[v]=e[i].w;
DFS(v,u);
LT.chain[u].insert(LT.L[v]);
LT.pt[u].insert(LT.mians[v]);
}
LT.pushup(u);
return;
}
int n,Q;
int main(){
int i,j,u,v,vl;
n=read();
for(i=;i<n;i++){
u=read();v=read();vl=read();
add_edge(u,v,vl);
add_edge(v,u,vl);
}
LT.init(n);
// for(i=1;i<=n;i++)col[i]=0;
DFS(,);
LT.ans=LT.mians[];
// printf("LT:%d\n",LT.ans);
Q=read();char op[];
while(Q--){
scanf("%s",op);
if(op[]=='C')v=read(),LT.change(v);
else{
// LT.query(v);
if(LT.ans<)printf("They have disappeared.\n");
else printf("%d\n",LT.ans);
}
}
return ;
}
SPOJ QTREE4 SPOJ Query on a tree IV的更多相关文章
- 【SPOJ QTREE4】Query on a tree IV(树链剖分)
Description 给出一棵边带权(\(c\))的节点数量为 \(n\) 的树,初始树上所有节点都是白色.有两种操作: C x,改变节点 \(x\) 的颜色,即白变黑,黑变白. A,询问树中最远的 ...
- SPOJ VJudge QTREE - Query on a tree
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submi ...
- 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 ——动态点分治
[题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...
- Query on a tree IV SPOJ - QTREE4
https://vjudge.net/problem/SPOJ-QTREE4 点分就没有一道不卡常的? 卡常记录: 1.把multiset换成手写的带删除堆(套用pq)(作用很大) 2.把带删除堆里面 ...
- SPOJ - QTREE4 Query on a tree IV 边分治
题目传送门 题意:有一棵数,每个节点有颜色,黑色或者白色,树边有边权,现在有2个操作,1修改某个点的颜色, 2询问2个白点的之前的路径权值最大和是多少. 题解: 边分治思路. 1.重构图. 因为边分治 ...
- 2019.02.16 spoj Query on a tree IV(链分治)
传送门 题意简述: 捉迷藏强化版(带有边权,可以为负数) 思路:好吧这次我们不用点分树,我们用听起来更屌的链分治. 直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog ...
- 【SPOJ】375. Query on a tree(树链剖分)
http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...
随机推荐
- 2- vue django restful framework 打造生鲜超市 -环境搭建
使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...
- 十七、MySQL UNION 操作符
MySQL UNION 操作符 本教程为大家介绍 MySQL UNION 操作符的语法和实例. 描述 MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多 ...
- 卸载Redhat 7自带的yum,安装并使用网易163源
由于redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,安装CentOS yum工具,再配置其他源,以下为详细过程:删除redhat原有的yum ...
- php获取随机字符串
获取随机字符串 /** * 获取随机字符串 * @param int $randLength 长度 * @param int $addtime 是否加入当前时间戳 * @param int $incl ...
- day 71 Django基础六之ORM中的锁和事务
Django基础六之ORM中的锁和事务 本节目录 一 锁 二 事务 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 锁 行级锁 select_for_update(no ...
- pandas知识点(基本功能)
1.重新索引 如果reindex会根据新索引重新排序,不存在的则引入缺省: In [3]: obj = Series([4.5,7.2,-5.3,3.6], index=["d", ...
- selenium中webdriver跳转新页面后定位置新页面的两种方式
刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...
- Django配置邮箱登录
1.settings下配置 # AUTH 方法(支持邮箱登录) AUTHENTICATION_BACKENDS = ('users.views.CustomBackend',) 2.views下逻辑如 ...
- 对二维数组使用指针进行操作的探索(C语言)
/* Name: 对二维数组使用指针进行操作的探索 Copyright: Author: lingr7 Date: 01/12/18 11:55 Description: */ #include< ...
- 51nod_1255字典序最小的子序列
作为贪心算法的某道例题,赶脚药丸啊..这么简单的代码重构第三遍才过... 首先是贪心算法思想, 1,证明贪心算法有效性:贪心策略,使用栈结构实现,遍历输入串中所有元素,对于某个元素有如下两种情况: 情 ...