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. POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))

    题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...

  2. POJ 2251:Dungeon Master

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20687   Accepted: 8004 D ...

  3. VMWare WorkStation15--Win10下开机启动虚拟机

    参考 https://www.cnblogs.com/qmfsun/p/6284236.html http://www.cnblogs.com/eliteboy/p/7838091.html VMWa ...

  4. CSS的Flex弹性布局概念

    1.Flex概念: Flex是Flexible Box的缩写,顾名思义为“弹性布局”,用来为盒装模型提供最大的灵活性. 任何一个容器都可以指定为Flex 布局. 设为flex布局以后,子元素的floa ...

  5. 最短路问题-- Dijkstra Choose the best route

    Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...

  6. 题解 P1403 【[AHOI2005]约数研究】

    题目 看到题解区很多人直接给出结论:答案为 \(\displaystyle \sum_{i=1}^n\lfloor{n\over i}\rfloor\) ,没给出证明,这里给出证明 [分析] 首先,我 ...

  7. 72)MFC测试动态共享库

    动态共享库: 首先我建立一个新的动态库: 然后不选择空项目了,因为我们普通的cpp文件 入口是main  win32入口是winmain  那么这个动态库的入口在哪里  我们就是为了看一看: 出来这样 ...

  8. 吴裕雄--天生自然MySQL学习笔记:MySQL UNION 操作符

    MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 语法 MySQL UNION 操作符语法格式: SELECT ...

  9. 关于 tf.image.crop_and_resize的使用

    https://blog.csdn.net/m0_38024332/article/details/81779544 关于 tf.image.crop_and_resize 的使用  最近在学习fas ...

  10. js正则验证数字的方法

    正则验证数字的方法: <script type="text/javascript"> function validate(){ var reg = new RegExp ...