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. Java IO 体系结构

    参考文章地址: http://blog.csdn.net/oracle_microsoft/article/details/2634231 Java IO体系结构看似庞大复杂,其实有规律可循,要弄清楚 ...

  2. 版本号控制-git(二)

    上次文章给大家介绍了Git的一些基本知识(http://www.cnblogs.com/jerehedu/p/4582398.html).并介绍了使用git init初始化化版本号库.使用git ad ...

  3. Visual GC(监控垃圾回收器)

    Java VisualVM默认没有安装Visual GC插件,需要手动安装,JDK的安装目录的bin目露下双击jvisualvm.exe,即可打开Java VisualVM,点击菜单栏 工具-> ...

  4. iOS:UIToolBar控件的使用

    UIToolBar控件:是经常使用的一个工具条控件,虽然在上面可以添加子控件,但是toolbar中只能添加UIBarButtonItem类型的子控件,其他子控件会被包装成这种类型的,例如UIButto ...

  5. 我所遭遇过的中间件--VTK

    我所遭遇过的中间件--VTK Vtk是我接触的第一款软件开发包,它引导我对图形学的入门.我是先学的VTK,后学的OpenGL和D3D.VTK是专为图形学开发,特点是接口清晰,好上手,又含有大量的图像处 ...

  6. DB-library 常用函数

    以下转自:http://blog.csdn.net/lwbeyond/article/details/5620801 1. Dbcmd和dbfcmd 函数原形: Dbcmd(DBPROCESS *pr ...

  7. 附3 springboot源码解析 - 构建SpringApplication

    package com.microservice.framework; import org.springframework.boot.SpringApplication; import org.sp ...

  8. 浅析Linux线程调度

    在Linux中,线程是由进程来实现,线程就是轻量级进程( lightweight process ),因此在Linux中,线程的调度是按照进程的调度方式来进行调度的,也就是说线程是调度单元.Linux ...

  9. 同时启动多个Tomcat服务器

    以下步骤能够同时启动两个tomcat:1.特别要注意:不要设置CATALINA_HOME 2.分别修改安装目录下的conf子目录中的server.xml文件: a.修改http访问端口(默认为8080 ...

  10. 国内各视频网站android pad客户端支持分辨率情况初步统计

    视频网站名称 800*600 1024*600 1280*800 其他 国际化   备注 优酷 支持 支持 支持 支持 不支持     土豆网 没有pad版的 没有pad版的 没有pad版的 支持 不 ...