hdu KiKi's K-Number 主席树
KiKi's K-Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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?
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.
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
6
Not Find!
2
2
4
Not Find!
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=2e9;
const ll INF=1e18+,mod=;
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 pre,int l,int r,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,ls[pre],l,mid,ls[pos]);
else
update(p,c,rs[pre],mid+,r,rs[pos]);
}
int rank(int s,int e,int L,int R,int l,int r)
{
if(L<=l&&r<=R)return sum[e]-sum[s];
int mid=(l+r)>>;
int ans=;
if(L<=mid)
ans+=rank(ls[s],ls[e],L,R,l,mid);
if(R>mid)
ans+=rank(rs[s],rs[e],L,R,mid+,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) return query(ls[L],ls[R],l,mid,k);
else return query(rs[L],rs[R],mid+,r,k-x);
}
};
Chairmantree tree;
int main()
{
int n,le=1e5+;
while(~scanf("%d",&n))
{
tree.init();
tree.build(,le,tree.rt[]);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
if(x==)
{
int z;
scanf("%d",&z);
tree.update(z,,tree.rt[i-],,le,tree.rt[i]);
}
else if(x==)
{
int z;
scanf("%d",&z);
if(tree.rank(tree.rt[],tree.rt[i-],z,z,,le))
{
tree.update(z,-,tree.rt[i-],,le,tree.rt[i]);
}
else
{
tree.update(z,,tree.rt[i-],,le,tree.rt[i]);
printf("No Elment!\n");
}
}
else
{
tree.update(,,tree.rt[i-],,le,tree.rt[i]);
int a,k;
scanf("%d%d",&a,&k);
int v=tree.rank(tree.rt[],tree.rt[i],,a,,le);
k+=v;
int q=tree.rank(tree.rt[],tree.rt[i],,le,,le);
//cout<<"xxxx "<<v<<" "<<k<<" "<<q<<endl;
if(k>q)
printf("Not Find!\n");
else
printf("%d\n",tree.query(tree.rt[],tree.rt[i],,le,k));
}
//for(int j=1;j<=9;j++)
//cout<<"xxx "<<j<<" "<<tree.rank(tree.rt[0],tree.rt[i],j,j,1,le)<<endl;
}
}
return ;
}
hdu KiKi's K-Number 主席树的更多相关文章
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- hdu 2665 Kth number 主席树
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- 【POJ】2104 K-th Number(区间k大+主席树)
http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- HDU 4417 Super Mario(主席树 区间不超过k的个数)题解
题意:问区间内不超过k的个数 思路:显然主席树,把所有的值离散化一下,然后主席树求一下小于等于k有几个就行.注意,他给你的k不一定包含在数组里,所以问题中的询问一起离散化. 代码: #include& ...
- HDU - 2665 Kth number 主席树/可持久化权值线段树
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- nginx如何利用自定义的conf文件启动?
1.进入nginx的安装目录; 2.nginx -s -c conf/my.conf (校验该文件是否有效) nginx -s -c conf/my.conf 命令会卡死,存疑 3.start ...
- 查看Oracle的redo日志切换频率
1.Oracle log 每次切换会记录到告警日志中 设想写个方案来查看log切换频率来判断Oracle log是否应该更改大小. 2.sql a.查看redo日志信息 select * from v ...
- SQL中distinct的用法(转载)
1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 ...
- 170522、Linux 平台通过 nginx 和 vsftpd 构建图片服务器
Nginx WEB 服务器 Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第 ...
- 焦作网络赛L-Poor God Water【矩阵快速幂】
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- Django - 常用配置
一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...
- Windows版本搭建安装React Native环境配置
1 安装Chocolatey 打开cmd黑窗口 @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-obje ...
- NULL头文件
#include<stddef.h> NULL不是C语言基本类型,其定义在stddef.h文件中,作为最基本的语言依赖宏存在.但是随着C/C++的发展,很多文件只要涉及了系统或者标准操作都 ...
- Spring—spring概述
Spring框架的特点? 1:轻量级,一站式开发 2:易用,追求代码的最佳实现 3:Spring的内容: a:Ioc容器 b:AOP实现 c:数据访问支持(ORM框架/声明事务[Transaction ...
- Java backup
待补充 ........ 0:常用头文件(待补充) import java.util.Arrays; import java.util.HashSet; import java.util.TreeSe ...