SPOJ 375 Query on a tree 树链剖分模板
第一次写树剖~ #include<iostream>
#include<cstring>
#include<cstdio>
#define L(u) u<<1
#define R(u) u<<1|1
using namespace std;
const int MAX=;
int t,head[MAX*],next1[MAX*],tov[MAX*],val[MAX*],tot,n;
int fa[MAX],w[MAX],son[MAX],depth[MAX],tot2,size[MAX];
int d[MAX][],tree[MAX*],top[MAX];
char ch[];
inline void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
inline void add(int a,int b,int c)
{
tot++;
next1[tot]=head[a];
head[a]=tot;
tov[tot]=b;
val[tot]=c;
}
inline void dfs1(int k)
{
son[k]=;
size[k]=;
for (int i=head[k];i;i=next1[i])
{
int v=tov[i];
if (v!=fa[k])
{
fa[v]=k;
depth[v]=depth[k]+;
dfs1(v);
son[k]=size[v]<size[son[k]]?son[k]:v;
size[k]+=size[v];
}
}
}
inline void dfs2(int k,int _top)
{
w[k]=++tot2;
top[k]=_top;
if (son[k]!=) dfs2(son[k],_top);//the same _top
for (int i=head[k];i;i=next1[i])
{
int v=tov[i];
if (v!=fa[k]&&v!=son[k])
dfs2(v,v);//the new _top;
}
}
inline void update(int u,int l,int r,int loc,int _value)
{
if (l==loc&&r==loc)
{
tree[u]=_value;
return ;
}
int mid=(l+r)>>;
if (loc<=mid) update(L(u),l,mid,loc,_value);
else update(R(u),mid+,r,loc,_value);
tree[u]=max(tree[R(u)],tree[L(u)]);//pushup
}
void doit()
{
memset(head,,sizeof(head));
memset(depth,,sizeof(depth));
memset(fa,,sizeof(fa));
memset(tree,,sizeof(tree));
tot2=tot=;
scanf("%d",&n);
for (int i=;i<=n-;i++)
{
scanf("%d%d%d",&d[i][],&d[i][],&d[i][]);
add(d[i][],d[i][],d[i][]);
add(d[i][],d[i][],d[i][]);
}
dfs1();
dfs2(,);
for (int i=;i<=n-;i++)
{
if (depth[d[i][]]>depth[d[i][]]) swap(d[i][],d[i][]);//make the higher depth node to map the Edge
update(,,tot2,w[d[i][]],d[i][]); //change the value of the loc->w[d[i][1]]
}
}
inline int query(int u,int l,int r,int l1,int r1)
{
if (l1<=l&&r1>=r) return tree[u];
int mid=(l+r)>>;
if (r1<=mid) return query(L(u),l,mid,l1,r1);
else if (l1>mid) return query(R(u),mid+,r,l1,r1);
else {
return max(query(L(u),l,mid,l1,r1),query(R(u),mid+,r,l1,r1));
}
}
int findans(int va,int vb)
{
int f1,f2,ans;
f1=top[va];
f2=top[vb];
ans=;
while (f1!=f2)//find lca
{
if (depth[f1]<depth[f2])
{
swap(f1,f2);
swap(va,vb);
}
ans=max(ans,query(,,tot2,w[f1],w[va]));//swap make w[f1]<w[f2]
if (f1==f2) return ans;//in the same chain
va=fa[f1];f1=top[va];
}
if (va==vb) return ans;
if (depth[va]>depth[vb]) swap(va,vb);
return max(ans,query(,,tot2,w[son[va]],w[vb]));//the last do the f1=top[va]
}
void read()
{
ch[]='G';
scanf("%s",ch);
}
void work()
{
for (read();ch[]!='D';read())
{
int x,y;
scanf("%d%d",&x,&y);
if (ch[]=='Q') printf("%d\n",findans(x,y));
else update(,,tot2,w[d[x][]],y);
}
}
int main()
{
scanf("%d",&t);
for (int i=;i<=t;i++)
{
doit();
work();
}
}
SPOJ 375 Query on a tree 树链剖分模板的更多相关文章
- 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 ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- 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 ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- Query on a tree 树链剖分 [模板]
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- spoj 375 QTREE - Query on a tree 树链剖分
题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- cookie和session简单的用法
一.登录成功则设置cookie和session 二.在登录页判断是否已记住密码 三.在首页判断,和创建会话区 四.在首页执行并显示
- struts 国际化
国际化 1. 国际化与本地化 国际化 (Internationalization: I18N): 是程序在不做任何修改的情况下,就可以再不同的国家或地区和不同的语言环境下,按照当地的语言和格式习惯显示 ...
- Windows内核开发中如何区分文件对象究竟是文件还是文件夹?
今天有同行问了一个问题,Windows文件过滤驱动里的如何去区分一个对象是文件还是文件夹?我花了1小时左右翻阅了一些微软的文档以及以前的遗留代码,发现在WDK的帮助文档中是这么定义的: FILE_OB ...
- AppCan开发者资料分享(定期更新)
开发者培训 上海20150925开发者培训资料:链接:http://pan.baidu.com/s/1mgCLzz6 密码:mqgi 版权声明:本文为博主原创文章,未经博主允许不得转载.
- SVN在eclipse的整合应用
目前很多的Java.Flex.Android开发人员是用eclipse作为开发工具的,本文主要介绍SVN在eclipse平台中的整合应用. 我的eclipse版本是Version: 3.4.2.本身没 ...
- Qt qml listview 列表视图控件(下拉刷新、上拉分页、滚动轴)
Qt qml listview下拉刷新和上拉分页主要根据contentY来判断.但要加上顶部下拉指示器.滚动条,并封装成可简单调用的组件,着实花了我不少精力:) [先看效果] [功能] 下拉刷新 ...
- Qt::QObject类
QObject 类是Qt 所有类的基类. QObject是Qt对象模型的核心.这个模型的中心要素就是一种强大的叫做信号与槽无缝对象沟通机制.你可以用 connect()函数来把一个信号连接到槽,也可以 ...
- Android 子线程测试
private volatile boolean mStopped = false; private int i; TextView tv1; TextView tv2; @Override prot ...
- java equals和==的区别
大概说equals和==都比较的是什么: 1. boolean tem = a == b; 首先==比较的肯定是地址,从堆栈的角度说也就是说==比较的是栈上面的内容.因为栈是用来存放地址或是java中 ...
- JavaScript简介
JavaScript JavaScript 是一种轻量级的编程语言,是可插入 HTML 页面的编程代码,这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等 ...