hdu2852 KiKi's K-Number 树状数组求第k大数

//再求第k大数时只需要getsum(b-1)<getsum(a)+k<=getsum(b)

//b就是a的第k大数

//又gesum(b-1)<=getsum(b)则可以用二分查找来做

#include<iostream>

#include<cstdio>

#include<cstring>

using namespace std;

const int maxn=100010;

int tree[maxn];

int lowbit(int i)

{

return (i&(-i));

}

int getsum(int i)

{

int sum=0;

while(i>0)

{

sum+=tree[i];

i-=lowbit(i);

}

return sum;

}

void update(int i,int dx)

{

while(i<maxn)

{

tree[i]+=dx;

i+=lowbit(i);

}

}

void find(int left,int right,int num)

{

while(left<=right)

{

//int middle=(left+right)/2;

int middle = left + (right - left) / 2;

int tmp=getsum(middle);

if(tmp<num)

left=middle+1;

else

right=middle-1;

}

if(left>=maxn)

printf("Not Find!\n");

else

printf("%d\n",left);

}

int main()

{

//freopen("in.txt","r",stdin);

// freopen("out.txt","w",stdout);

int m;

while(scanf("%d",&m)!=EOF)

{

int p,a,k;

memset(tree,0,sizeof(tree));

while(m--)

{

scanf("%d",&p);

if(p==0)

{

scanf("%d",&a);

update(a,1);

}

else if(p==1)

{

scanf("%d",&a);

if(!(getsum(a)-getsum(a-1)))

{

printf("No Elment!\n");

continue;

}

else

update(a,-1);

}

else

{

scanf("%d%d",&a,&k);

int tmp=getsum(a);

find(a+1,maxn-1,tmp+k);

}

}

}

}

hdu2852 KiKi's K-Number 树状数组求第k大数

时间: 2024-12-31 14:39:20

hdu2852 KiKi's K-Number 树状数组求第k大数的相关文章

树状数组求第K小值 (spoj227 Ordering the Soldiers &amp;&amp; hdu2852 KiKi&#39;s K-Number)

题目:http://www.spoj.com/problems/ORDERS/ and http://acm.hdu.edu.cn/showproblem.php?pid=2852 题意:spoj227:告诉每个位置前面有多少个数比当前位置小,求出原序列.hdu2852:设计一个容器,支持几种操作:增加/删除元素,求容器中比a大的数中第k小的数是多少. 分析:两个题思路都是求数组里面的第K小的数.开始一直在找O(N*logN)的方法,后来发现O(N*logN*logN)也是可以过的...两步:和

hdu-2852 KiKi&#39;s K-Number---二分+树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意: 题意:    给出三种操作,    0 在容器中插入一个数.    1 在容器中删除一个数.    2 求出容器中大于a的第k大元素. 解题思路: 用树状数组维护每个值,插入数字是add(x, 1),删除时add(x, -1) 查询第k大时,先判断是否存在,存在的话直接根据树状数组sum值的单调性二分法求解即可 1 #include<iostream> 2 #include&l

hdu 2985 The k-th Largest Group 树状数组求第K大

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted: 2712 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g

HDU 5249 离线树状数组求第k大+离散化

KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1160    Accepted Submission(s): 488 Problem Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度.数十亿的请求被推到一个大管道后同时服务从管头拉取请求.让我们来定义每个请求都有一个重要值.我的

POJ2985 The k-th Largest Group[树状数组求第k大值 并查集]

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted: 2875 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu1394Minimum Inversion Number树状数组求逆序对水题

//ans[i]=ans[i-1]+(n+1)-2*num[i] //num[i]为输入时的数据 //ans[i]为m=i时的逆序数 //用树状数组求ans[0]的逆序对 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=5010; int num[maxn]; int tree[maxn]; int lowbit(int i) { retu

HDU 1394 Minimum Inversion Number (树状数组求逆序对)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多少. 一开始就求原来初始数组的逆序对,树状数组求或者归并方法求(可以看<挑战程序设计>P178),然后根据最前面的元素大小递推一下每次移到最后得到的逆序数,取最小值. 1 #include <iostream> 2 #include <cstdio> 3 #include

zoj 3635 Cinema in Akiba (树状数组求第K大)

Cinema in Akiba Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies wit