Hdu 6161 -- Big binary tree (thinking -- compressing space), hdu6161 -- big

Source: Internet
Author: User

Hdu 6161 -- Big binary tree (thinking -- compressing space), hdu6161 -- big

Question Link

 

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 too x/2 then ⌋. 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.
 

 

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

 

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

 

Sample Input6 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 Output171717161717121212111212 question: there is a Complete Binary Tree composed of n nodes, node number 1 ~ N. For an I node, its father's day is I/2 and its initial weight is x. Now there are m operations: 1. Modify the node I weight to x. 2. Calculate the maximum weight and value of the node on the path in all the paths passing through the I node. Idea: Because n is large and 1e8, it is not possible to directly build a tree for computing, but how to calculate a tree for it? We found that m is 1e5, so we can use map to save the modified node. Every time we modify a node u, we can calculate and use the map to store the maximum weight sum of the u from the u to its leaf node, and search for the u's ancestor node up, map is also used to store the corresponding maximum path weight and. We only need to search for the ancestor nodes from u to obtain the maximum path weights of u. For example, calculate the maximum path weight and of D. There are the following paths: 1. Start with another leaf node from D to D. 2. A leaf node from leaf node d B to leaf node B. 3. From A leaf node of D to A leaf node of B D. Therefore, the greatest path weight after D and the time are calculated by searching for the ancestor node of D starting from D. The Code is as follows:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <map>using namespace std;typedef long long LL;map<int,LL>mp;map<int,LL>mx;LL ans;int n,m;int pos[100];void init(){    int tmp=n;    int deep=(int)log2(n)+1;    for(int i=deep;i>=1;i--)    {        pos[i]=tmp;        tmp>>=1;    }}void cal(int x){   if(mp.count(x)) return ;   if(x>n) { mp[x]=0; return ; }   int deep=(int)log2(x)+1;   LL tmp=0;   for(int i=x;i<=n;i=(i<<1|1)) tmp+=i;   if(pos[deep]==x){       LL sum=0;       for(int i=deep;;i++)       {           sum+=pos[i];           if(pos[i]==n) break;       }       tmp=max(tmp,sum);   }   mp[x]=tmp;}void update(int x){    if(!x) return ;    LL y;    if(mx.count(x)==0) y=x;    else y=mx[x];    cal(x<<1);    cal(x<<1|1);    mp[x]=max(mp[x<<1],mp[x<<1|1])+y;    update(x>>1);}void query(LL sum,int x,int son){    if(!x) return ;    cal(x<<1);    cal(x<<1|1);    if(!mx.count(x)) mx[x]=x;    ans=max(ans,sum+mp[son^1]+mx[x]);    sum+=mx[x];    query(sum,x>>1,x);}int main(){    char s[10];    while(scanf("%d",&n)!=EOF)    {        init();        mp.clear();        mx.clear();        scanf("%d",&m);        while(m--)        {            scanf("%s",s);            if(s[0]=='q')            {                int x; scanf("%d",&x);                cal(x<<1);                cal(x<<1|1);                if(!mx.count(x)) mx[x]=x;                ans=mp[x<<1]+mp[x<<1|1]+mx[x];                cal(x);                query(mp[x],x>>1,x);                printf("%lld\n",ans);            }            else            {               int x; LL y; scanf("%d%lld",&x,&y);               mx[x]=y;               update(x);            }        }    }    return 0;}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.