*HDU2852 树状数组(求第K小的数)
KiKi's K-Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3864 Accepted Submission(s): 1715
the k-th number, we all should be very familiar with it. Of course,to
kiki it is also simple. Now Kiki meets a very similar problem, kiki
wants to design a container, the container is to support the three
operations.
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?
some groups of test data ,each test data the first number is an
integer m (1 <= m <100000), means that the number of operation to
do. The next m lines, each line will be an integer p at the beginning, p
which has three values:
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.
each deletion, if you want to delete the element which does not exist,
the output "No Elment!". For each query, output the suitable answers in
line .if the number does not exist, the output "Not Find!".
//求大于a的第k个数就是求第sum(a)+k小的数。二分求第k小数。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int A[];
int m;
int lowbit(int x)
{
return x&(-x);
}
void add(int id,int val)
{
while(id<=)
{
A[id]+=val;
id+=lowbit(id);
}
}
int sum(int id)
{
int s=;
while(id>)
{
s+=A[id];
id-=lowbit(id);
}
return s;
}
int main()
{
int a,b,p;
while(scanf("%d",&m)!=EOF)
{
memset(A,,sizeof(A));
for(int i=;i<m;i++)
{
scanf("%d",&p);
if(p==)
{
scanf("%d",&a);
add(a,);
}
else if(p==)
{
scanf("%d",&a);
if(sum(a)-sum(a-)==)
printf("No Elment!\n");
else add(a,-);
}
else if(p==)
{
scanf("%d%d",&a,&b);
int tem=sum(a);
if(sum()-tem<b)
{
printf("Not Find!\n");
continue;
}
tem+=b;
int lef=,rig=,mid;
while(lef+<rig) //二分结束条件!!!!!!!
{
mid=(lef+rig)>>;
int num=sum(mid);
if(num>=tem) rig=mid;
else lef=mid;
}
printf("%d\n",rig);
}
}
}
return ;
}
*HDU2852 树状数组(求第K小的数)的更多相关文章
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 树状数组求第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? ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- 树状数组求第K大(From CLJ)
; <<log2[n];p;p>>=) if(a[ret+p]<=kth) kth-=a[ret+=p]; return ret;
随机推荐
- 【强烈推荐】如何给TortoiseGit 配置密钥?
TortoiseGit 使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥.也就是说使用 ssh-keygen -C "username@email.com" - ...
- 【Alpha版本】冲刺-Day10
队伍:606notconnected 会议时间:11月18日 会议总结 张斯巍(433) 今天安排:完成剩余界面的设计,小修小补 完成度:100% 遇到的问题:无 感想:因为自己是负责界面美工部分的, ...
- css中为了清除浮动经常用到的after样式
.cf:after { display: block; visibility: hidden; width:; height:; line-height:; font-size:; clear: bo ...
- 听说你们要开始学C了
同学们好: 先自我介绍一下,我是你们的助教吴喆(厚颜无耻地要求大家叫我吉吉老师:-D),本科学的电子,所以陆陆续续接触了不少C/C++的工作,曾经被编码折磨得死去活来,如今却对其念念不舍,颇有点“斯德 ...
- 【ZJOI2008】 树的统计 count
Description 一 棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. ...
- effective OC2.0 52阅读笔记(七 系统框架)
47 熟悉系统框架 总结:将代码封装为动态库,并提供接口的头文件,就是框架.平时的三方应用都用静态库(因为iOS应用程序不允许在其中包含动态库),并不是真正的框架,然而也经常视为框架.例如:NSLin ...
- ASM:《X86汇编语言-从实模式到保护模式》第17章:保护模式下中断和异常的处理与抢占式多任务
★PART1:中断和异常概述 1. 中断(Interrupt) 中断包括硬件中断和软中断.硬件中断是由外围设备发出的中断信号引发的,以请求处理器提供服务.当I/O接口发出中断请求的时候,会被像8259 ...
- NPOI导出EXCEL 打印设置分页及打印标题
在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置 sheet1.FitToPage = false; 而 ...
- css中选择器的使用技巧
td:first-child{选择第一个} td:last-child{选择最后一个} td:nth-child(3){选择第3个} 一个简单的选择方法,很方便
- iOS开发UI篇—核心动画(转场动画和组动画)
转自:http://www.cnblogs.com/wendingding/p/3801454.html iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的 ...