KiKi's K-Number

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

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
题意:

一个空的容器,进行三种操作:
0 a : 把a加进容器里;
1 a : 把a从容器中删除,如果没有a则输出No Elment!,如果有多个a则只删除一个;
2 a k : 求整个容器中所有比a大的数中的第k大的数,并输出,如果没有则输出NO Finds!。
题解:因为数的范围比较小 所以不需要离散化。主席树记录线段树的历史版本。查找容器中比a大的第k大的数字可以 先查询范围在[1,a]的数字数量q
求第q+k大即可。
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
const int N=;
using namespace std;
struct chairmantree
{
int rt[*N],ls[*N],rs[*N],sum[*N];
int tot;
void init()
{
tot=;
}
void build(int l,int r,int &pos)
{
pos=++tot;
sum[pos]=;
if(l==r) return ;
int mid=(l+r)>>;
build(l,mid,ls[pos]);
build(mid+,r,rs[pos]);
}
void update(int p,int c,int l,int r,int pre,int &pos)
{
pos=++tot;
ls[pos]=ls[pre];
rs[pos]=rs[pre];
sum[pos]=sum[pre]+c;
if(l==r) return ;
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,ls[pre],ls[pos]);
else
update(p,c,mid+,r,rs[pre],rs[pos]);
}
int rank(int s,int e,int l,int r,int L,int R)
{
if(s<=l&&e>=r) return sum[R]-sum[L];
int ans=;
int mid=(l+r)>>;
if(s<=mid)
ans+=rank(s,e,l,mid,ls[L],ls[R]);
if(e>mid)
ans+=rank(s,e,mid+,r,rs[L],rs[R]);
return ans;
}
int query(int L,int R,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)>>;
int x=sum[ls[R]]-sum[ls[L]];
if(k<=x)
query(ls[L],ls[R],l,mid,k);
else
query(rs[L],rs[R],mid+,r,k-x);
}
} tree;
int m;
int exm;
int e;
int main()
{
int rr=;
while(scanf("%d",&m)!=EOF)
{
tree.init();
tree.build(,rr,tree.rt[]);
for(int i=; i<=m; i++)
{
scanf("%d",&exm);
if(exm==)
{
scanf("%d",&e);
tree.update(e,,,rr,tree.rt[i-],tree.rt[i]);
}
if(exm==)
{
scanf("%d",&e);
if(tree.rank(e,e,,rr,tree.rs[],tree.rt[i-]))
{
tree.update(e,-,,rr,tree.rt[i-],tree.rt[i]);
}
else
{
tree.update(e,,,rr,tree.rt[i-],tree.rt[i]);
printf("No Elment!\n");
}
}
if(exm==)
{
int a,k;
scanf("%d %d",&a,&k);
tree.update(,,,rr,tree.rt[i-],tree.rt[i]);
int q=tree.rank(,a,,rr,tree.rt[],tree.rt[i]);
int xx=tree.rank(,rr,,rr,tree.rt[],tree.rt[i]);
k+=q;
if(k>xx)
printf("Not Find!\n");
else
printf("%d\n",tree.query(tree.rt[],tree.rt[i],,rr,k));
}
}
}
return ;
}

HDU 2852 主席树的更多相关文章

  1. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  2. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  3. HDU 2655 主席树

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  5. HDU 2665(主席树,无修改第k小)

    Kth number                                                 Time Limit: 15000/5000 MS (Java/Others)   ...

  6. HDU 3333 & 主席树

    题意: balabala SOL: 这题用主席树怎么做呢...貌似一模一样...一个一个建n棵的线段树.先把上一棵树复制下来,当a[i]出现过,就把这棵树里的那个位置去掉------一模一样的思维.. ...

  7. HDU 4251 --- 主席树(划分树是正解)

    题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...

  8. hdu 5140 主席树

    这题说的是每个员工有工资 水平 在公司待的年限这几个属性,有大量的查询 查的是在一定的水平和工作年限的工人总工资是多少 这个思路是比较简单的我们按照他们的水平排序,排完后,使用主席树不断地往里面插,然 ...

  9. HDU - 4866 主席树 二分

    题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...

随机推荐

  1. Java异常层次结构

    1. 如果是不可查异常(unchecked exception),即Error.RuntimeException或它们的子类,那么可以不使用throws关键字来声明要抛出的异常,编译仍能顺利通过,但在 ...

  2. Logistic回归和SVM的异同

    这个问题在最近面试的时候被问了几次,让谈一下Logistic回归(以下简称LR)和SVM的异同.由于之前没有对比分析过,而且不知道从哪个角度去分析,一时语塞,只能不知为不知. 现在对这二者做一个对比分 ...

  3. 2.安装hdfs yarn

    下载hadoop压缩包设置hadoop环境变量设置hdfs环境变量设置yarn环境变量设置mapreduce环境变量修改hadoop配置设置core-site.xml设置hdfs-site.xml设置 ...

  4. C Program进阶-数组

    (一)数组的内存布局 对于语句int a[5]; 我们明白这里定义了一个数组,数组里有5个元素,每一个元素都是int类型,我们可以用a[0],a[1]等访问数组里的元素,但是这些元素的名字就是a[0] ...

  5. Cortex-M3(NXP LPC 1788) 启动代码

    startup_LPC177x_8x.s启动代码分析. 参考资料: Cortex-M3 (NXP LPC1788)之启动代码分析 ARM启动过程(Cortex-M3 NXP LPC1768为例) ;/ ...

  6. 【转】jQuery的deferred对象详解

    jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本. 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5.0版本开始引入的一个新功能----deferred对象. ...

  7. 软件工程课堂作业(十一)——NABC分析

    一.团队开发项目:基于Android的重力感应的解锁APP 二.项目特点:区别于一般解锁软件用开机按钮开锁解锁,我们的重力解锁软件根据动作实现解锁,减少了开机按钮的使用频率,提高寿命. 三.NABC分 ...

  8. ubuntu下修改MySQL的配置文件my.cnf

    先sudo su转换成root,再用cd转到/etc/MySQL目录下,用chmod修改权限(chmod 755 my.cnf),但这样还不能修改,再用vi命令(vi my.cnf),通过上下方向键将 ...

  9. 利用闭包判断Dom元素和滚动条的方向

    本文收集整理自网上. 一,判断滚动条的方向,利用闭包首先保存滚动条的位置,然后当滚动时候不断更新滚动初始值,然后通过差指判断方向 function scroll(fn) { //利用闭包判断滚动条滚动 ...

  10. 关于FEer发展方向的思考

    今天学习了HTTP权威指南这本书,虽然标题是对FEer发展的思考,不过我打算稍后再说这个议题,先对今天学习的内容做个总结. 首先:原来访问服务器的方式有多重,核心是URI,也就是统一资源定位,按照访问 ...