第一次写树剖~

#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 树链剖分模板的更多相关文章

  1. 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 ...

  2. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

  3. 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 ...

  4. SPOJ QTREE Query on a tree ——树链剖分 线段树

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

  5. SPOJ QTREE Query on a tree --树链剖分

    题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...

  6. Query on a tree 树链剖分 [模板]

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  7. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  8. spoj 375 QTREE - Query on a tree 树链剖分

    题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...

  9. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

随机推荐

  1. 扩展Wcf call security service, 手动添加 Soap Security Head.

    有次我们有个项目需要Call 一个 Java 的 web service, Soap包中需要一个 Security Head <soapenv:Header> <wsse:Secur ...

  2. table 边框

    <table width="100%" border="0" cellpadding="2" cellspacing="0& ...

  3. Android 监听返回键、HOME键

    拦截返回键,HOME键,继承BaseActivity即可 import android.app.Activity; import android.content.BroadcastReceiver; ...

  4. 非常好的Oracle教程【转】

    http://www.blogjava.net/kiant/articles/234781.html Oracle 笔记(四).SQL 几个要点 附录: 1.SQL 简介 2.SQL 操作符 3.Or ...

  5. 并发框架Disruptor浅析

    1.引言 Disruptor是一个开源的Java框架,它被设计用于在生产者—消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS)和尽量低的延迟.D ...

  6. 利用 async & await 的异步编程

    走进异步编程的世界 - 开始接触 async/await 利用 async & await 的异步编程 async 的三大返回类型 公司技术需求备忘录

  7. iTunes

    我们的电脑都要下载比较好的显卡那项 https://support.apple.com/zh_CN/downloads/itunes

  8. 07-阻塞赋值与非阻塞赋值原理分析——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线--普利斯队长精心奉献   实验目的:掌握阻塞赋值与非阻塞赋值的区别 实验平台:无 实验原理:     阻塞赋值,操作符为"=","阻塞"是指在进程语句( ...

  9. html table表头斜线

    关于htnl的table的表头斜线,符合表格设计规范,<style> .biaotou { line-height: 5px; text-align: left; } .biaotou { ...

  10. php 笔试题

    1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21 解:echo date(‘Y-m-d H:i:s’, strtotime(‘-1 day’)); 原因: format 字符说 ...