zoukankan      html  css  js  c++  java
  • CodeForces 916E Jamie and Tree(树链剖分+LCA)

    To your surprise, Jamie is the final boss! Ehehehe.

    Jamie has given you a tree with n vertices, numbered from 1 to n. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.

    Jamie also gives you three types of queries on the tree:

    v — Change the tree's root to vertex with number v.

    u v x — For each vertex in the subtree of smallest size that contains u and v, add x to its value.

    v — Find sum of values of vertices in the subtree of vertex with number v.

    A subtree of vertex v is a set of vertices such that v lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root.

    Show your strength in programming to Jamie by performing the queries accurately!

    Input

    The first line of input contains two space-separated integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of vertices in the tree and the number of queries to process respectively.

    The second line contains n space-separated integers a1, a2, ..., an ( - 108 ≤ ai ≤ 108) — initial values of the vertices.

    Next n - 1 lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ n) describing edge between vertices ui and vi in the tree.

    The following q lines describe the queries.

    Each query has one of following formats depending on its type:

    v (1 ≤ v ≤ n) for queries of the first type.

    u v x (1 ≤ u, v ≤ n,  - 108 ≤ x ≤ 108) for queries of the second type.

    v (1 ≤ v ≤ n) for queries of the third type.

    All numbers in queries' descriptions are integers.

    The queries must be carried out in the given order. It is guaranteed that the tree is valid.

    Output

    For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.

    Examples
    input
    Copy
    6 7
    1 4 2 8 5 7
    1 2
    3 1
    4 3
    4 5
    3 6
    3 1
    2 4 6 3
    3 4
    1 6
    2 2 4 -5
    1 4
    3 3
    output
    Copy
    27
    19
    5
    input
    Copy
    4 6
    4 3 5 6
    1 2
    2 3
    3 4
    3 1
    1 3
    2 2 4 3
    1 1
    2 2 4 -3
    3 1
    output
    Copy
    18
    21
    Note

    The following picture shows how the tree varies after the queries in the first sample.

    题意(来自mangoyang大佬)

     

    有一棵n个节点的有根树,标号为1-n,你需要维护一下三种操作

     

    1.给定一个点v,将整颗树的根变为v

     

    2.给定两个点u, v,将lca(u, v)所在的子树都加上x

     

    3.给定一个点v,你需要回答以v所在的子树的权值和

    题解:

    这道题大概有以下几个难点


    首先是换根情况下的子树权值和查询,但如果你做过洛谷P3979遥远的国度这道题,而且你还是用树链剖分写的,你大致就会做这个东西了。

    维护一个LCA,然后从根节点跳到当前这个子树根节点的儿子所处的深度,记这个点为A,如果A点的父亲是我们询问的子树根节点,那么根节点在子树中,此时当前的新子树就是除了A点和A点的原子树以外的所有点,否则的话就是子树根节点的原子树,当然也要特判一下,如果根节点刚好是要查询的子树根节点,那么查询范围就是全部节点

    修改也是同理的

    然后是包括u和v的最小子树,画一下图就可以知道子树的根节点应该是u到根节点的路径与v到根节点的路径的第一个交点

    这个交点可以通过对根和u,根和v,v和u两两取LCA后取深度最大的点获得,挺简单的想法,可以自行模拟体会一下

    假设我们已经获得了这个根节点,那么之后的操作就是换根情况下子树的权值修改了,这在上面已经提到过了,就不再赘述了。

     

     

     

     

     

  • 相关阅读:
    java陷阱一箩筐面试
    应用MVC设计模式解决J2ME应用程序导航问题 mingjava
    我怎么了
    运营商掘宝手机游戏 无限“钱途”引人竞折腰
    网络工程师考试大纲
    java 正则
    java集合使用——LinkedList
    java集合使用——HashMap
    java集合使用——HashSet
    JAVA集合使用——基本概念
  • 原文地址:https://www.cnblogs.com/stxy-ferryman/p/8795744.html
Copyright © 2011-2022 走看看