2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

Big binary tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 597    Accepted Submission(s): 207

Problem Description
2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
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.
 
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 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 Output
17
17
17
16
17
17
12
12
12
11
12
12
Source
分析:考虑dp,f(x)表示从点x开始向下走得到的最大的点权和,查询直接从x开始向上走更新答案即可。
考虑快速算 f(x) 对于子树内没有被修改过的点的 f(x) 可以快速分类讨论算出,而不满足本条件的点只有 O(mlogm) 个,在hash上dp即可。
下面给出AC代码:
 #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define ll long long
#define M 1000000007
#define all(a) a.begin(), a.end() int n, m;
char s[];
map<int, ll> mp;
map<int, int> amp; inline ll askmax(int u){
if(u > n) return ;
if(mp.count(u)) return mp[u];
else{
int l = u, r = u;
while(l * <= n){
l <<= ;
r = (r << ) | ;
}
r = min(r, n);
ll res = ;
while(r >= u) res += r, r >>= ;
return res;
}
} inline int ask(int x){
return amp.count(x) ? amp[x] : x;
} int main(){
while(~scanf("%d%d", &n, &m)){
mp.clear();
amp.clear();
while(m--){
int x, v;
scanf("%s", s);
if(s[] == 'c'){
scanf("%d%d", &x, &v);
amp[x] = v;
for(; x; x >>= )
mp[x] = max(askmax(x << ), askmax((x << ) | )) + ask(x);
}else{
scanf("%d", &x);
int px = x;
ll res = , now = ;
for(; x >> ;){
bool k = ~x & ; x >>= ;
now += ask(x);
ll tmp = askmax(x << | k);
if(now + tmp > res) res = now + tmp;
}
res += askmax(px);
res = max(res, askmax(px << ) + askmax(px << | ) + ask(px));
printf("%lld\n", res);
}
}
} #ifndef ONLINE_JUDGE
system("pause");
#endif
return ;
}
上一篇:如何区别?三个角度看云计算与网格计算


下一篇:2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】