HZAU 1203 One Stroke(倍增)
题目链接:http://acm.hzau.edu.cn/problem.php?id=1203
【题意】给你一颗完全二叉树每个节点都有一个权值,然后要你从上往下找一条链,值得链上权值的和<K,且节点数最大。
【分析】有两种做法:一种是在树上双指针,另一种是先求一下前缀和,当到i节点时前缀和<K,更新ans,当前缀和>K,倍增向上找
使得前缀和<K的节点,更新ans。
#include <bits/stdc++.h>
#define inf 100000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
const int N = 1e6+;
const int M = 4e5+;
int n,m,ans;
int a[N];
int sum[N],fa[N][],dep[N];
void dfs(int u,int f){
if(u>n)return;
sum[u]=sum[f]+a[u];
dep[u]=dep[f]+;
fa[u][]=f;
for(int i=;i<;i++)fa[u][i]=fa[fa[u][i-]][i-];
if(sum[u]<=m)ans=max(ans,dep[u]);
else {
int v=u;
for(int i=;i>=;i--){
if(sum[u]-sum[fa[v][i]]<=m){
ans=max(ans,dep[u]-dep[fa[v][i]]);
v=fa[v][i];
}
}
}
dfs(u*,u);
dfs(u*+,u);
}
int main() {
int T,op,x,y,v;
scanf("%d",&T);
for(int cas=;cas<=T;cas++){
met(fa,);
ans=;
met(sum,);
met(dep,);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
dfs(,);
printf("%d\n",ans==?-:ans);
}
return ;
}
HZAU 1203 One Stroke(倍增)的更多相关文章
- hzau 1203 One Stroke
1203: One Stroke Time Limit: 2 Sec Memory Limit: 1280 MBSubmit: 264 Solved: 56[Submit][Status][Web ...
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- [板子]倍增LCA
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm ...
- 背水一战 Windows 10 (13) - 绘图: Stroke, Brush
[源码下载] 背水一战 Windows 10 (13) - 绘图: Stroke, Brush 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Stroke - 笔划 Brush ...
- 在线倍增法求LCA专题
1.cojs 186. [USACO Oct08] 牧场旅行 ★★ 输入文件:pwalk.in 输出文件:pwalk.out 简单对比时间限制:1 s 内存限制:128 MB n个被自 ...
- LCA 倍增||树链剖分
方法1:倍增 1498ms #include <iostream> #include <cstdio> #include <algorithm> #include ...
- iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)
像往常一样 这个系列的博客是跟着大神的脚步来的.按照往例 在此贴出原博客的出处: http://blog.csdn.net/hello_hwc?viewmode=list我对大神的崇拜之情 如滔滔江水 ...
- Codevs 2370 小机房的树 LCA 树上倍增
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...
- Uva 11354 LCA 倍增祖先
题目链接:https://vjudge.net/contest/144221#problem/B 题意:找一条从 s 到 t 的路,使得瓶颈路最小. 点的数目是10^4,如果向之前的方案求 maxc ...
随机推荐
- 数据结构&图论:图
在这里对图的存储和遍历进行一个规范,为以后更复杂的数据结构学习打下基础 首先是邻接矩阵的形式,适合于存稠密图,如果是全连接图就再合适不过了 int a[maxn][maxn]; 一个二维数组就可以搞定 ...
- Spring Boot 启动报错:LoggingFailureAnalysisReporter
17:57:19: Executing task 'bootRun'... Parallel execution with configuration on demand is an incubati ...
- HDU 2084 数塔 (dp)
题目链接 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数 ...
- hasOwnProperty()方法与in操作符
1.hasOwnProperty() 该方法检测属性存在于实例,还是存在于原型,对于存在于实例中的属性则返回true 2.in 使用该操作符时只要通过对象能够访问到的属性都会返回true
- wce.exe getpass.exe 读取密码
http://www.ampliasecurity.com/research/wce_v1_4beta_x32.zip http://www.ampliasecurity.com/research/w ...
- document.onclick在ios上不触发的解决方法与touchstart点击穿透处理
document.onclick = function (e) { var e = e ? e : window.event; var tar = e.srcElement || e.target; ...
- Caffe 学习笔记1
Caffe 学习笔记1 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和 ...
- python实战===一键刷屏
#当按键q的时候,自动输入 “大家好!”并回车键发送!from pynput import keyboard from pynput.keyboard import Key, Controller k ...
- C基础 多用户分级日志库 sclog
引言 - sclog 总的设计思路 sclog在之前已经内置到simplec 简易c开发框架中一个日志库. 最近对其重新设计了一下. 减少了对外暴露的接口. 也是C开发中一个轮子. 比较简单, 非常适 ...
- Mysql安装发生「Access denied for user ‘root’@’localhost’ (using password: NO)」错误
参考:http://www.aipacommander.com/entry/2014/05/26/152247 mysql_secure_installation 依赖重置密码