HDU 6161.Big binary tree 二叉树

Big binary tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 670    Accepted Submission(s): 235

Problem Description


You are given a complete binary tree with n nodes. The root node is numbered 1, and node x‘s father node is ⌊x/2⌋. At the beginning, node x has a value of exactly x. We define the value of a path as the sum of all nodes it passes(including two ends, or one if the path only has one node). Now there are two kinds of operations:
1.  change u x Set node u‘s value as x(1≤u≤n;1≤x≤10^10)
2.  query u Query the max value of all paths which passes node u.

Input

There are multiple cases.
For each case:
The first line contains two integers n,m(1≤n≤10^8,1≤m≤10^5), which represent the size of the tree and the number of operations, respectively.
Then m lines follows. Each line is an operation with syntax described above.

Output

For each query operation, output an integer in one line, indicating the max value of all paths which passes the specific node.

Sample Input

6 13

query 1

query 2

query 3

query 4

query 5

query 6

change 6 1

query 1

query 2

query 3

query 4

query 5

query 6

Sample Output

17

17

17

16

17

17

12

12

12

11

12

12

Source

2017 Multi-University Training Contest - Team 9

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161

题意:题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号,然后有两种操作: 
1、把u点权值改为x 
2、查询所有经过u点的路径中,路径上的点权和最大。

思路:每次修改只会改变log(n)个节点,最多改变log(n)*m个节点,约为2.7*10^6个节点,但是n最大为10^8,所以只能用map存储。没有修改过的结点不需要存储,因为没有修改过的话一定是右儿子大,有以只需要往右走就能寻找到以当前点为根的最大路径点权和,但是如果左边的结点数比右边的多的话就需要往左了。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
const int N=2e5+100,M=1e5+100;
const int inf=0x3f3f3f3f;
const ll INF=1e18+7,mod=1e9+7;
ll n;
char s[100];
map<ll,ll> vis,deep;
ll getnum(ll tmp)
{
    ll ans=0;
    while(tmp<=n)
    {
        if(deep.find(tmp)!=deep.end()) return ans+deep[tmp];
        if(vis.find(tmp)==vis.end()) ans+=tmp;
        else ans+=vis[tmp];
        ll tmp1=tmp<<1,tmp2=tmp<<1|1;
        int h1=0,h2=0;
        while(tmp1<=n) tmp1=tmp1<<1,h1++;
        while(tmp2<=n) tmp2=tmp2<<1,h2++;
        if(h1==h2) tmp=tmp<<1|1;
        else tmp=tmp<<1;
    }
    return ans;
}
void getchild(ll u,ll &ch1,ll &ch2)
{
    ch1=getnum(u<<1),ch2=getnum(u<<1|1);
}
int main()
{
    int m;
    while(scanf("%lld%d",&n,&m)!=EOF)
    {
        vis.clear(),deep.clear();
        for(int i=1; i<=m; i++)
        {
            ll u,x;
            scanf("%s %lld",s,&u);
            if(s[0]==‘c‘)
            {
                scanf("%lld",&x);
                vis[u]=x;
                while(u>=1)
                {
                    ll ch1,ch2;
                    getchild(u,ch1,ch2);
                    if(vis.find(u)==vis.end()) deep[u]=max(ch1,ch2)+u;
                    else deep[u]=max(ch1,ch2)+vis[u];
                    u/=2;
                }
            }
            else
            {
                ll ch1,ch2;
                getchild(u,ch1,ch2);
                u=ch1>ch2?(u<<1):(u<<1|1);
                ll cou=max(ch1,ch2);
                ll ans=0;
                while(u>1)
                {
                    if(vis.find(u/2)==vis.end()) cou+=u/2;
                    else cou+=vis[u/2];
                    ans=max(ans,getnum(u^1)+cou);
                    u/=2;
                }
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}

时间: 2024-10-17 15:16:36

HDU 6161.Big binary tree 二叉树的相关文章

UVALive 6577 Binary Tree 二叉树的LRU串

今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一个向下无限延伸的二叉树.然后输入起点(通过只含LRU的字符串S,从根结点开始执行).LRU分别表示往左儿子走,往右儿子走,往爹娘处走(根结点的爹娘是自己,估计他是石头里蹦出来的). 然后输入一个可选步骤串T.可以选择T中的子序,从起点开始走.然后问可以走到多少个不同的结点. 比赛的时候不会做啊╮(╯

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

Maximum Depth of Binary Tree 二叉树的深度

Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 此题是经典的求二叉树深度深度题目,可采用递归的方式,以此求每个节点的左子树深度和右子树深度,然后返回该节点左右子树深度最大的那个即为该节点的深度 具体代码如下: 1 /** 2 *

Maximum Depth of Binary Tree 二叉树的最大深度

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 求二叉树的最大深度问题用到深度优先搜索DFS,递归的完美应用,跟求二叉树的最小深度问题原理相同.代码如下: /** * Definition for binary tree * s

[LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w

671. Second Minimum Node In a Binary Tree 二叉树中第二小节点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G

lintcode 容易题:Maximum Depth of Binary Tree 二叉树的最大深度

题目: 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / 4 5 这个二叉树的最大深度为3. 解题: 递归方式求树的深度,记住考研时候考过这一题 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeN

Leetcode 226: Invert Binary Tree(二叉树反转)

nvert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1