POJ 2309:BST lowbit
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9140 | Accepted: 5580 |
Description
we can also find the maximum number by going down the right node. Now you are given some queries as "What are the minimum and maximum numbers in the subtree whose root node is X?" Please try to find answers for there queries.
Input
Output
Sample Input
2
8
10
Sample Output
1 15
9 11
发现每个点含有的节点数量就是做树状数组时lowbit()的值,lowbit()就是一个数x在二进制表示下最右边的那个1保留,其余值全部赋值为0的二进制数。
简单表示就是x&(x^(x-1))=x&(-x)。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std; const int N = 32005; int c[N],a[N],n; int lowbit(int x)
{
return x & (-x);
} int main()
{
int i,query;
long long ask;
scanf("%d",&query); for(i=1;i<=query;i++)
{
cin>>ask;
cout<<ask-(lowbit(ask)-1)<<" "<<ask+(lowbit(ask)-1)<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2309:BST lowbit的更多相关文章
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 2309 BST(树状数组Lowbit)
题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...
- poj 2309 BST 使用树阵lowbit
假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...
- POJ 2309 BST
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8565 Accepted: 5202 Description C ...
- POJ 2309 BST(二叉搜索树)
思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...
- POJ 2309 BST(二叉搜索树)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8657 Accepted: 5277 Description C ...
- 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)
题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值. 首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数.最 ...
- BST POJ - 2309 思维题
Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...
随机推荐
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- HackerOne去年发放超过8200万美元的赏金,联邦政府参与度大幅上涨
2019年,由黑客驱动的漏洞赏金平台HackerOne支付的漏洞奖金几乎是前几年总和的两倍,达到8200万美元. HackerOne平台在2019年也将注册黑客数量翻了一番,超过了60万,同时全年收到 ...
- PHP PDO_MYSQL 链式操作 非链式操作类
<?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +-------------------------------------- ...
- redhat 7.6 用户和组
用户 useradd user1 //新建用户user1 userdel user1 //删除用户user1 userdel -r user1 //删除用户并且删除h ...
- PaperReading20200223
CanChen ggchen@mail.ustc.edu.cn AdaBatch Motivation: Current stochastic gradient descend methods u ...
- IAR STM32F10x_StdPeriph_Driver 3.4转3.6.1库
1.Fatal Error[Pe1696]: cannot open source file core_cmInstr.h STM32F10x_StdPeriph_Driver 3.4库移植换成3.6 ...
- 解决maven项目创建过慢的问题,一直resolving dependencies...
##方法一: 1.在创建项目时设置其Properties (我大多在创建项目时 就解决这个问题) 创建项目的时候,在Properties属性面板中添加一项:archetypeCatalog = int ...
- 「NOIP2011」聪明的质监员
传送门 Luogu 解题思路 第一眼肯定是没什么思路的 dalao勿喷,但我们仔细看一看式子就会发现 \(Y\) 是随着 \(W\) 的变大而变小的. 所以 \(Y\) 随 \(W\) 的变化是单调的 ...
- PHP+swoole实现聊天群发功能
本篇文章主要介绍PHP+swoole实现聊天群发功能,感兴趣的朋友参考下,希望对大家有所帮助. php代码: $serv = new swoole_websocket_server("127 ...
- Vue中 onmouseenter,onmouseleave,onmouseover,onmouseout的区别
今天在学Vue视频的时候,提到了这四个触发事件,我就想做下笔记: 1.onmouseenter和onmouseleave是一组:当鼠标进入指定区域的时候触发,但是不支持冒泡,进入或者离开子组件都不触发 ...