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只用了半 ...
随机推荐
- MySql学习笔记01
MySql01 课程介绍 数据库简介 之前通过流操作文件的方式存储数据弊端: 1. 效率低 2. 不管是存还是取都比较麻烦 3. 一般只能存储小量数据 4. 只能存储文本数据 什么是DB DataBa ...
- yum安装报错
检查了好久才知道原来是 sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33 下的DNS配错了,改好之后,sudo service network ...
- 关于web.xml配置中的<url-pattern>
标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标签,例: <servlet><servlet-n ...
- C++学习网站——www.cplusplus.com
http://www.cplusplus.com/ 有各个函数.语法的实例代码,可以在线运行http://cpp.sh/不支持中文字符,不错.
- V4L2学习(三)框架分析
整个v4l2的框架分为三层: 在应用层,我们可以在 /dev 目录发现 video0 类似的设备节点,上层的摄像头程序打开设备节点进行数据捕获,显示视频画面.设备节点的名字很统一,video0 vid ...
- mybatis特殊字符处理
在mybatis 的mapper.xml文件中特殊字符处理方式 仅供参考 出处:http://yaobenzhang.blog.163.com/blog/static/214395113201561 ...
- Django权限管理系统设计分析
权限管理顾名思义,其实就是角色控制权限的系统,每个用户对应一个角色,每个角色有对应的权限,比如公司会有CEO,总监,销售经理,销售员,每个人的权限都不一样,那我们给他展示的url也都不同 一.首先创建 ...
- linux学习(二) -- ubuntu下lnmp环境的配置
亲测的教程,,希望能对大家提供些许帮助,转载请注明出处 ubuntu+nginx+mysql+php7 一.安装Nginx 1.首先添加nginx_signing.key(必须,否则出错) $ wge ...
- ogre3D学习基础5 -- 阴影与动画
五.阴影 阴影是渲染一个真实场景的重要组成部分,它可以给场景中的物体提供更加真实的感觉,同时还可以帮助用户更好的了解对象间的空间关系. 启用阴影: 缺省情况下,阴影是关闭的,开启方式如下: 1.建立场 ...
- Robotium测试报告的生成方法(上)
7.1 使用junit-report生成报告 这个是参考网上的:http://www.xuebuyuan.com/2148574.html,经我个人验证是可行的方法,网上写的挺详细的,不过有些不太清楚 ...