传送门

题意

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

  • 把第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. webpack.config.js====插件purifycss-webpack,提炼css文件

    1. 安装:打包编译时,可以删除一些html中没有使用的选择器,如果html页面中没有class=a class="b"的元素,.a{}.b{}样式不会加载 cnpm instal ...

  2. 第一课:K线

    1       K线是根据价格走势中形成的四个价位(开盘价.收盘价.最高价.最低价)绘制而成的.K线是最基本的描述股价涨跌的表现符号(记录某种股票一天的价格变动情况). K线构造的四个价格因素:开盘价 ...

  3. 判断字符串string是数字、json结构、xml结构

    import org.json.JSONException; import org.json.JSONObject; import org.dom4j.DocumentException; impor ...

  4. Kendo UI Widgets 概述

    UI Widgets 概述 Kendo UI 是基于 jQuery 库开发的,Kendo UI widgets 是以 jQuery 插件形式提供的.这些插件的名称基本上都是以 kendo 作为前缀.比 ...

  5. 数据结构-List接口-LinkedList类-Set接口-HashSet类-Collection总结

    一.数据结构:4种--<需补充> 1.堆栈结构:     特点:LIFO(后进先出);栈的入口/出口都在顶端位置;压栈就是存元素/弹栈就是取元素;     代表类:Stack;     其 ...

  6. Ubuntu 16.04 server版本开机启动脚本不支持

    Ubuntu16.04开机启动的脚本一直不支持,错误用在将开机启动脚本放到了home/usr/的目录下,应该放到/root才能正常启动.#!/bin/sh -e ## rc.local## This ...

  7. copyout函数

    copyout Kernel Service   Purpose Copies data between user and kernel memory. Syntax #include <sys ...

  8. 【LeetCode】2.Add Two Numbers 链表数相加

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. LibreOJ #2130. 「NOI2015」软件包管理器

    内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名 树链剖分+线段树 屠龙宝刀点击就送 #include <vector> ...

  10. js中异步方案比较完整版(callback,promise,generator,async)

    JS 异步已经告一段落了,这里来一波小总结 1. 回调函数(callback) setTimeout(() => { // callback 函数体 }, 1000) 缺点:回调地狱,不能用 t ...