SPOJ 375 树链剖分 QTREE - Query on a tree
人生第一道树链剖分的题目,其实树链剖分并不是特别难。
思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护。
貌似大家都是从这篇博客上学的。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = + ; int n;
int tot;
vector<int> G[maxn];
int u[maxn], v[maxn], d[maxn]; int fa[maxn];
int top[maxn];
int id[maxn];
int L[maxn];
int son[maxn];
int sz[maxn]; void dfs(int u)
{
sz[u] = ; son[u] = ;
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(v == fa[u]) continue;
fa[v] = u;
L[v] = L[u] + ;
dfs(v);
sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
} void dfs2(int u, int tp)
{
id[u] = ++tot;
top[u] = tp;
if(son[u]) dfs2(son[u], tp);
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} int maxv[maxn << ]; void update(int o, int L, int R, int p, int val)
{
if(p < L || p > R) return ;
if(L == R) { maxv[o] = val; return ; }
int M = (L + R) / ;
update(o<<, L, M, p, val);
update(o<<|, M+, R, p, val);
maxv[o] = max(maxv[o<<], maxv[o<<|]);
} int query(int o, int L, int R, int qL, int qR)
{
if(qR < L || qL > R) return ;
if(qL <= L && R <= qR) return maxv[o];
int M = (L + R) / ;
return max(query(o<<, L, M, qL, qR), query(o<<|, M+, R, qL, qR));
} int QUERY(int u, int v)
{
int ans = ;
int t1 = top[u], t2 = top[v];
while(t1 != t2)
{
if(L[t1] < L[t2]) { swap(u, v); swap(t1, t2); }
ans = max(ans, query(, , tot, id[t1], id[u]));
u = fa[t1]; t1 = top[u];
}
if(u == v) return ans;
if(L[u] < L[v]) swap(u, v);
ans = max(ans, query(, , tot, id[son[v]], id[u]));
return ans;
} int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++) G[i].clear();
fa[] = L[] = ;
for(int i = ; i < n; i++)
{
scanf("%d%d%d", u + i, v + i, d + i);
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
} dfs();
tot = ;
dfs2(, ); memset(maxv, , sizeof(maxv));
for(int i = ; i < n; i++)
{
if(L[u[i]] < L[v[i]]) swap(u[i], v[i]);
update(, , tot, id[u[i]], d[i]);
} char op[];
while(scanf("%s", op) && op[] != 'D')
{
int x, y; scanf("%d%d", &x, &y);
if(op[] == 'Q')
{
printf("%d\n", QUERY(x, y));
}
else
{
update(, , tot, id[u[x]], y);
}
}
} return ;
}
代码君
SPOJ 375 树链剖分 QTREE - Query on a tree的更多相关文章
- SPOJ 375 树链剖分
SPOJ太慢了,SPOJ太慢了, 题意:给定n(n<=10000)个节点的树,每条边有边权,有两种操作:1.修改某条变的边权:2.查询u,v之间路径上的最大边权. 分析:树链剖分入门题,看这里: ...
- SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...
- SPOJ 375 (树链剖分+线段树)
题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...
- spoj 375 树链剖分 模板
QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...
- spoj 375 树链剖分模板
/* 只是一道树链刨分的入门题,作为模板用. */ #include<stdio.h> #include<string.h> #include<iostream> ...
- 数据结构(并查集||树链剖分):HEOI 2016 tree
[注意事项] 为了体现增强版,题目限制和数据范围有所增强: 时间限制:1.5s 内存限制:128MB 对于15% 的数据,1<=N,Q<=1000. 对于35% 的数据,1<=N,Q ...
- 树链剖分【CF343D】Water Tree
Description Mad scientist Mike has constructed a rooted tree, which consists of nnvertices. Each ver ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- SPOJ 375 Query on a tree(树链剖分)(QTREE)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- Web.config文件 详解
一.认识Web.config文件Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式), ...
- 用TextWriterTraceListener实现建议log文件记录
log4net之类3方组件确实很方便,但是想写个小小的demo之类的程序,有点用不起啊. 微软自带的TraceListener要实现一个简易的日志帮助类还是很简单的,直接上代码,自己备用,也希望对同样 ...
- 移动端之js控制rem,适配字体
方法一:设置fontsize 按照iphone 5的适配 1em=10px 适配320 // “()()”表示自执行函数 (function (doc, win) { var docEl = ...
- AnyCAD OpenSource 版本下载和编译
下载: SVN下载地址:https://anycad.svn.codeplex.com/svn 或者直接下载:http://anycad.codeplex.com/SourceControl/late ...
- log4j.properties配置详情
log4j: log for java 是Apache的一个开源项目! 00.将我们的日志信息,输出到指定的位置(控制台 文件中) 01.我们可以控制每一条日志的输出格式 02.我们设置日志信息的 ...
- zuul忽略表达式
如果有error过滤器,会进入error
- Swift 扩展(Extension)总结
概要 扩展是给已经存在的类(class),结构体(structure),枚举类型(enumeration)和协议(protocol)增加新的功能.类似Objective-C中的Category,不同的 ...
- 如何将SAP Multi Target应用部署到SAP云平台的Cloud Foundry环境去
SHINA是SAP HANA Interactive Education的缩写,是一个demo应用,用于演示如何开发SAP HANA原生应用. 这个应用包含了sample数据以及HANA数据库表,vi ...
- spring security 2.x HttpSessionEventPublisher 以及listener配置
在环境为spring security2.x时 *JDK6 spring 2* 正确的filter路径是:org.springframework.security.ui.session.HttpSes ...
- Object Modeling
https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/Objec ...