题意

有一颗高度为 \(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的更多相关文章

  1. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  2. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  3. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  4. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  6. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  7. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  8. 【题解】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是可以用来处 ...

  9. CodeForces 396C On Changing Tree

    On Changing Tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

随机推荐

  1. java.lang.RuntimeException: Fail to connect to camera service

    玩自定义照相机的时候出现了:java.lang.RuntimeException: Fail to connect to camera service 讲过百度和Google后知道是权限少加了.(试验 ...

  2. Mapnik初学笔记

    前言:夏天总是感觉想要睡觉,一心想颓废的我却要一周六天都要处于工作状态,但有些事虽然麻烦,但还是要去做,不由得想起火影忍者里面鹿丸这一个角色,有时候真能理解他的心理状态,或许我应该向他学习:善于思考的 ...

  3. Dozer 使用小结

    这篇文章是本人在阅读Dozer官方文档(5.5.1版本,官网已经一年多没更新了)的过程中,整理下来我认为比较基础的应用场景. 本文中提到的例子应该能覆盖JavaBean映射的大部分场景,希望对你有所帮 ...

  4. Hadoop集群nodes unhealthy解决方法

    在搭建好Hadoop集群之后,所有服务均可正常启动,但是在运行MapReduce程序的时候,发现任务卡在7/09/07 22:28:14 INFO mapreduce.Job: Running job ...

  5. spring-boot dubbo项目使用docker方式部署

    项目结构 本项目采用maven构建,有三个模块,分别是pms-interfaces, pms-services, pms-portal. 模块 描述 pms-interfaces 接口层,只能存放实体 ...

  6. spring boot项目配置RestTemplate超时时长

    配置类: @Configuration public class FeignConfiguration { @Bean(name="remoteRestTemplate") pub ...

  7. 20155308『网络对抗技术』Exp5 MSF基础应用

    20155308『网络对抗技术』Exp5 MSF基础应用 一.原理与实践说明 实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实 ...

  8. 20155318 《网络攻防》Exp6 信息搜集与漏洞扫描

    20155318 <网络攻防>Exp6 信息搜集与漏洞扫描 基础问题 哪些组织负责DNS,IP的管理. 互联网名称与数字地址分配机构,ICANN机构.其下有三个支持机构,其中地址支持组织( ...

  9. Spring @Value注入值失败,错误信息提示:Could not resolve placeholder

    问题根源: @Value("${wx.app.config.appid}") public Object appid; 异常信息: Caused by: java.lang.Ill ...

  10. ActiveMQ 的安装与使用(springboot版本)

    一.安装 上官网下载tar包 http://activemq.apache.org/ tar -zxvf 后进入bin/linux-86-64 ./activimq start 启动 二.使用 pom ...