KiKi's K-Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2393    Accepted Submission(s): 1101

Problem Description
For 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?

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

 
Output
For 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!".
 
Sample Input
5 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
 
Sample Output
No Elment! 6 Not Find! 2 2 4 Not Find!
 
Source
 
这道题是一题,题意是要找a的第k大的数,由于想到树状数组,也有这么一种解法,所以就拿来练练手,首先采用传统的树状数组上升模式,进行
累加和,不过这里的和表示的是比a比大的个数,所以在我们查询的时候只要将要查找的的数sum(val)-sum(a)==k来进行二分,当然由于数据是离散的,而我们这里有无法进行离散化处理,因此我们只需要加入一个辅助数组lab[],进行判断就可以了.....这道题,第一次遇到可算是想了一段时间,毕竟是个菜鸟....做出来还是很幸福的说....
代码如下:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 100000
#define lowbit(x) ((x)&(-x))
int aa[maxn+];
int lab[maxn+];
void ope(int x,int val)
{
while(x<=maxn)
{
aa[x]+=val;
x+=lowbit(x);
}
}
int cont(int x)
{
int ans=;
while(x>)
{
ans+=aa[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int n,p,a,k;
int left,right,mid,res;
bool tag=true;
// freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
while(scanf("%d",&n)!=EOF)
{
memset(aa,,sizeof(aa));
memset(lab,,sizeof(lab));
while(n--)
{
scanf("%d",&p);
if(p==)
{
scanf("%d%d",&a,&k);
left=a, right=maxn;
tag=true;
while(left<=right)
{
mid=left+(right-left)/;
res=cont(mid)-cont(a);
if(res<k)
left=mid+;
else if(res-lab[mid]>=k)
right=mid-;
else
if(res>=k&&res-lab[mid]<k)
{
printf("%d\n",mid);
tag=false;
break;
}
}
if(tag)
printf("Not Find!\n"); }
else
{
scanf("%d",&a);
if(p==)
{
if(lab[a]==)
printf("No Elment!\n");
else
{
lab[a]--;
ope(a,-); //1代表insert
}
}
else
{
lab[a]++;
ope(a,); //0代表elete
}
}
}
}
return ;
}

并给出一些数据来帮助验证吧....


 

HDUOJ-----2852 KiKi's K-Number(树状数组+二分)的更多相关文章

  1. HDU 2852 KiKi's K-Number【 树状数组 二分 】

    题意:给出m个操作,0:是增加一个数,add(x,1)1:是删除一个指定的数,这个是看sum(x) - sum(x-1)是否为0,为0的话则不存在,不为0的话,则add(x,-1)2:是查询比x大的数 ...

  2. HDU 2852 KiKi's K-Number(离线+树状数组)

    题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...

  3. HDU 2852 KiKi's K-Number(树状数组+二分搜索)

    题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...

  4. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  5. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  6. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  7. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  8. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  9. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

随机推荐

  1. c++模板类成员的声明和定义

    c++模板类成员的声明和定义应该都放在*.h中,有普通类不一样. 如果定义放在*.cpp中,最终链接时,会报方法undefined错误. 参考:http://users.cis.fiu.edu/~we ...

  2. Cocos2dx 小技巧(十四)ScrollView实现缩放效果

    这阶段心绪比較乱,所以这篇开头就不扯淡了.(谁说大姨夫来了我跟谁急!~~)说到大姨夫我突然想到英雄联盟有个美女讲解叫伊芙蕾亚,她的堂弟ID居然叫:姨夫累呀,好笑吧(呵呵,有点冷~~额,我都说不扯淡了) ...

  3. 使用HTML5开发离线应用 - cache manifest

    HTML5 是目前正在讨论的新一代 HTML 标准,它代表了现在 Web 领域的最新发展方向.在 HTML5 标准中,加入了新的多样的内容描述标签,直接支持表单验证.视频音频标签.网页元素的拖拽.离线 ...

  4. Linux中Shell的执行流程

    Shell执行流程 1.Printthe info of reminding 打印提示信息 2.Waitinguser for input(wait) 等待用户输入 3.Acceptthe comma ...

  5. [leetcode]Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...

  6. VMware与Cisco DRAC中的virtual disk的对应关系

    笔者面临的问题如下: 笔者有一台Cisco C240的服务器, 其中有十块容量一样大的SAS的local disk, 一块SSD. 其中的两块SAS盘组成了一个RAID 1的virtual drive ...

  7. jQuery调用WCF需要注意的一些问题

    昨天,博客园首页增加了Digg功能.在该功能中我们开始尝试使用jQuery直接调用WCF.之前我们采用的方案是jQuery调用Web Service,然后WebService再调用服务层.这样调用主要 ...

  8. 五个瓶颈影响你的Asp.Net程序(网站)性能

    在今天的手机设备世界里,生活的节奏继续加快,因此访问你的网站的用户的耐心也在渐渐失去.同时,我提供了非常多的特性,为了防止你的网站变得过时或者廉价,你必须跟上竞争对手.你想赢得访问者的喝彩,但访问者没 ...

  9. 使用SqlServer中的float类型时发现的问题

    在做项目中,使用了float类型来定义一些列,如:Price,但是发现了很多问题1.当值的位数大于6位是float型再转varchar型的时候会变为科学技术法显示    此时只好将float型转换成n ...

  10. C++设计模式实现--模板(Template)模式

    一. 问题 在面向对象系统的分析与设计过程中常常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,可是逻辑(算法)的框架(或通用的应用算法)是同样的.Template提 ...