【Codeforces】894D. Ralph And His Tour in Binary Country 思维+二分
题意
给定一棵$n$个节点完全二叉树,$m$次询问,每次询问从$a$节点到其它所有节点(包括自身)的距离$L$与给定$H_a$之差$H_a-L$大于$0$的值之和
对整棵树从叶子节点到父节点从上往下预处理出每个节点到它的子节点的距离$d$并排序,因为每个节点最多有$\log n$个父亲,所以预处理时间复杂度为$O(n\log n)$
通过二分查找可以得到任意节点到子节点的满足条件的距离,对于每个询问,查询当前节点的子树权值和,并不断向树根转移,每次计算兄弟节点对应的权值和(相当于看作以当前节点为根重构树),直到转移到树根
时间复杂度$O((n+m)\log^2n)$
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m, L[1000005], a, h, tmp, lch, rch, last;
long long ans = 0;
vector<int> d[1000005];
vector<LL> pre[1000005];
LL query(int x, int h) {
if(h <= 0) return 0;
int p = upper_bound(d[x].begin(), d[x].end(), h) - d[x].begin();
return 1LL * p * h - 1LL * pre[x][p - 1];
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 2; i <= n; ++i) scanf("%d", &L[i]);
for(int i = n; i >= 1; --i) {
d[i].push_back(0);
lch = i << 1; rch = i << 1 | 1;
if(lch <= n) {
for(int j = 0; j < d[lch].size(); ++j) d[i].push_back(d[lch][j] + L[lch]);
}
if(rch <= n) {
for(int j = 0; j < d[rch].size(); ++j) d[i].push_back(d[rch][j] + L[rch]);
}
sort(d[i].begin(), d[i].end());
pre[i].resize(d[i].size());
for(int j = 1; j < pre[i].size(); ++j) {
pre[i][j] = pre[i][j - 1] + d[i][j];
}
}
for(; m; --m) {
scanf("%d%d", &a, &h); ans = 0;
tmp = a; last = 0;
while(tmp) {
if(h < 0) break; ans += h;
lch = tmp << 1; rch = tmp << 1 | 1;
if(lch <= n && lch != last) {
ans += query(lch, h - L[lch]);
}
if(rch <= n && rch != last) {
ans += query(rch, h - L[rch]);
}
h -= L[tmp]; last = tmp; tmp >>= 1;
}
printf("%I64d\n", ans);
}
return 0;
}
【Codeforces】894D. Ralph And His Tour in Binary Country 思维+二分的更多相关文章
- Codeforces 894.D Ralph And His Tour in Binary Country
D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 me ...
- codeforces D. Mahmoud and Ehab and the binary string(二分)
题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)
<题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的 Hamming ...
- Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)
题目链接 \(Description\) 有一个长为\(n\)的二进制串,保证\(01\)都存在.你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串 ...
- 2019牛客多校第三场B Crazy Binary String 思维
Crazy Binary String 思维 题意 给出01串,给出定义:一个串里面0和1的个数相同,求 满足定义的最长子序列和子串 分析 子序列好求,就是0 1个数,字串需要思考一下.实在没有思路可 ...
- codeforces 894B - Ralph And His Magic Field - [数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894B Ralph has a magic field which is divided into n × ...
- Codeforces 894B - Ralph And His Magic Field
894B - Ralph And His Magic Field 思路: 当k为1时,如果n和m奇偶性不同,那么没有答案. 可以证明,在其他情况下有答案,且答案为2^(n-1)*(m-1),因为前n- ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- 【codeforces 792D】Paths in a Complete Binary Tree
[题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...
随机推荐
- Python3 标准库
Python3标准库 更详尽:http://blog.csdn.net/jurbo/article/details/52334345 文本 string:通用字符串操作 re:正则表达式操作 diff ...
- boost智能指针总结
智能指针是一种具备指针类似行为的对象,当不在需要它的时候自动删除其引用的c++对象.直接点说就是自动析构C++对象. boost提供了6种智能指针,如下所示: scoped_ptr <boost ...
- Python基础总结(字符串常用,数字类型转换,基本运算符与流程控制)
一.字符串常用操作 #Python strip() 方法用于移除字符串头尾指定的字符(默认为空格) name='*egon**' print(name.strip('*'))#移除 name 变量对应 ...
- django生成json
好方便啊……list什么的一下都变成json了呢! import json from django.core.serializers.json import DjangoJSONEncoder def ...
- CMSIS_OS中osMailPut 和 osMessagePut 的问题
1. 背景 为了屏蔽不同OS之间的差别,ARM公司开发了一套OS接口--CMSIS_OS. 在使用STM32 cube生成的free rtos工程中,遇到一些问题. 问题1:osMessageGet ...
- Spring 之定义切面尝试(基于注解)
[Spring 之定义切面尝试] 1.标记为深红色的依赖包是必须的 <dependency> <groupId>org.springframework</groupId& ...
- gh-ost测试
gh-ost测试 1.不支持没有主键或者唯一索引的表 2018-08-24 09:53:33 FATAL No PRIMARY nor UNIQUE key found in table! Baili ...
- 机器学习中的numpy库
日常学习中总是遇到数据需要处理等问题,这时候我们就可以借助numpy这个工具来做一些有意思的事. 1.生成随机数的几种方式 x=np.random.random(12) ###生成12 ...
- 如何评价一个pipeline的好坏
生物信息NGS相关软件众多. 常用的比对软件:bwa,bowtie: 去pcr重复的软件\:samtools,picard: calling variant:samtools/bcftools,gat ...
- iOS7中彻底隐藏status bar
用Xcode5开发新游戏,发现在iOS7中按照以前的方法隐藏status bar失效了. 想要彻底隐藏status bar,需要在info.plist中添加新行“View controller-bas ...