hdu2852--KiKi's K-Number(段树,求第一k的数量)
KiKi's K-Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2546 Accepted Submission(s): 1174
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.
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
No Elment!
6
Not Find!
2
2
4
Not Find!
三种操作,0 代表加入一个数x。1代表删除一个数x。2代表 找比a大的第k个数,使用线段树求解,线段树统计在一个区间内出现的数的个数,对于找比a大的第k个数。从a開始向后查找,假设在某段中累加的和大于k。就让它跳入这段中,直到深入到一个叶子节点时,刚好ans >= k。
对线段树的更新不解释,主要是查询的时候
1.假设当前区间[l,r]中 r<=a那么这一段的数都不用统计。
2.假设r >a,代表该段中存在比a大的数就要向下深入。
3.假设l > a,那么该段中所有的点都会大于a,開始推断。假设该段所有增加后仍然小于k。那么就能够所有增加。假设加进去以后大于等于k,那么就要向下深入。一直深入到叶子节点,满足条件的,最左的叶子节点就是我们要求的值。(线段树。从左向右查找。一定能够找到第一个)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 110000
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
int cl[maxn<<2] , k1[maxn] , k2[maxn<<2] , top ;
void push_up(int_now)
{
cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;
}
void creat(int_now)
{
cl[rt] = 0 ;
if(l != r)
{
creat(lson);
creat(rson);
push_up(now);
}
else
{
cl[rt] = 0 ;
k2[rt] = ++top ;
}
}
void update(int x,int d,int_now)
{
if( l > x || r < x )
return ;
if( l == r && l == x )
{
cl[rt] += d ;
return ;
}
update(x,d,lson);
update(x,d,rson);
push_up(now);
}
int query(int ll,int ans,int num,int_now)
{
if( r <= ll )
return 0;
if( ll < l )
{
if( ans + cl[rt] < num )
return ans + cl[rt] ;
if(ans < num && ans + cl[rt] >= num && l == r )
{
printf("%d\n", k2[rt] );
return ans + cl[rt] ;
}
if(ans < num && ans + cl[rt] >= num )
{
if( ans + cl[rt<<1] >= num )
ans = query(ll,ans,num,lson);
else
ans = query(ll,ans+cl[rt<<1],num,rson);
return ans ;
}
}
else
{
if( ans < num )
ans = query(ll,ans,num,lson);
if(ans < num)
ans = query(ll,ans,num,rson);
return ans;
}
}
int main()
{
int m , i , n , l , r , x , temp , num ;
while(scanf("%d", &m) != EOF)
{
top = 0 ;
n = maxn ;
creat(root);
memset(k1,0,sizeof(k1));
while(m--)
{
scanf("%d", &temp);
if( temp == 0 )
{
scanf("%d", &x);
k1[x]++ ;
update(x,1,root);
}
else if( temp == 1 )
{
scanf("%d", &x);
if( k1[x] == 0 )
printf("No Elment!\n");
else
{
k1[x]-- ;
update(x,-1,root);
}
}
else
{
scanf("%d %d", &l, &num);
x = query(l,0,num,root);
if(x < num)
printf("Not Find!\n");
}
}
} }
版权声明:转载请注明出处:http://blog.csdn.net/winddreams
hdu2852--KiKi's K-Number(段树,求第一k的数量)的更多相关文章
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- poj-1151-Atlantis-线段树求面积并
非常裸的线段树求面积并. 坐标须要离散化一下. #include<stdio.h> #include<iostream> #include<stdlib.h> #i ...
- hdu-3642--Get The Treasury-线段树求面积并
求空间中叠加3次及3次以上的体积. 由于|z|<=500.所以直接把z轴剥离出来1000层. 然后对于每一层进行线段树求面积并. #include<stdio.h> #include ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- hdu2852 KiKi's K-Number
Problem Description For the k-th number, we all should be very familiar with it. Of course,to kiki i ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- HDU 1542.Atlantis-线段树求矩形面积并(离散化、扫描线/线段树)-贴模板
好久没写过博客了,这学期不是很有热情去写博客,写过的题也懒得写题解.现在来水一水博客,写一下若干年前的题目的题解. Atlantis Time Limit: 2000/1000 MS (Java/Ot ...
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53312 Accepted: 16050 Des ...
随机推荐
- linux下查看日志基本命令
1.cat命令: 功能:1)显示整个文件. 演示样例: $ cat fileName 2)把文件串连接后传到基本输出,如将几个文件合并为一个文件或输出到屏幕. 演示样例: $ cat file1 fi ...
- 百度富文本编辑器UEditor1.3上传图片附件等
今天一直在整我的一个项目的编辑器上传图片,我用的是百度UEditor 1.3版本号的:如今已经有了1.4的了,只是还算比較新吧,可是官网上面没有上传图片这些的教程,而网上对于这方面的资料非常少啊,折腾 ...
- SE 2014年5月6日
如图配置: 三台交换机两两相连接,构成一二层环路,同时为了保证链路的较为可靠,使用双线链接 请用自己的语言描述以上拓扑搭建的优劣势:并使用哪些技术较为合理,请描述并实施 SW3为接入层交换机,下链接三 ...
- WebService之Soap头验证入门
1.新建一个类,如"AuthHeaderUser",继承于"System.Web.Services.Protocols.SoapHeader"类 2.新建Web ...
- iOS7 文本转语音 AVSpeechSynthesizer
OS7 的这个功能确实不错.我刚试了下,用官方提供的API ,简单的几句代码就能实现文本转语音! Xcode 5.0 工程建好后首先把AVFoundation.framework 加入到工程 AVSp ...
- MySQL命令行数据操作使用心得(总结版)
Char 0~255 Varchar 0~65535 text 0~65535(只能保存字符) Longtext 0~4294967295(只能保存字符) CMD登陆mysql mysql -u ro ...
- Java整型数组的最大长度到底有多长?
Java整型数组的最大长度到底有多长? 今天上网查了一下,各种说法都有,这个问题似乎总困扰我们Java初学者,无奈,只好自己试了一下,以下是我的测试代码,如果有错误,还望不吝赐教! 使用eclipse ...
- 一类斜率优化的dp(特有性质:只能连续,不能交叉)
hdu3480 给定一个有n个数的集合,将这个集合分成m个子集,要求子集的并等于全集求花费最小. 花费为该子集的(最大数-最小数)的平方. 我们将n个数排序, a < b < c < ...
- 一篇哥们自己的写的IBM电话面试感想-@大国
两天没写博了,还是没有养成一个习惯.前天和昨天休息,和哥们几个出去打球,运动一下,放松下脑子.今天就补一篇我哥们自己的写的关于他的IBM电话面试的感想,希望能帮到有需要的人. ------------ ...
- TCP header
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vzc2VyNDM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...