传送门

题意

给出一棵树,每条边都有权值,有两种操作:

  • 把第p条边的权值改为x
  • 询问x,y路径上的权值最大的边

code

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; const int N = ;
struct Edge {
int to,nxt,w;
}e[];
int head[N],tot,tn,n;
int deth[N],son[N],fa[N],siz[N],bel[N],pos[N];
int mx[N<<],a[N],b[N],c[N],data[N]; void init() {
tot = tn = ;
memset(head,,sizeof(head));
memset(son,,sizeof(son));
}
inline void add_edge(int u,int v,int w) {
e[++tot].to = v,e[tot].w = w,e[tot].nxt = head[u],head[u] = tot;
}
void dfs1(int u,int pa) {
siz[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==pa) continue;
fa[v] = u;
deth[v] = deth[u] + ;
dfs1(v,u);
siz[u] += siz[v];
if (son[u]== || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u,int top) {
pos[u] = ++tn;
bel[u] = top;
if (!son[u]) return ;
dfs2(son[u],top);
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v != fa[u] && v != son[u]) dfs2(v,v);
}
}
void pushup(int rt) {
mx[rt] = max(mx[rt<<],mx[rt<<|]);
}
void build(int l,int r,int rt) {
if (l==r) {
mx[rt] = data[l];return ;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int p,int x) {
if (l==r) {
mx[rt] = x;return ;
}
int m = (l + r) >> ;
if (p <= m) update(lson,p,x);
else update(rson,p,x);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
return mx[rt];
}
int m = (l + r) >> ;
int ret = -1e9;
if (L <= m) ret = max(ret,query(lson,L,R));
if (R > m) ret = max(ret,query(rson,L,R));
return ret;
}
void Change(int p,int x) {
if (deth[a[p]] > deth[b[p]]) update(,n,,pos[a[p]],x);
else update(,n,,pos[b[p]],x);
}
int Ask(int x,int y) {
int ans = -1e9;
while (bel[x] != bel[y]) {
if (deth[bel[x]] < deth[bel[y]]) swap(x,y);
ans = max(ans,query(,n,,pos[bel[x]],pos[x]));
x = fa[bel[x]];
}
if (deth[x] > deth[y]) swap(x,y);
if (x != y) ans = max(ans,query(,n,,pos[x]+,pos[y]));
return ans;
}
int main() {
char opt[];
int T;scanf("%d",&T);
while (T--) {
init();
scanf("%d",&n);
for (int i=; i<n; ++i) {
scanf("%d%d%d",&a[i],&b[i],&c[i]);
add_edge(a[i],b[i],c[i]);
add_edge(b[i],a[i],c[i]);
}
deth[] = ;
dfs1(,);
dfs2(,);
for (int i=; i<n; ++i) {
if (deth[a[i]] > deth[b[i]]) data[pos[a[i]]] = c[i]; // 一定是pos
else data[pos[b[i]]] = c[i];
}
build(,n,);
scanf("%s",opt);
while (opt[]!='D') {
int x,y;
scanf("%d%d",&x,&y);
if (opt[]=='Q') printf("%d\n",Ask(x,y));
else Change(x,y);
scanf("%s",opt);
}
}
return ;
}

SPOJ375 Query on a tree(树链剖分)的更多相关文章

  1. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

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

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

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

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

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

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

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

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

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

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

  8. SPOJ 375 Query on a tree 树链剖分模板

    第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...

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

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

随机推荐

  1. Mysql 函数创建

    DELIMITER $$DROP FUNCTION IF EXISTS `shouy`.`Sel_FUNC_GOODS_type` $$ CREATE FUNCTION `shouy`.`Sel_FU ...

  2. ThreadLocal源码解析,内存泄露以及传递性

    我想ThreadLocal这东西,大家或多或少都了解过一点,我在接触ThreadLocal的时候,觉得这东西很神奇,在网上看了很多博客,也看了一些书,总觉得有一个坎跨不过去,所以对ThreadLoca ...

  3. LaTeX小技巧——File ended while scanning use of \@writefile错误的

    早上在修改编译论文时发现了这个问题,仔细检查代码并没发现错误,一时也找不到具体的解决办法.我一直以为是因为runaway argument的错误提示,可实际上就是因为aux文件没有完整输入,导致上次编 ...

  4. Windows Azure 配置Active Directory 主机(2)

    前一篇概况给大家介绍了,在云端部署一台DC 需要满足一些条件,接下来进入正题,云端VM安装域控制器具体步骤. 步骤1 :验证 主DC 的静态 IP 地址 1.登录到 Corp 网络上的 主DC. 2. ...

  5. Python各种参数类型

    1. Python的参数传递是值传递还是引 举例说明Python函数参数传递的几种形式,并说明函数传参是值传递还是引用传递 一.位置参数 调用函数时根据函数定义的参数位置来传递参数.例子: def p ...

  6. 进度条插件使用demo

    1.下载地址: http://down.htmleaf.com/1502/201502031710.zip 2.效果图: 3.HTML代码:其中80设置当前所占百分比,即蓝色部分比例:注意引入必须的j ...

  7. SC || 那些CheckStyle中的错误们

    lab5里给了我们一个checkstyle查代码风格的方法.. 然后 lab4代码 copy一份! 添加checkstyle! 项目 右键 checkstyle!(自信脸) 3s后——7256 war ...

  8. c#和Java中的接口

    使用场景: 在c#和Java中: 1.接口可以实现“多继承”(多实现),一个类只能继承自一个父类,但是可以实现多个接口. 2.接口解决了不同类型之间的多态问题,比如鱼与船不是同一类型,但是都能在水里“ ...

  9. 关于SQL语言的初步认识

    关于SQL语言的初步认识 1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义. 2.一个SQL表由行集构成,一行是列的序列(集合),每列与行对应一个数据项. 3.一个表或者是一 ...

  10. ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)

    在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...