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?

InputInput 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.

OutputFor 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!
题意:有3种操作:0.将所给元素压进容器。1.将所给元素从容器中删除,若没有输出No Elment! 2.找比a大的数中第k大的数(可用二分逼近)
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
typedef long long ll;
int lowbit(int x){return x&-x;}
const int maxn=;
int c[maxn+];
int n,m;
void update(int x,int v)
{
for(int i=x;i<=maxn;i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int ans=;
for(int i=x;i>=;i-=lowbit(i))
ans+=c[i];
return ans;
} int main()
{
ios::sync_with_stdio();
int m;
while(cin>>m){
memset(c,,sizeof(c));
while(m--){
int op,x,k;
cin>>op;
if(op==){
cin>>x;
update(x,);
}
else if(op==){
cin>>x;
if(sum(x)-sum(x-)==)cout<<"No Elment!"<<endl;
else update(x,-);
}
else{
cin>>x>>k;
int l=x+,r=,ans=-;
while(l<=r){
int mid=(l+r)/;
if(sum(mid)-sum(x)>=k)//如果[x+1,mid]区间比x大的超过k个,说明答案可以更小
ans=mid,r=mid-;
else l=mid+;//否则答案需要更大
}
if(ans==-)cout<<"Not Find!"<<endl;
else cout<<ans<<endl;
}
}
}
return ;
}

2:线段树版本(不能简单的用二分了,需要直接写个递归函数,有点像权值线段树的意思了)

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
typedef long long ll;
int lowbit(int x){return x&-x;}
const int maxn=;
int tree[maxn<<];
void update(int l,int r,int x,int v,int rt)
{
if(l==r){
if(v==)tree[rt]++;
else{
if(tree[rt]==)
cout<<"No Elment!"<<endl;
else
tree[rt]--;
}
return ;
}
int mid=(l+r)/;
if(x<=mid)update(l,mid,x,v,rt*);
else update(mid+,r,x,v,rt*+);
tree[rt]=tree[rt*]+tree[rt*+];
}
int querysum(int l,int r,int L,int R,int rt)
{
if(L<=l&&R>=r)return tree[rt];
int mid=(l+r)/;
int ans=;
if(L<=mid)ans+=querysum(l,mid,L,R,rt*);
if(R>=mid+)ans+=querysum(mid+,r,L,R,rt*+);
return ans;
}
int query(int l,int r,int k,int rt)
{
if(l==r)return l;
int mid=(l+r)/;
if(k<=tree[rt*])return query(l,mid,k,rt*);
else return query(mid+,r,k-tree[rt*],rt*+);
}
int main()
{
int m;
int n=;
while(scanf("%d", &m) != EOF){
memset(tree,,sizeof(tree));
while(m--){
int op,x,k;
scanf("%d",&op);
if(op==){
scanf("%d",&x);
update(,n,x,,);
}
else if(op==){
scanf("%d",&x);
update(,n,x,-,);
}
else{
scanf("%d%d",&x,&k);
int pos=querysum(,n,,x,);
int ans=query(,n,pos+k,);
if(ans>=maxn)cout<<"Not Find!"<<endl;
else cout<<ans<<endl;
}
}
}
return ;
}

G - KiKi's K-Number(树状数组求区间第k大)的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  3. 树状数组求区间和模板 区间可修改 参考题目:牛客小白月赛 I 区间

    从前有个东西叫树状数组,它可以轻易实现一些简单的序列操作,比如单点修改,区间求和;区间修改,单点求值等. 但是我们经常需要更高级的操作,比如区间修改区间查询.这时候树状数组就不起作用了,只能选择写一个 ...

  4. 【POJ2104】【整体二分+树状数组】区间第k大

    Description You are working for Macrohard company in data structures department. After failing your ...

  5. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  6. 【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012 题意:维护一个数列,开始时没有数值,之后会有两种操作, Q L :查询数列末 ...

  7. 【BZOJ1012】【树状数组求区间最值】最大数maxnumber

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  8. 主席树套树状数组 动态区间第k小

    先打上代码以后更新解释 #include <cstdio> #include <iostream> #include <algorithm> #include &l ...

  9. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

随机推荐

  1. great vision|be quite honest with you

    won a national championship拿到全国冠军 come play for you参加你的队伍 Really not true事实并非如此 Being the Socratic p ...

  2. Python爬虫之解析网页

    常用的类库为lxml, BeautifulSoup, re(正则) 以获取豆瓣电影正在热映的电影名为例,url='https://movie.douban.com/cinema/nowplaying/ ...

  3. SpringBoot项目 org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Jetty servlet container报错

    SpringBoot项目启动报错 ERROR 2172 --- [ main] o.s.boot.SpringApplication : Application startup failed org. ...

  4. LVS三种模式区别

    参考文档 http://www.magedu.com/65436.html 名词:CIP 客户端IP地址   VIP:即DS服务器上的代理IP地址,也是客户端访问的执行IP地址 1.NAT模式 ①.客 ...

  5. LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

    题目: Given a binary tree, find the length of the longest path where each node in the path has the sam ...

  6. 最短路——迪杰斯特拉算法 HDU_3790

    初识最短路,今天只弄了一个迪杰斯特拉算法,而且还没弄成熟,只会最基本的O(n^2),想弄个优先队列都发现尼玛被坑爆了,那个不应该用迪杰斯特拉算法写 表示还是不会优化版的迪杰斯特拉算法,(使用优先队列) ...

  7. addlayer添加神经网络层

    def addlayer(inputs,insize,outsize,activity_function = None):    weights = tf.Variable(tf.random_nor ...

  8. tomcat8.5的安装、卸载、配置和部署

    安装和卸载 下载 http://tomcat.apache.org/ 环境变量 1.点击此电脑 右键—>属性. 2.创建变量名为CATALINA_HOME,的值为所解压安装tomcat的本地目录 ...

  9. java8+tomcate8仅支持TLSv1.2

    1.编辑$tomcat_home/conf/server.xml <Connector protocol="org.apache.coyote.http11.Http11NioProt ...

  10. JavaScript—原生轮播和无缝滚动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...