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)!+ ...
随机推荐
- 启动运行python3时 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 170: illegal multibyte sequence
重现 在cmd中输入Python,运行后,出现以下错误: Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...
- python类(3)感悟
1.关于类属性attribute和实例(对象)特性property思考 为什么特性会出现,类属性不能完全替代它吗? 属性: python在为属性赋值时,只会搜索对象本身的__dict__,如果找不到对 ...
- docker入门1---docker的简介和安装
Tomxin7 Simple, Interesting | 简单,有趣 什么是Docker? 简介: Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发 ...
- UML-设计对象时涉及的制品有哪些?
1.SSD.系统操作.交互图--->用例实现 1).通信图 2).顺序图 将SSD中的系统操作作为领域层控制器对象的起始消息.这是关键. 2.用例--->用例实现 在整个开发过程中,业务人 ...
- Spring Cloud Zuul 网关服务的fallback
当我们的zuul进行路由分发时,如果后端服务没有启动,或者调用超时,这时候我们希望Zuul提供一种降级功能,而不是将异常暴露出来. Spring cloud zuul提供这种降级功能,操作步骤如下: ...
- 35. docker swarm dockerStack 部署 投票应用
1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...
- leetcode 690.员工的重要性
题目: 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结 ...
- 移植zlib
平台说明 开发平台:Ubuntu12.04 编 译器:arm-linux-gcc version 4.4.4 (4.4.4_09.06.2010) Zlib源码包:zlib-1.2.11.tar.gz ...
- day65-CSS选择器和样式优先级
1. CSS CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). 2.CSS语法 每个 ...
- ZJNU 2208 - 你渴望力量吗
在图的最外面套一层0(防止到头) 然后搜索图有多少块在 '0'有两块0,一块1 '1'有一块0,一块1 其余情况不存在 #include<stdio.h> ],dx[]={,,,-},dy ...