题意

给定一棵$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 思维+二分的更多相关文章

  1. 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 ...

  2. codeforces D. Mahmoud and Ehab and the binary string(二分)

    题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...

  3. Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)

    <题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的   Hamming ...

  4. Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)

    题目链接 \(Description\) 有一个长为\(n\)的二进制串,保证\(01\)都存在.你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串 ...

  5. 2019牛客多校第三场B Crazy Binary String 思维

    Crazy Binary String 思维 题意 给出01串,给出定义:一个串里面0和1的个数相同,求 满足定义的最长子序列和子串 分析 子序列好求,就是0 1个数,字串需要思考一下.实在没有思路可 ...

  6. codeforces 894B - Ralph And His Magic Field - [数学题]

    题目链接:https://cn.vjudge.net/problem/CodeForces-894B Ralph has a magic field which is divided into n × ...

  7. Codeforces 894B - Ralph And His Magic Field

    894B - Ralph And His Magic Field 思路: 当k为1时,如果n和m奇偶性不同,那么没有答案. 可以证明,在其他情况下有答案,且答案为2^(n-1)*(m-1),因为前n- ...

  8. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

  9. 【codeforces 792D】Paths in a Complete Binary Tree

    [题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...

随机推荐

  1. django 通过orm操作数据库

    Django Model 每一个Django Model都继承自django.db.models.Model 在Model当中每一个属性attribute都代表一个database field 通过D ...

  2. 关于\r和\n的区别

    回车和换行来源 在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两 ...

  3. sublime txet 3 python 开发环境安装配置

    下载python 下载地址:https://www.python.org/downloads/windows/ 下载sublime text 3 下载地址:https://www.sublimetex ...

  4. Bytes to be written to the stream exceed the Content-Length bytes size specified 解决方法

    context.Response.ContentType = encode;                using (StreamWriter writer = new StreamWriter( ...

  5. LeetCode:逆波兰表达式求值【150】

    LeetCode:逆波兰表达式求值[150] 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除 ...

  6. Node单线程与异步编程的初步理解

    最近学习了一些关于node的单线程与异步的知识,想拿过来和大家分享下: var async = require('async') //并行无关联,等待事件为最长时间请求过程.如以下两个任务执行时间 c ...

  7. 【HTML5校企公益课】第三天

        1.上午2D.旋转变色的... 基本思路就是先写静态画面然后添加动画. <!--告诉浏览器该文件为网页格式--> <html> <!--网页的头部标签--> ...

  8. CSS3自定义发光radiobox单选框

    在线演示 本地下载

  9. FAQ | 是什么导致MySQL数据库服务器磁盘I/O高(本文章来自知数堂)

    FAQ | 是什么导致MySQL数据库服务器磁盘I/O高 2016-12-26 叶金荣 老叶茶馆 0.导读 有个MySQL服务器的磁盘I/O总有过高报警,怎么回事? 本文约1500字,阅读时间约10分 ...

  10. 20145217《网络对抗》 MAL_简单后门学习总结

    20145217<网络对抗> MAL_简单后门学习总结 实践内容: 1.netcat的应用 2.socat的应用 3.meterpreter的应用 知识点学习总结 后门程序一般是指那些绕过 ...