2017多校九 01题 HDU6161 Big binary tree 树形dp+hash
来源:互联网 发布:sftp默认端口号 编辑:程序博客网 时间:2024/06/05 09:08
题目链接
题意
有一棵
修改某个点的权值
询问 经过某个点的 权值和最大的 链 的权值和
思路
考虑经过点
但这道题的重点在于数据范围——
至于算,就是尽量一路向右走(因为一旦要算就说明这棵子树内的点都没有被修改过),特殊情况是如果
Code
#include <bits/stdc++.h>#define lson(x) (x << 1)#define rson(x) (x << 1 | 1)using namespace std;typedef long long LL;int n, m;map<int, LL> dp, val;LL calc(int u) { if (u > n) return 0; if (dp[u]) return dp[u]; int temp = u, ldep = 0, rdep = 0; while (temp <= n) temp = lson(temp), ++ldep; temp = u; while (temp <= n) temp = rson(temp), ++rdep; if (ldep != rdep) temp = n; else temp /= 2; LL ret = 0; while (true) { ret += temp; if (temp == u) break; temp /= 2; } return ret;}LL uval, sum;void dfs(int u, int son) { if (u == 0) return; uval = max(uval, sum + (val[u] ? val[u] : u) + calc(son ^ 1)); if (u == 1) return; sum += val[u] ? val[u] : u; dfs(u/2, u);}LL ask(int u) { LL lval = calc(lson(u)), rval = calc(rson(u)); uval = sum = 0; dfs(u/2, u); LL mn = min(min(lval, rval), uval); return lval + rval + uval - mn + (val[u] ? val[u] : u);}void update(int u) { if (u == 0) return; dp[u] = max(calc(lson(u)), calc(rson(u))) + (val[u]?val[u]:u); if (u == 1) return; update(u/2);}void change(int u, LL x) { dp[u] = (dp[u]?dp[u]:calc(u)) - (val[u]?val[u]:u) + x; val[u] = x; update(u/2);}void work() { val.clear(); dp.clear(); char s[10]; while (m--) { scanf("%s", s); if (s[0] == 'q') { int u; scanf("%d", &u); printf("%lld\n", ask(u)); } else { int u; LL x; scanf("%d%lld", &u, &x); change(u, x); } }}int main() { while (scanf("%d%d", &n, &m) != EOF) work(); return 0;}
阅读全文
0 0
- 2017多校九 01题 HDU6161 Big binary tree 树形dp+hash
- HDU 6161 Big binary tree(树形DP)
- URAL 1018 Binary Apple Tree(树形dp入门题)
- Ural 1018 Binary Apple Tree 树形dp
- 【树形dp】Binary Tree Maximum Path Sum
- URAL 1018 Binary Apple Tree (树形DP)
- URAL 1018 Binary Apple Tree (树形DP)
- Ural 1018 Binary Apple Tree [树形dp]
- 【URAL1018】【树形DP】Binary Apple Tree题解
- hdu6161Big binary tree-(树形dp+空间优化)
- Ural 1018 Binary Apple Tree (树形DP)
- Ural 1018 Binary Apple Tree(树形dp)
- ural 1018 Binary Apple Tree(树形dp | 经典)
- URAL 1018 Binary Apple Tree(树形dp)
- ural 1018 Binary Apple Tree(树形DP)
- Ural 1018 Binary Apple Tree (树形dp)
- HDU6161
- hdu 6161 Big binary tree
- Linux 高级编程
- Java,封装代码
- Java网络编程详解
- SGU 326 Perspective(网络流)
- Android实现日夜间模式的三种常用方法(二)
- 2017多校九 01题 HDU6161 Big binary tree 树形dp+hash
- hdu 6188 贪心
- phpcms自定义导航栏当前栏目高亮
- java三大特性_封装
- java语言概述
- ife task1
- HwpViewer 2007(hwp文件阅读器) 官方版下载
- JavaScript_表单验证
- 普元 EOS Platform 7.2 Governor定时任务配置界面上的有状态和无状态选项是什么意思