HDU 6161

来源:互联网 发布:网络直播怎么赚钱的 编辑:程序博客网 时间:2024/06/06 03:17

Big binary tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description

You are given a complete binary tree with n nodes. The root node is numbered 1, and node x's father node isx/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 13query 1query 2query 3query 4query 5query 6change 6 1query 1query 2query 3query 4query 5query 6
 

Sample Output
171717161717121212111212

题意:给你一个完全二叉树  每个结点的初始值是他自己的标号 

change x y  将x标号的值改为y

query x 询问经过x这个点的最大路径值


题解:对于更新  我们对他及祖先结点建立或更新结构体 

对于查询  我们从下往上查询即可


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespace std;typedef long long ll;struct node{    ll lef,rig,lab,mix;}e[3200000];map<int,int>sp1,sp;int cnt,n,m;ll getpos(ll x){    if(sp1.find(x)!=sp1.end())return sp1[x];    else return x;}int wei[33],l1;int nn;ll cal(int x,int n){//    return 0;    if(x>n)return 0;    int w[33];    int len=0,xx=x;    while(xx)w[++len]=(xx&1),xx>>=1;    reverse(w+1,w+1+len);    int f=1;    for(int i=1;i<=len&&f;i++){        f&=(w[i]==wei[i]);    }    ll re=0;    if(!f){        while(x<=n){            re+=x;            if(x<=n){                if(x*2+1<=n)x=x*2+1;                else x=x*2;            }        }    }    else {        re+=x;        for(int i=len+1;i<=l1;i++){            if(wei[i])x=x*2+1;            else x=x*2;            re+=x;            }    }    return re;}void query(int x){    ll now,ans;    if(sp.find(x)!=sp.end()){        now=e[sp[x]].mix;        ans=e[sp[x]].lef+e[sp[x]].rig+getpos(x);    }    else{        now=cal(x,n);        ans=cal(x*2,n)+cal(x*2+1,n)+getpos(x);    }    ll tp=x/2;    while(tp){        ll ops=getpos(tp);        if(x%2==0){            if(sp.find(tp)!=sp.end()){                ans=max(ans,now+e[sp[tp]].rig+ops);            }            else{                ans=max(ans,now+cal(tp*2+1,n)+ops);            }        }        else{            if(sp.find(tp)!=sp.end()){                ans=max(ans,now+e[sp[tp]].lef+ops);            }            else{                ans=max(ans,now+cal(tp*2,n)+ops);            }        }        x=tp;        now+=ops;        tp/=2;    }    printf("%lld\n",ans);}void update(int pos,ll val){    int now=pos;    ll pre=getpos(pos),tp=pos;    if(sp.find(now)!=sp.end()){        int labc=sp[now];        e[labc].mix=max(e[labc].lef,e[labc].rig)+val;        sp1[now]=val;    }    else{        sp[now]=++cnt;        e[cnt].lab=pos;        e[cnt].lef=cal(pos*2,n);        e[cnt].rig=cal(pos*2+1,n);        e[cnt].mix=max(e[cnt].lef,e[cnt].rig)+val;        sp1[now]=val;    }    now/=2;    while(now){        if(tp%2==0){            if(sp.find(now)!=sp.end()){                int labc=sp[now];                e[labc].lef=e[sp[tp]].mix;                e[labc].mix=max(e[labc].lef,e[labc].rig)+getpos(now);            }            else{                sp[now]=++cnt;                e[cnt].lab=now;                e[cnt].lef=e[sp[tp]].mix;                e[cnt].rig=cal(now*2+1,n);                e[cnt].mix=max(e[cnt].lef,e[cnt].rig)+getpos(now);            }        }        else{            if(sp.find(now)!=sp.end()){                int labc=sp[now];                e[labc].rig=e[sp[tp]].mix;                e[labc].mix=max(e[labc].lef,e[labc].rig)+getpos(now);            }            else{                sp[now]=++cnt;                e[cnt].lab=now;                e[cnt].lef=cal(now*2,n);                e[cnt].rig=e[sp[tp]].mix;                e[cnt].mix=max(e[cnt].lef,e[cnt].rig)+getpos(now);            }        }        tp=now;        now/=2;    }}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        char s[10];        nn=n;        cnt=0;        sp.clear();        sp1.clear();        int x,i,j;        ll y;        l1=0;        while(nn)wei[++l1]=(nn&1),nn>>=1;        reverse(wei+1,wei+1+l1);        for(i=1;i<=m;i++){            scanf("%s",s);            if(s[0]=='q'){                scanf("%d",&x);                query(x);            }            else{                scanf("%d%lld",&x,&y);                update(x,y);            }        }    }    return 0;}/*6 13change 6 1query 1*/


原创粉丝点击