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, ...
随机推荐
- MySQL数据库中tinyint类型字段读取数据为true和false
今天遇到这么一个问题,公司最近在做一个活动,然后数据库需要建表,其中有个字段是关于奖励发放的状态的字段,结果读取出来的值为true 一.解决读取数据为true/false的问题 场景: 字段:stat ...
- Hive:Spark中如何实现将rdd结果插入到hive1.3.0表中
DataFrame写入hive API: registerTempTable函数是创建spark临时表 insertInto函数是向表中写入数据,可以看出此函数不能指定数据库和分区等信息,不可以直接进 ...
- ADC 转换序列暂时难理解
通常情况下,core文件会包含了程序运行时的内存,寄存器状态,堆栈指针,内存管理信息还有各种函数调用堆栈信息等,我们可以理解为是程序工作当前状态存储生成第一个文件,程序出错的时候理论上都会产生一个co ...
- 显示python已安装模块及路径,添加修改模块搜索路径
在python交互模式下输入: help('modules') #可以显示出已安装的模块 在python交互模式下输入: import sys sys.path #可以显示出模块搜索路径 增加搜索路径 ...
- ERROR 1130: Host 'XXXXXXX' is not allowed to connect to this MySQL server
解决方法:1. 改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 & ...
- spring-cloud-event-sourcing-example-master 运行效果及说明
1.微服务架构下电商示例,部署成功!监控等技术细节未深入研究. 简单介绍: 配置服务(config-service ),端口8888,健康检查 xx:8888/hea ...
- 8139too.c网卡驱动简单分析
从事linux C开发工作以来,工作内容主要是在应用层,对nginx和unbound等软件有些了解,也常对这2个软件进行二次开发. 对网络这块一直比较有兴趣.也很好奇网卡到底是怎么接受到报文的,以及报 ...
- AngularJs的UI组件ui-Bootstrap
http://www.cnblogs.com/pilixiami/p/5597634.html
- vs加js引用
今天又有一个同事问我“在VS中如何让一个JS文件智能提示另一个JS文件中的成员”,他说Google了一下,并没有找到答案,然后我把这个小技巧贴出来,希望能被Google到. 有时候会有这种情况:当我的 ...
- cocos2d-x quick 学习 一 环境
最近几天都在学习quick 一直也在查找资料. 本来这篇文章在昨晚就能写好的.可是昨晚环境遇到点问题自己没想通. 正题:首先是环境配置: 由于我在mac下 所以在网上找了很多资料提前看了. 我之前也 ...