Codeforces 68D - Half-decay Tree
题意
有一颗高度为 \(h\) 的完全二叉树(即点数为 \(2^{h+1}-1\) ),有两种操作:
add x y给 \(x\) 点的权值加 \(y\)decay一次衰变定义为选择一个叶子节点,断掉它到根的所有边,这样整个树会变成很多个连通块,一个连通块的权值是其中所有点的权值和;这个衰变的权值为这些连通块的权值中的最大值。这个操作要求输出随机选一个叶子进行衰变的期望权值。(衰变之间互不影响)
\(h\le 30,q\le 10^5\) 。
分析
一般这种完全二叉树都要利用其深度很小的性质。
可以发现,衰变的时候,最终得到的连通块都是由一个点和其一个子树构成的。对衰变求期望其实是求所有节点的衰变权值和。
对于一个点 \(x\) ,设其左儿子的子树权值和为 \(s_l\) ,右儿子的子树权值和为 \(s_r\) ,那么显然,若 \(s_l\ge s_r\) ,那么右子树中的所有点在衰变的时候,所取到的最大连通块都不可能使右子树中的。所以我们用当前的 \(\max\) 和左子树加当前点即可直接计算右子树的答案,接着递归进入左子树即可。
我们要维护的东西就是子树的权值和。
因此单次操作是 \(O(h\log n)\) 的(用 map )。
代码
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
typedef __gnu_pbds::cc_hash_table<int,int> Map;
typedef long long giant;
inline char nchar() {
static const int bufl=1<<20;
static char buf[bufl],*a=NULL,*b=NULL;
return a==b && (b=(a=buf)+fread(buf,1,bufl,stdin),a==b)?EOF:*a++;
}
template<class T> inline T read() {
T x=0,f=1;
char c=nchar();
for (;!isdigit(c);c=nchar()) if (c=='-') f=-1;
for (;isdigit(c);c=nchar()) x=x*10+c-'0';
return x*f;
}
template<> char read<char>() {
char c=nchar();
for (;!isalpha(c);c=nchar());
for (char f=nchar();isalpha(f);f=nchar());
return c;
}
int h,q,last;
namespace tree {
Map sum;
void add(int x,int d) {
for (;x;x>>=1) sum[x]+=d;
}
inline int size(int x) {
return 1<<(h-(31-__builtin_clz(x)));
}
giant calc(int x,int mx) {
if (x>=last) return max(mx,sum[x]);
int lc=x<<1,rc=lc+1,sl=sum[lc],sr=sum[rc],sx=sum[x];
giant ret=0;
if (sl>sr) swap(lc,rc),swap(sl,sr);
ret=(giant)size(lc)*max(sx-sl,mx);
ret+=calc(rc,max(mx,sx-sr));
return ret;
}
long double ans() {
long double ret=calc(1,0);
ret/=last;
return ret;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
h=read<int>(),q=read<int>(),last=1<<h;
while (q--) {
char o=read<char>();
if (o=='a') {
int x=read<int>(),y=read<int>();
tree::add(x,y);
} else printf("%.12lf\n",(double)tree::ans());
}
return 0;
}
Codeforces 68D - Half-decay Tree的更多相关文章
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- 【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree
Prelude 很好的模板题. 传送到Codeforces:(* ̄3 ̄)╭ Solution 首先要会DSU on Tree,不会的看这里:(❤ ω ❤). 众所周知DSU on Tree是可以用来处 ...
- CodeForces 396C On Changing Tree
On Changing Tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
随机推荐
- C. Sad powers
You're given Q queries of the form (L, R). For each query you have to find the number of such x that ...
- 异常处理简单例子--python
捕获所有异常 #!/usr/bin/pythona = 10b = 0try: c = a/b print c print 'nothing happen...'#todo: catch all ex ...
- React Native创建一个APP
React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 React 抽 ...
- 【本地服务器】json-server搭建本地https服务器(windows)
(一)用json-server搭建简单的服务器 (搭建出来的服务器地址为localhost:3000) 1.新建Mockjson文件夹,进入该文件夹目录,运行命令 npm install -g jso ...
- ajax入门简述
ajax 全称为 “Asynchronous JavaScript and XML”(异步 JavaScript 和 XML),是一种创建交互式网页应用的网页开发技术,本质上是一个浏览器端的技术,就是 ...
- 20155302《网络对抗》Exp3 免杀原理与实践
20155302<网络对抗>Exp3 免杀原理与实践 实验要求 1.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编 ...
- # 2017-2018-2 20155319『网络对抗技术』Exp5:MSF基础应用
2017-2018-2 20155319『网络对抗技术』Exp5:MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:使用者利用漏洞进行攻击 ...
- vue build,本地正常访问,服务器上,网页一刷新是404,解决办法
服务器报错如下图: 此原因,是服务器配置的原因,跟build代码本身无关 以ftp为例,在/etc/nginx/conf.d文件夹下,找到xxx.conf,修改成自己需要的路径即可 位置如下两张图:
- WPF编程,自定义鼠标形状的一种方法。
原文:WPF编程,自定义鼠标形状的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details/8727 ...
- stm32f051 DMA需要注意的一点
STM32f051的DMA注意事项 问题说明:在使用ADC的DMA通道时,遇到了序列转换的乱序问题,我使用的是DMA循环模式,但是采集的数据却总是错的:第二个内存地址存放的是ADC序列转换中的第一个通 ...