SPOJ375.QTREE树链剖分
题意:一个树,a b c 代表a--b边的权值为c。CHANGE x y 把输入的第x条边的权值改为y,QUERY x y 查询x--y路径上边的权值的最大值。
第一次写树链剖分,其实树链剖分只能说是一种思想。树链剖分 就是 先选择从根节点到叶子节点的最长的路径的权值对应到线段树上,然后从一个子树的根节点到叶子的最长路径的权值对应到线段树上这样直到把所有的点都处理了,然后就是线段树区间查询最值了。
具体可以看这个博客。http://blog.sina.com.cn/s/blog_6974c8b20100zc61.html
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e4+;
int top[maxn],fa[maxn],son[maxn],dep[maxn],siz[maxn],seg_tree[maxn<<],head[maxn],pos[maxn];
int edge,tot;
struct
{
int to,next;
}e[maxn<<];
void add(int x,int y)
{
e[edge].to = y;
e[edge].next = head[x];
head[x] = edge++;
} void dfs(int r)
{
siz[r] = ;
son[r] = ;
for (int i = head[r]; i > ; i = e[i].next)
if (fa[r] != e[i].to)
{
dep[e[i].to] = dep[r] + ;
fa[e[i].to] = r;
dfs(e[i].to);
if (siz[e[i].to] > siz[son[r]])
son[r] = e[i].to;
siz[r] += siz[e[i].to];
}
} void build(int r,int f)
{
pos[r] = tot++;
top[r] = f;
if (son[r] > )
build(son[r],top[r]);
for (int i = head[r]; i > ; i = e[i].next)
{
if (fa[r] != e[i].to && son[r] != e[i].to)
build(e[i].to,e[i].to);
}
} void update(int l,int r,int o,int x,int v)
{
if (l == r)
{
seg_tree[o] = v;
return;
}
int mid = (l + r) >> ;
if (x <= mid)
update(l,mid,o<<,x,v);
if (x > mid)
update(mid+,r,o<<|,x,v);
seg_tree[o] = max(seg_tree[o<<],seg_tree[o<<|]);
} int query(int l,int r,int o,int ua,int ub)
{
if (ua <= l && ub >= r)
return seg_tree[o];
int mid = (l + r) >> ;
int t1,t2;
t1 = t2 = ;
if (ua <= mid)
t1 = query(l,mid,o<<,ua,ub);
if (ub > mid)
t2 = query(mid+,r,o<<|,ua,ub);
return max(t1,t2);
} int get_max(int ua,int ub)
{
int f1 = top[ua];
int f2 = top[ub];
int tmp = ;
while (f1 != f2)
{
if (dep[f1] < dep[f2])
swap(f1,f2),swap(ua,ub);
tmp = max(tmp,query(,tot,,pos[f1],pos[ua]));
ua = fa[f1],f1 = top[ua];
}
if (ua == ub)
return tmp;
if (dep[ua] > dep[ub])
swap(ua,ub);
tmp = max(tmp,query(,tot,,pos[son[ua]],pos[ub]));
return tmp;
} int d[maxn][];
void init()
{
int n;
scanf ("%d",&n);
memset(dep,,sizeof(dep));
memset(son,,sizeof(son));
memset(head,,sizeof(head));
memset(fa,,sizeof(fa));
memset(top,,sizeof(top));
int root = (n+)>>;
fa[root] = dep[root] =;
tot = edge = ;
int a,b,c;
for (int i = ; i < n; i++)
{
scanf ("%d%d%d",&a,&b,&c);
add(a,b),add(b,a);
d[i][] = a,d[i][] = b,d[i][] = c;
}
dfs(root);
build(root,root);
tot--;
for (int i = ; i < n; i++)
{
if (dep[d[i][]] < dep[d[i][]])
swap(d[i][],d[i][]);
update(,tot,,pos[d[i][]],d[i][]);
} }
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int t;
scanf ("%d",&t);
while (t--)
{
init();
char op[];
while (scanf ("%s",op),op[] != 'D')
{
int x,y;
scanf ("%d%d",&x,&y);
if (op[] == 'C')
update(,tot,,pos[d[x][]],y);
if (op[] == 'Q')
printf("%d\n",get_max(x,y));
}
}
return ;
}
SPOJ375.QTREE树链剖分的更多相关文章
- QTREE 树链剖分---模板 spoj QTREE
<树链剖分及其应用> 一文讲得非常清楚,我一早上就把他学会了并且A了这题的入门题. spoj QTREE 题目: 给出一棵树,有两种操作: 1.修改一条边的边权. 2.询问节点a到b的最大 ...
- SPOJ QTREE 树链剖分
树链剖分的第一题,易懂,注意这里是边. #include<queue> #include<stack> #include<cmath> #include<cs ...
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- 【学术篇】SPOJ QTREE 树链剖分
发现链剖这东西好久不写想一遍写对是有难度的.. 果然是熟能生巧吧.. WC的dalao们都回来了 然后就用WC的毒瘤题荼毒了我们一波, 本来想打个T1 44分暴力 然后好像是特判写挂了还是怎么的就只能 ...
- Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划
题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆ 输入文件:qtree.in ...
- [SPOJ375]QTREE - Query on a tree【树链剖分】
题目描述 给你一棵树,两种操作. 修改边权,查找边权的最大值. 分析 我们都知道,树链剖分能够维护点权. 而且每一条边只有一个,且唯一对应一个儿子节点,那么就把信息放到这个儿子节点上. 注意,lca的 ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- 树链剖分边权模板spoj375
树链剖分是树分解成多条链来解决树上两点之间的路径上的问题 如何求出树链:第一次dfs求出树上每个结点的大小和深度和最大的儿子,第二次dfs就能将最大的儿子串起来并hash(映射)到线段树上(或者其他数 ...
- 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 ...
随机推荐
- [置顶] API相关工作过往的总结之整体介绍
此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...
- Java组合与继承生成的类中构造函数的执行顺序
[程序实例] import java.util.*; class Meal{ Meal() { System.out.println("Meal Constructor"); } ...
- Python进阶(面向对象编程基础)(二)
1.初始化实例属性 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'ziv·chan' #定义Person类的__init__方法 ...
- Gson 基础教程 —— 自定义类型适配器(TypeAdapter)
1,实现一个类型适配器(TypeAdapter) 自定义类型适配器需要实现两个接口: JsonSerializer<T> JsonDeserializer<T> 和两个方法: ...
- 【iOS基础】NSURLConnection
一.大文件下载1.方案:利用NSURLConnection和它的代理方法1> 发送一个请求 // 1.URL NSURL *url = [NSURL URLWithString:@"h ...
- ETL-Career RoadMap
RoadMap: 1.Tester:sql的单体或批处理测试: 2. Application Developer 2.1 批处理手动工具(如何使用.如何调度批处理.如何生成批处理脚本): 2.2 批处 ...
- 获取当前位置信息-ios
locationManager= [[CLLocationManager alloc] init];//位置管理器 locationManager.desiredAccuracy = kCLLocat ...
- 常用渗透性测试工具(Tools for penetration testing)
常用渗透性测试工具 原文:http://hi.baidu.com/limpid/item/14a2df166adfa8cb38cb3068 对一个应用项目进行渗透性测试一般要经过三个步骤. 第一步, ...
- 使用spring @Scheduled注解运行定时任务、
曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...
- 如何用Github的gh-pages分支展示自己的项目
很多新同学觉得github不就是一个代码托管所吗,如何能展示项目呢?其实完全可以借助Github的gh-pages打造出自己的一个作品集,无论是对自己的提升整合还是日后的面试都大有裨益. 前置准备 G ...