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. 【checkbox-group、checkbox】 多项选择器组件说明

    checkbox-group组件包裹checkbox组件的容器 原型: <check-group bindchange="[EventHandle]"> <che ...

  2. lintcode539 移动零

    移动零 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序 注意事项 1.必须在原数组上操作2.最小化操作数 您在真实的面试中是否遇到过这个题? Yes 样例 给出  ...

  3. django 增删改查操作 数据库Mysql

    下面介绍一下django增删改查操作: 1.view.py # -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom dja ...

  4. 【转】UTF8字符串转换为汉字 c#,转自游戏开发主席

    using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...

  5. scipy 图像处理-深度学习

    scipy 图像处理(scipy.misc.scipy.ndimage).matplotlib 图像处理 from scipy.misc import imread / imsave / imshow ...

  6. 【机器学习】线性回归sklearn实现

    线性回归原理介绍 线性回归python实现 线性回归sklearn实现 这里使用sklearn框架实现线性回归.使用框架更方便,可以少写很多代码. 写了三个例子,分别是单变量的.双变量的和多变量的.单 ...

  7. 2018科大讯飞AI营销算法大赛全面来袭,等你来战!

    AI技术已成为推动营销迭代的重要驱动力.AI营销高速发展的同时,积累了海量的广告数据和用户数据.如何有效应用这些数据,是大数据技术落地营销领域的关键,也是检测智能营销平台竞争力的标准. 讯飞AI营销云 ...

  8. "Hello world!"团队第一次会议

    今天是我们"Hello world!"团队第一次召开会议,今天的会议可能没有那么正式,但是我们一起确立了选题——基于WEB的售票系统.博客内容是: 1.会议时间 2.会议成员 3. ...

  9. Ubuntu 基础操作 基础命令 热键 man手册使用 关机 重启等命令使用

    . : 关机, 如果将Linux默认运行等级设置为0, 系统将无法启动; -- : 多用户模式, 允许使用网络文件系统, 一般不使用图形界面登陆就是这种模式; -- : 多用户图形界面模式, 该模式下 ...

  10. 算法与数据结构3.3 calculator

    ★实验任务 小 V 发明了一个神奇的整数计算器: 给定一个合法的表达式,这个计算器能求出这个表达式的最终答案. 表达式可能包含: +:运算符,整数加法.如 1+1=2 -:运算符,整数减法.如 1-1 ...