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只用了半 ...
随机推荐
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- MSBuild常用方法
打包后把nuget包复制到指定的目录 <Target Name="CopyPackage" AfterTargets="Pack"> <Cop ...
- 关于request.getServletContext()方法报错的问题
可以通过修改pom文件来添加一个javax.servlet-api-3.1.0.jar的jar包,找到你的pom.xml文件添加代码如下: <dependency> <groupId ...
- 震惊!几道Python 理论面试题,Python面试题No18
本面试题题库,由公号:非本科程序员 整理发布 第1题: 简述解释型和编译型编程语言? 解释型语言编写的程序不需要编译,在执行的时候,专门有一个解释器能够将VB语言翻译成机器语言,每个语句都是执行的时候 ...
- java NIO简介
1)java nio简介 nio 是 java New IO 的简称,在 jdk1.4 里提供的新 api . Sun 官方标榜的特性如有:为所有的原始类型提供 (Buffer) 缓存支持:字符集编码 ...
- 配置Wampserver和安装thinksns
一.先安装Wampserver(去官网下载) 二.安装好后单击wampserver图标,Apache->Service->测试80端口,如果显示: i 端口被iis占用 控制面板-> ...
- postgreysql
基础 syntax * \help 生成所有的pg命令 * abort 终止事务/work * alter aggregate 修改聚合函数的定义 ALTER AGGREGATE name ( typ ...
- BZOJ 1222: [HNOI2001]产品加工
F[i]表示第一个机器用了i的时间,第二个机器的最小时间 转移即可 #include<cstdio> #include<algorithm> using namespace s ...
- Python虚拟机类机制之对象模型(一)
Python对象模型 在Python2.2之前,Python中存在着一个巨大的裂缝,就是Python的内置类type,比如:int和dict,这些内置类与程序员在Python中自定义的类并不是同一级别 ...
- 03012_会话技术Cookie&Session
1.会话技术简介 (1)存储客户端的技术 网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术识 ...