hdu 6161--Big binary tree(思维--压缩空间)
2018-06-18 03:59:41来源:未知 阅读 ()
题目链接
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.
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.
#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; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:读书笔记--C陷阱与缺陷(一)
下一篇:面向对象中装饰模式的理解
- CF662C Binary Table 2020-04-26
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash