hdu2852 KiKi's K-Number
2024-09-30 22:06:38
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?
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.
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!这题能够用树状数组做,查找用到了二分,二分用在树状数组上感觉如虎添翼(笑)。 这题查找的是比i这个数大d的位置是什么,所以仅仅要求出getsum(i)+d这个位置所相应的数是什么即可了。二分的时候要特殊推断一下。
6
Not Find!
2
2
4
Not Find!#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;
}
最新文章
- setup notifier actions in aodh alarm
- Javascript 中的 in, hasOwnProperty, delete, for/in
- windows 7 安装 scrapy
- 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数
- Ubuntu修改屏幕默认亮度
- HTML5 Canvas 的事件处理---转
- Google谷歌搜索引擎登录网站 - Blog透视镜
- JavaScript检测原始值、引用值、属性
- webstorm与phpstorm主题配置
- react.js 从零开始(七)React (虚拟)DOM
- java调用dll-JNA
- 小偷网站工具--Teleport Ultra
- springJdbc like模糊查询,Spring namedParameterJdbcTemplate like查询
- linkin大话java
- Tutorial: Build a Spring WebMVC App with Primefaces
- mount: mounting proc on /proc failed: Device or resource busy
- Liferay7 BPM门户开发之16: Liferay中用户\站点\组织架构\角色\用户组以及关联关系
- Spring使用RMI进行远程方法调用
- Win7下VB6.0不能加载mscomctl.ocx的解决办法
- luoguP4643 阿狸和桃子的挑战 思维
热门文章
- switch-case用法
- 一天搞定jQuery(三)——使用jQuery完成复选框的全选和全不选
- Deepin系统关于每次启动终端都要输入source /etc/profile的问题
- ie6,ie7,ie8,FF 浏览器兼容问题
- WIndows 系统下的常用命令 和 检测方法
- springboot的jsp热部署
- MySQL Insert数据量过大导致报错 MySQL server has gone away
- CCF201512-2 消除类游戏 java(100分)
- Greek symbols --Latex
- sql语句的字段转成Date