hdu6161Big binary tree-(树形dp+空间优化)
2017-08-24 20:05
330 查看
题解:经过这个点最长路径可能是这个点的左右子树的最大值得和加上这个点的值或者是以这个点为根下面叶子节点爬上来的最大值加上max(它往真正的根爬途中经过的结点的值加上另外一棵子树)
因为这个结点的值更新了只有影响到它上面那些节点的最大值所以只要把影响的那些节点的值记录一下,其他的结点的最大值根据它向下能向下几层向左取或者向右取就可以了。所以我们只要记录那些被影响的节点的值,其他的不记录到时候按照上面的方法搜索一下即可,这样空间复杂度就是0(mlogn)如果把每个值都记录的话这题空间不够
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cstdio>
using namespace std;
typedef long long int ll;
#define lson 2*x
#define rson 2*x+1
map<int,ll>v;
map<int,ll>f;
int n,m;
int deep(int x){
int ans = 0;
while(x<=n){
ans++;
x *= 2;
}
return ans;
}
ll dp(int x){
if(x>n) return 0;
if(f.count(x)) return f[x];
ll ans;
if(v.count(x))
ans = v[x];
else
ans = x;
if(deep(rson)==deep(lson))
ans += dp(rson);
else
ans += dp(lson);
return ans;
}
ll search(int x){
ll ans = dp(lson)+dp(rson);
if(v.count(x))
ans += v[x];
else
ans += x;
if(x==1)
return ans;
ll sum = dp(x);
int pre;
while(x>1){
pre = x;
x /= 2;
sum += v.count(x)?v[x]:x;
if(pre&1)
ans = max(sum+dp(lson),ans);
else
ans = max(sum+dp(rson),ans);
}
return max(ans,sum);
}
void change(int x,ll y){
v[x] = y;
f[x] = max(dp(lson),dp(rson))+y;
x /= 2;
while(x){
f[x] = max(dp(lson),dp(rson));
if(v.count(x))
f[x]+=v[x];
else
f[x]+=x;
x /= 2;
}
}
int main(){
char s[20];
while(scanf("%d%d",&n,&m)!=EOF){
f.clear();
v.clear();
while(m--){
int x;
ll y;
scanf("%s%d",s,&x);
if(s[0]=='q')
printf("%I64d\n",search(x));
else{
scanf("%I64d",&y);
change(x,y);
}
}
}
return 0;
}
因为这个结点的值更新了只有影响到它上面那些节点的最大值所以只要把影响的那些节点的值记录一下,其他的结点的最大值根据它向下能向下几层向左取或者向右取就可以了。所以我们只要记录那些被影响的节点的值,其他的不记录到时候按照上面的方法搜索一下即可,这样空间复杂度就是0(mlogn)如果把每个值都记录的话这题空间不够
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cstdio>
using namespace std;
typedef long long int ll;
#define lson 2*x
#define rson 2*x+1
map<int,ll>v;
map<int,ll>f;
int n,m;
int deep(int x){
int ans = 0;
while(x<=n){
ans++;
x *= 2;
}
return ans;
}
ll dp(int x){
if(x>n) return 0;
if(f.count(x)) return f[x];
ll ans;
if(v.count(x))
ans = v[x];
else
ans = x;
if(deep(rson)==deep(lson))
ans += dp(rson);
else
ans += dp(lson);
return ans;
}
ll search(int x){
ll ans = dp(lson)+dp(rson);
if(v.count(x))
ans += v[x];
else
ans += x;
if(x==1)
return ans;
ll sum = dp(x);
int pre;
while(x>1){
pre = x;
x /= 2;
sum += v.count(x)?v[x]:x;
if(pre&1)
ans = max(sum+dp(lson),ans);
else
ans = max(sum+dp(rson),ans);
}
return max(ans,sum);
}
void change(int x,ll y){
v[x] = y;
f[x] = max(dp(lson),dp(rson))+y;
x /= 2;
while(x){
f[x] = max(dp(lson),dp(rson));
if(v.count(x))
f[x]+=v[x];
else
f[x]+=x;
x /= 2;
}
}
int main(){
char s[20];
while(scanf("%d%d",&n,&m)!=EOF){
f.clear();
v.clear();
while(m--){
int x;
ll y;
scanf("%s%d",s,&x);
if(s[0]=='q')
printf("%I64d\n",search(x));
else{
scanf("%I64d",&y);
change(x,y);
}
}
}
return 0;
}
相关文章推荐
- 洛谷P3994 Highway(树形DP+斜率优化+可持久化线段树/二分)
- hdu 1243 反恐训练营(DP+空间优化)
- zoj3951 Independent Set(树形dp,空间优化)
- hdu 1243 反恐训练营(DP+空间优化)
- bzoj3672: [Noi2014]购票(树形DP+斜率优化+可持久化凸包)
- POJ-3345 Bribing FIPA 树形DP+分组背包
- POJ1159求LCS长度 滚动数组优化空间
- 最长公共子序列 空间优化最长公共子序列
- dp优化专辑 D - Party at Hali-Bula [树形dp]
- hdu 5009 Paint Pearls(DP+链表优化)
- Apple Tree (树形dp+背包)
- 如何对ArcSDE空间网格大小进行优化?
- poj 3356 编辑距离(空间优化)
- ArcGIS Engine实现图层间空间选择的优化策略
- UVA 11078 Open Credit System(空间优化+时间优化)
- 剑指offer-第五章优化时间和空间效率(数组中的逆序对的总数)
- 多目标遗传算法NSGA-II在工业机器人关节空间轨迹优化上的应用
- HDU 5927 Auxiliary Set (树形DP+思维)
- 【OpenCV】 RGB和CIEXYZ颜色空间的转换及相关优化。
- zoj Special Subsequence 3349 (DP+线段树优化) 好题***