Codeforces Round447 D树上前缀和
已知完全二叉树和每条边的权值,q次询问,每次给出sta起点和H。
w=(H-点到sta的权值),求w>0的所有w的加和。
这题用树上前缀和来写,e[i]记录子树上的点到点i的距离,sum[i][j]为e[i]的前缀和
这样每次找到满足大于h-len[i]的长度就行(二分查找)
void init(){
for(ll x=n;x>=;x--){
e[x].push_back();
ll lc=x<<;ll rc=x<<|;
if(lc<=n){
for(int i=;i<e[lc].size();i++){
e[x].push_back(e[lc][i]+len[lc-]);
}
}
if(rc<=n){
for(int i=;i<e[rc].size();i++){
e[x].push_back(e[rc][i]+len[rc-]);
}
}
sort(e[x].begin(),e[x].end());
sum[x].resize(e[x].size());
for(int i=;i<e[x].size();i++){
sum[x][i]=sum[x][i-]+e[x][i];
}
}
}
ll query(int x,ll h){
if(h<=)return ;
int index=upper_bound(e[x].begin(),e[x].end(),h)-e[x].begin();
return index*h-sum[x][index-];
}
int main()
{
// freopen("in.txt","r",stdin);
int m;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%lld",&len[i]);
}
init();
while(m--){
ll a;
ll h;
ll pre=;
scanf("%lld%lld",&a,&h);
ll ans=;
while(a&&h>){
ans+=h;
ll lc=a<<;
ll rc=a<<|;
if(lc!=pre&&lc<=n){
ans+=query(lc, h-len[lc-]);
}
// cout<<ans<<"\n";
if(rc!=pre&&rc<=n){
ans+=query(rc, h-len[rc-]);
}
//cout<<ans<<"\n";
h-=len[a-];
pre=a;
a/=;
}
printf("%lld\n",ans);
}
}
Codeforces Round447 D树上前缀和的更多相关文章
- Codeforces 986E - Prince's Problem(树上前缀和)
题面传送门 题意: 有一棵 \(n\) 个节点的树,点上有点权 \(a_i\),\(q\) 组询问,每次询问给出 \(u,v,w\),要求: \(\prod\limits_{x\in P(u,v)}\ ...
- BZOJ2783: [JLOI2012]树(树上前缀和+set)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1215 Solved: 768[Submit][Status][Discuss] Descriptio ...
- 「LuoguP2420」 让我们异或吧(树上前缀和
P2420 让我们异或吧-洛谷 题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor ...
- [Bzoj3631][JLOI2014]松鼠的新家 (树上前缀和)
3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2350 Solved: 1212[Submit][Sta ...
- Codeforces 919 行+列前缀和 树上记忆化搜索(树形DP)
A B C #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) ...
- Codeforces 1082C Multi-Subject Competition 前缀和 A
Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...
- Codeforces 578B "Or" Game (前缀和 + 贪心)
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) ...
- NOIP2015运输计划(树上前缀和+LCA+二分)
Description 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球. 小 P 掌管 ...
- Kuro and Walking Route CodeForces - 979C (树上DFS)
Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...
随机推荐
- 创建dynamics CRM client-side (八) - 获取attribute的值 和 设置disable
大家可以用下面的方式来获取attribute的值 formContext.getAttribute("address1_shippingmethodcode").getText() ...
- Springboot | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin
案例 今天搭建spring boot 环境时,使用mvn install ,出现Failed to execute goal org.springframework.boot:spring-boot- ...
- win10下安装Anaconda3
1.官方下载:https://www.anaconda.com/distribution/#download-section (最新版直接下即可) 或者国内镜像下载:https://mirrors. ...
- 解决docker容器无网络、无法连接互联网----长期更新
众所周知,docker有三种默认的网络模式,分别是: bridge host none 然后需要注意的就是内核转发这个,一定不要忘了!配置方法如下 echo -e "net.ipv4.ip_ ...
- 【WPF学习】第三十六章 样式基础
前面三章介绍了WPF资源系统,使用资源可在一个地方定义对象而在整个标记中重用他们.尽管可使用资源存储各种对象,但使用资源最常见的原因之一是通过他们的保存样式. 样式是可应用于元素的属性值集合.WPF样 ...
- iNeuOS工业互联平台,iNeuKernel(物联网核心组件)远程控制标准化设计与实现。发布v2.3版本。
目 录 1. 概述... 2 2. 平台演示... 2 3. 控制端与iNeuKernel的交互协议... 3 4. 设备驱动实现控制业务... 4 ...
- 带大家用40行python代码实现一个疫情地图
最近两个月,因为新冠病毒无情的肆虐,相信会给每个中国人的记忆中画上重重的一笔.到今天为止,疫情形势依然十分严峻,虽然除湖北外的其他省份已经连续十一天确诊人数下降,但是接下来还有将近至少1.6亿的人口迁 ...
- 形象解释各种卷积算法(Convolution animations)
No padding, no strides Arbitrary padding, no strides Half padding, no strides Full padding, no strid ...
- HDU_3853_概率dp
http://acm.hdu.edu.cn/showproblem.php?pid=3853 又因为总期望为子期望的加权和,加权因子为子期望的转移概率,所以得到:dp[ i ][ j ]= p1 * ...
- HDU_1175_dfs
http://acm.hdu.edu.cn/showproblem.php?pid=1175 dfs(x,y,i,num),xy表示位置,i表示方向,num表示转向次数,num=2时候的剪枝很重要. ...