题目描述

给你一棵树,两种操作。
修改边权,查找边权的最大值。

分析

我们都知道,树链剖分能够维护点权。
而且每一条边只有一个,且唯一对应一个儿子节点,那么就把信息放到这个儿子节点上。
注意,lca的信息不能算到,也就是当查询到了\(top[u]=top[v]\)的时候,要从\(idx[u]+1\)到\(v\)的查询,因为\(idx[u]\)为lca。

代码

#include <bits/stdc++.h>
#define ms(a, b) memset(a, b, sizeof(a))
#define ll long long
#define ull unsigned long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define db double
#define Pi acos(-1)
#define eps 1e-8
#define N 200005
using namespace std;
template <typename T> void read(T &x) {
    x = 0; T fl = 1; char ch = 0;
    for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') fl = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
    x *= fl;
}
template <typename T> void write(T x) {
    if (x < 0) x = -x, putchar('-');
    if (x > 9) write(x / 10); putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) { write(x); puts(""); }
struct edge {
    int to, nt, w;
}E[N << 1];
int H[N], top[N], dep[N], son[N], idx[N], fa[N], sz[N], val[N], a[N], U[N], V[N];
int cnt, tot = 0, n;
void add_edge(int u, int v, int w) {
    E[++ cnt] = (edge){v, H[u], w};
    H[u] = cnt;
}
struct Segment_Tree {
    #define lc (nod << 1)
    #define rc (nod << 1 | 1)
    struct node {
        int mx, l, r;
    }tr[N << 2];
    void pushup(int nod) { tr[nod].mx = max(tr[lc].mx, tr[rc].mx); }
    void build(int nod, int l, int r, int *a) {
        tr[nod].l = l, tr[nod].r = r; tr[nod].mx = -inf;
        if (l == r) { tr[nod].mx = a[l]; return; }
        int mid = (l + r) >> 1;
        build(lc, l, mid, a); build(rc, mid + 1, r, a);
        pushup(nod);
    }
    void update(int nod, int k, int val) {
        int l = tr[nod].l, r = tr[nod].r;
        if (l == r) { tr[nod].mx = val; return; }
        int mid = (l + r) >> 1;
        if (k <= mid) update(lc, k, val); else update(rc, k, val);
        pushup(nod);
    }
    int query(int nod, int ql, int qr) {
        int l = tr[nod].l, r = tr[nod].r;
        if (ql <= l && r <= qr) return tr[nod].mx;
        int mid = (l + r) >> 1, res = -inf;
        if (ql <= mid) res = max(res, query(lc, ql, qr));
        if (qr > mid) res = max(res, query(rc, ql, qr));
        return res;
    }
}sgt;
void dfs1(int u, int ft, int dp) {
    dep[u] = dp; fa[u] = ft; sz[u] = 1;
    int maxson = -1;
    for (int e = H[u]; e; e = E[e].nt) {
        int v = E[e].to; if (v == fa[u]) continue;
        dfs1(v, u, dp + 1);
        a[v] = E[e].w;  sz[u] += sz[v];
        if (sz[v] > maxson) maxson = sz[v], son[u] = v;
    }
}
void dfs2(int u, int tp) {
    top[u] = tp; idx[u] = ++ tot; val[tot] = a[u];
    if (!son[u]) return; dfs2(son[u], tp);
    for (int e = H[u]; e; e = E[e].nt) {
        int v = E[e].to;
        if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}
char opt[10];
int query_chain(int u, int v) {
    int res = -inf;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        res = max(res, sgt.query(1, idx[top[u]], idx[u]));
        u = fa[top[u]];
    }
    if (dep[u] > dep[v]) swap(u, v);
    res = max(res, sgt.query(1, idx[u] + 1, idx[v]));
    return res;
}
int main() {
    read(n);
    for (int i = 1; i < n; i ++) {
        int u, v, w; read(u); read(v); read(w);
        add_edge(u, v, w); add_edge(v, u, w);
        U[i] = u; V[i] = v;
    }
    dfs1(1, 0, 1); dfs2(1, 1);
    sgt.build(1, 1, n, val);
    while (scanf("%s", opt) != EOF) {
        if (opt[0] == 'D') return 0;
        int x, y; read(x); read(y);
        if (opt[0] == 'Q') writeln((x != y)? query_chain(x, y): 0);
        else {
            int u = U[x], v = V[x];
            if (fa[v] == u) swap(u, v);
            sgt.update(1, idx[u], y);
        }
    }
    return 0;
}

[SPOJ375]QTREE - Query on a tree【树链剖分】的更多相关文章

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

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

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

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

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

  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——树链剖分整理

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

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

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

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

  9. SPOJ375 Query on a tree(树链剖分)

    传送门 题意 给出一棵树,每条边都有权值,有两种操作: 把第p条边的权值改为x 询问x,y路径上的权值最大的边 code #include<cstdio> #include<algo ...

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

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

随机推荐

  1. 替换iframe的内容

    一般就是点个按钮然后在不跳转页面的情况下显示另外一个页面的内容,显示的速度比较快,ifream还算是常用的吧 用的时候实现方式有以下几种, 1.替换src路径(觉得麻烦,没接触过,就不这样干了) 2. ...

  2. 多线程系列之十一:Two-Phase Termination模式

    一,Two-Phase Termination模式 翻译过来就是:分两阶段终止 二,示例程序 public class CountupTread extends Thread { private lo ...

  3. 【学习总结】之 3Blue1Brown系列

    刷知乎看到的,各种力荐. YouTube: https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw/featured B站: https:// ...

  4. [官网]Red Hat Enterprise Linux Release Dates

    Red Hat Enterprise Linux Release Dates https://access.redhat.com/articles/3078 The tables below list ...

  5. display设置弹性盒布局

    转自:http://blog.csdn.net/itbwy/article/details/52648711 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖  ...

  6. 爬虫 之Requests库的详细使用

    1.什么是Requests? Requests是用Python语言编写的,基于urllib3来改写的,采用Apache2 Licensed 来源协议的HTTP库. 它比urllib更加方便,可以节约我 ...

  7. VSCode 汉化

    https://jingyan.baidu.com/article/7e44095377c9d12fc1e2ef5b.html

  8. 建议1---理解Pythonic的概念

    对于Pythonic的概念,众人都有自己的看法,但大家心中都认同一个更具体的指南,即Tim Peters的<The Zen of Python>.在这一篇充满禅意的诗篇中,有几点非常深入人 ...

  9. 如何使用 Yum Repository 安装指定版本的 MySQL

    自从从使用 debian 系的 apt-get 转到使用 yum 工具之后一直不是很习惯,也没有去看过很多工具包安装的时候到底影响到了哪些文件等.这次借这次社区版 MySQL 安装来一并梳理一下. 首 ...

  10. Python:matplotlib绘制直方图

    使用hist方法来绘制直方图:     绘制直方图,最主要的是一个数据集data和需要划分的区间数量bins,另外你也可以设置一些颜色.类型参数: plt.hist(np.random.randn(1 ...