hdu2852 KiKi's K-Number
Push: Push a given element e to container
Pop: Pop element of a given e from container
Query: Given two elements a and k, query the kth larger number which greater than a in container;
Although Kiki is very intelligent, she can not think of how to do it, can you help her to solve this problem?
If p is 0, then there will be an integer e (0 <e <100000), means press element e into Container.
If p is 1, then there will be an integer e (0 <e <100000), indicated that delete the element e from the container
If p is 2, then there will be two integers a and k (0 <a <100000, 0 <k <10000),means the inquiries, the element is greater than a, and the k-th larger number.
0 5
1 2
0 6
2 3 2
2 8 1
7
0 2
0 2
0 4
2 1 1
2 1 2
2 1 3
2 1 4
6
Not Find!
2
2
4
Not Find!
这题可以用树状数组做,查找用到了二分,二分用在树状数组上感觉如虎添翼(笑)。这题查找的是比i这个数大d的位置是什么,所以只要求出getsum(i)+d这个位置所对应的数是什么就行了。二分的时候要特殊判断一下。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100505
int b[maxn],num[maxn];
int lowbit(int x){
return x&(-x);
}
void update(int pos,int num)
{
while(pos<=maxn){
b[pos]+=num;pos+=lowbit(pos);
}
}
int getsum(int pos)
{
int num=0;
while(pos>0){
num+=b[pos];pos-=lowbit(pos);
}
return num;
}
int find(int l,int r,int x)
{
int mid;
while(l<=r){
mid=(l+r)/2;
if(getsum(mid)>=x){
if(num[mid]==0){
r=mid-1;continue;
}
if(getsum(mid)-num[mid]<x){
return mid;
}
else r=mid-1;
}
else l=mid+1;
}
}
int main()
{
int m,i,j,d,c,e;
while(scanf("%d",&m)!=EOF)
{
for(i=1;i<=maxn;i++){
b[i]=0;
num[i]=0;
}
for(i=1;i<=m;i++){
scanf("%d",&c);
if(c==0){
scanf("%d",&d);
num[d]++;
update(d,1);
}
else if(c==1){
scanf("%d",&d);
if(num[d]==0){
printf("No Elment!\n");continue;
}
num[d]--;
update(d,-1);
}
else if(c==2){
scanf("%d%d",&d,&e);
if(getsum(maxn)-getsum(d)<e){
printf("Not Find!\n");continue;
}
j=find(1,maxn,getsum(d)+e);
printf("%d\n",j);
}
}
}
return 0;
}
hdu2852 KiKi's K-Number的更多相关文章
- 权值树状数组 HDU-2852 KiKi's K-Number
引入 权值树状数组就是数组下标是数值的数组,数组存储下标对应的值有几个数 题目 HDU-2852 KiKi's K-Number 题意 几种操作,p=0代表push:将数值为a的数压入盒子 p=1代表 ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- hdu2852 KiKi's K-Number
Problem Description For the k-th number, we all should be very familiar with it. Of course,to kiki i ...
- 2019牛客网暑假多校训练第四场 K —number
链接:https://ac.nowcoder.com/acm/contest/884/K来源:牛客网 题目描述 300iq loves numbers who are multiple of 300. ...
- hdu-2852 KiKi's K-Number---二分+树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意: 题意: 给出三种操作, 0 在容器中插入一个数. 1 在容器中删 ...
- hdu2852 KiKi's K-Number
题意:给定三个操作添加删除查询大于a的的第k大值----树状数组的逆向操作 给定a利用BIT查询有多少值比a小,这样比a大的k大值就应该有k+sum(a)个小于他的值 因此可以二分枚举k大值看看是不是 ...
- 2019牛客暑期多校训练营(第四场)K.number
>传送门< 题意:给你一个字符串s,求出其中能整除300的子串个数(子串要求是连续的,允许前面有0) 思路: >动态规划 记f[i][j]为右端点满足mod 300 = j的子串个数 ...
- 2019牛客暑期多校训练营(第四场) - K - number - dp
https://ac.nowcoder.com/acm/contest/884/K 一开始整了好几个假算法,还好测了一下自己的样例过了. 考虑到300的倍数都是3的倍数+至少两个零(或者单独的0). ...
- 2019牛客多校第四场K number dp or 思维
number 题意 给一个数字串,问有几个子串是300的倍数 分析 dp写法:这题一看就很dp,直接一个状态dp[i][j]在第i位的时候膜300的余数是j左过去即可.这题比赛的时候样例老是少1,后面 ...
随机推荐
- Fail2ban工具使用
Fail2ban fail2ban扫描日志文件并且可以识别禁用某些多次尝试登录的IP,通过更新系统的防火墙规则来实现拒绝该IP连接,也可以配置禁用的时间.fail2ban提供了一些常用软件默认的日 ...
- 【栈和队列】2、栈的基本实现 - Java
简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 栈的实现 Stack<E> void push(E) E pop() E peek() int getSize() b ...
- 攻防世界 - Crypto(一)
base64: 根据题目base64可知编码方式,下载附件发现是一个txt文件,把内容用工具解码就彳亍了,即可得到flag, flag: cyberpeace{Welcome_to_new_World ...
- ctfhub技能树—sql注入—过滤空格
手注 查询数据库 -1/**/union/**/select/**/database(),2 查询表名 -1/**/union/**/select/**/group_concat(table_name ...
- [工作札记]03: 微软Winform窗体中ListView、DataGridView等控件的Bug,会导致程序编译失败,影响范围:到最新的.net4.7.2都有
工作中,我们发现了微软.net WinForm的一个Bug,会导致窗体设计器自动生成的代码失效,这个Bug从.net4.5到最新的.net4.7.2都存在,一直没有解决.最初是我在教学工作中发现的,后 ...
- 动态改变div标签中的内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- e.next = nil // avoid memory leaks e.prev = nil // avoid memory leaks
/Go/src/container/list/list.go:10
- Java 字符串简介
从概念上讲,Java 字符串就是 Unicode 字符序列.Java 没有内置的字符串类型,而是在标准 Java 类库中提供了一个预定义类,很自然地叫做 String.每个用双引号括起来的字符串都是 ...
- 扒一扒ELF文件
ELF文件(Executable Linkable Format)是一种文件存储格式.Linux下的目标文件和可执行文件都按照该格式进行存储,有必要做个总结. 目录 1. 链接举例 2. ELF文件类 ...
- Windows VS Code 配置 Java 开发环境
Windows VS Code 配置 C/C++ 开发环境 准备 Windows [这个相信大家都有 笑: )] VS Code JDK 建议 JDK8以上(不包含JDK8,关于 Windows环境下 ...