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. 对只转发结果集的无效操作:last

    调用时候发生这样一个错误: SQLException: 对只转发结果集的无效操作:last 原因是按照缺省方式打开的ResultSet不支持结果集cursor的回滚 如果想要完成上述操作,要在生成St ...

  2. openssh-server

    安装 apt-get install openssh-server 配置 sudo gedit /etc/ssh/sshd_config PermitRootLogin without-passwor ...

  3. linux 复制目录结构,但不复制文件

    find src -type d | sed 's/src/mkdir -p dst/'|sh

  4. 第二十一章 springboot + 定时任务

    1.application.properties #cron job.everysecond.cron=0/1 * * * * * job.everytensecond.cron=0/10 * * * ...

  5. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  6. vue-router 懒加载优化

    一.路由懒加载 1.先安装 babel 动态引入插件 npm install --save-dev babel-plugin-syntax-dynamic-import 2.修改router/inde ...

  7. 基于Packet Tracer 组建智能公司局域网

    背景及要求                                                                                               ...

  8. UVA 11573 - Ocean Currents(BFS+优先队列)

    UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面.数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 ...

  9. 向第一个 p 元素添加一个类

    This is a heading This is a paragraph. This is another paragraph. 向第一个 p 元素添加一个类 111 <html> &l ...

  10. [Algorithm] Radix Sort Algorithm

    For example we have the array like this: [, , , , , ] First step is using Counting sort for last dig ...