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. Python数学运算入门把Python当作计算器

    让我们尝试一些简单的 Python 命令.启动解释器,等待界面中的提示符,>>> (这应该花不了多少时间). 3.1.1. 数字 解释器就像一个简单的计算器一样:你可以在里面输入一个 ...

  2. python内建模块Collections

    # -*- coding:utf-8 -*- # OrderedDict可以实现一个FIFO(先进先出)的dict, # 当容量超出限制时,先删除最早添加的Key: from collections ...

  3. ionic 组件学习

    利用css列表多选框: <div class="{{Conceal}}" > <ion-checkbox color="secondary" ...

  4. 基于MTCNN多任务级联卷积神经网络进行的人脸识别 世纪晟人脸检测

    神经网络和深度学习目前为处理图像识别的许多问题提供了最佳解决方案,而基于MTCNN(多任务级联卷积神经网络)的人脸检测算法也解决了传统算法对环境要求高.人脸要求高.检测耗时高的弊端. 基于MTCNN多 ...

  5. Ubuntu16.04安装truffle时的一些错误

    1.使用truffle时出现 Error: /usr/bin/env: node: 没有那个文件或目录 1.如果是用sudo apt-get install nodejs命令安装的nodejs, ub ...

  6. selenium实现文件上传方法汇总(AutoIt、win32GUI、sengkeys)---基于python

    在使用selenium进行UI自动化测试时,经常会遇到一个关于本地文件上传的问题,解决此问题一般分两种情况: 1. 元素标签为input 2.非input型上传 下面我们分别对着两种情况进行实例分析 ...

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

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

  8. Memory及其controller芯片整体测试方案(上篇)

    如果你最近想买手机,没准儿你一看价格会被吓到手机什么时候偷偷涨价啦! 其实对于手机涨价,手机制造商也是有苦难言,其中一个显著的原因是存储器芯片价格的上涨↗↗↗ >>> 存储器memo ...

  9. 动态内存&对象

    一.对象的生存期 对于 static 对象和自动对象,它们都有着严格定义的生存期. 全局对象:在程序启动时分配,在程序结束时销毁. 局部自动对象:在对象定义语句时分配,在离开块时销毁 局部 stati ...

  10. CSS基础小记

    2017/10/29 CSS 认识CSS样式 CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏览器内的显示样式,如文字 ...