G - KiKi's K-Number(树状数组求区间第k大)
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?
 
 
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大)的更多相关文章
- HDU 1394 Minimum Inversion Number   (  树状数组求逆序数    )
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ... 
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ... 
- 树状数组求区间和模板  区间可修改  参考题目:牛客小白月赛 I 区间
		从前有个东西叫树状数组,它可以轻易实现一些简单的序列操作,比如单点修改,区间求和;区间修改,单点求值等. 但是我们经常需要更高级的操作,比如区间修改区间查询.这时候树状数组就不起作用了,只能选择写一个 ... 
- 【POJ2104】【整体二分+树状数组】区间第k大
		Description You are working for Macrohard company in data structures department. After failing your ... 
- [Split The Tree][dfs序+树状数组求区间数的种数]
		Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ... 
- 【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值
		题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012 题意:维护一个数列,开始时没有数值,之后会有两种操作, Q L :查询数列末 ... 
- 【BZOJ1012】【树状数组求区间最值】最大数maxnumber
		Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ... 
- 主席树套树状数组 动态区间第k小
		先打上代码以后更新解释 #include <cstdio> #include <iostream> #include <algorithm> #include &l ... 
- UVA11525 Permutation[康托展开 树状数组求第k小值]
		UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ... 
随机推荐
- CodeForces - 446A DZY Loves Sequences(dp)
			题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ... 
- 为什么ApplePay在中国一直火不起来?
			今年7月,易观发布<中国第三方移动支付市场2018年第1季度监测报告>.报告显示,2018年第一季度支付宝以53.76%的市场份额占据移动支付头把交椅,腾讯金融(微信支付)则以38.95% ... 
- Maven:Eclipse导入从SVN上检出的Maven多模块工程
			大致步骤: 1.从SVN中检出多模块项目,名称随意(Eclipse中可以在[Window ==>>Show View==>>Other==>>SVN==>&g ... 
- 百度网盘下载神器 PanDownload v2.0.9(破解版、不限速)
			一直用这个软件来下载百度网盘的东西,不限速,贼爽. 链接:https://pan.baidu.com/s/1UjF47YWd2v9x52c5sjhutQ 提取码:v9pe 也可以直接到官网下载:ht ... 
- 深入分析解决Deepin 15环境变量修改问题,完成JAVA环境搭建
			最近使用deepin配置JAVA环境时发现不论是修改/etc/profile还是 ~/.profile多次尝试后均无效,不得其解,最后通过官方论坛看到大神对deepin环境配置的解释,以及寻找到相关解 ... 
- POJ 1141 经典DP 轨迹打印
			又几天没写博客了,大二的生活实在好忙碌啊,开了五门专业课,每周都是实验啊实验啊实验啊....我说要本月刷够60题,但好像完不成了,也就每天1题的样子.如今写动规还是挺有条理的,包括这道需要打印轨迹,其 ... 
- idea拉取git项目并创建为maven项目(新创建github项目)
			0 环境 系统环境:win10 编辑器:idea 1 正文 1 clone项目 跟着提示yes 下一步 2 在根节点添加pom.xml(maven) <?xml version="1. ... 
- Maven--反应堆(Reactor)
			在一个多模块的 Maven 项目中,反应堆是指所有模块组成的一个构建结构.对于单模块的项目,反应堆就是该模块本身.但对于多模块项目来说,反应堆就包含了各模块之间继承与依赖的关系,从而能够自动计算出合理 ... 
- Python 字典列表嵌套输入问题
- (一)ORBSLAM2主要配置
			(1)ORBSLAM2文件夹里面有个build.sh文件,里面主要是编译时终端需要执行的命令,这里把它们放到.sh文件中统一执行. (2)阅读ORBSLAM2的CmakeList可以知道运行ORBSL ... 
