【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 [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...
随机推荐
- 0502-Hystrix保护应用-简介,使用,健康指标等
一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_circuit_ ...
- 关于 tf.nn.softmax_cross_entropy_with_logits 及 tf.clip_by_value
In order to train our model, we need to define what it means for the model to be good. Well, actuall ...
- 记一次centos7挂在nas盘的踩坑经过
p:first-child, #write > ul:first-child, #write > ol:first-child, #write > pre:first-child, ...
- mysql中解决主键自增长断号问题
情况一:如果表中本来已经存在数据,并且有断号的现象.那先得删除主键再添加,重新设置自增长. 1.ALTER TABLE student DROP id; 2.ALTER TABLE student A ...
- Hbase 学习笔记5----hbase region, store, storefile和列簇的关系
The HRegionServer opens the region and creates a corresponding HRegion object. When the HRegion is o ...
- Matplot相关(二)——统计图
Matplotlib:其能够支持所有的2D作图和部分3D作图.能通过交互环境做出印刷质量的图像. ————————缩写定义———————— import matplot.pyplot as plt — ...
- Canvas:橡皮筋线条绘制
Canvas:橡皮筋线条绘制 效果演示 实现要点 事件监听 [说明]: 在Canvas中检测鼠标事件是非常简单的,可以在canvas中添加一个事件监听器,当事件发生时,浏览器就会调用这个监听器. 我们 ...
- 多线程 wait和sleep区别
wait和sleep区别共同点: 1. 他们都是在多线程的环境下,都可以在程序的调用处阻塞指定的毫秒数,并返回. 2. wait()和sleep()都可以通过interrupt()方法 打断线程的暂停 ...
- 113. Path Sum II(求等于某个数的所有路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 【Flask】Flask常用信号
使用信号分为3步,第一是定义一个信号,第二是监听一个信号,第三是发送一个信号. 1. 定义信号:定义信号需要使用到blinker这个包的Namespace类来创建一个命名空间.比如定义一个在访问了某个 ...