杭电 2852 树状数组+二分

题目:

KiKi's K-Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1314    Accepted Submission(s): 565


Problem Description
For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar problem, kiki wants to design a container, the container is to support the three operations.

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?
 

Input
Input some groups of test data ,each test data the first 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.
 

Output
For 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!
ac代码:

//树状数组的应用。题目里给了3中操作,插入,删除和查询
//插入和删除可以归结为一类操作,用树状数组插入时,每个
//元素的值增加1即可,删除时,每个元素的值减1即可。查询
//操作时,用到了树状数组的查询和二分的方法。设比a大的第
//k大的元素,则设total=num[a+1]+...+num[M],若total比k大,
//则折半,时间复杂度为log(n);
#include <iostream>
#include <cstdio>
using namespace std;
const int M=100002;
int num[M];
int lowbit(int x){
  return x&(-x);
}//lowbit
void add(int pos,int value){
	while(pos<M){
	  num[pos]+=value;
	 // printf("num[%d]=%d\n",pos,num[pos]);
	  pos+=lowbit(pos);
	}
}//add
int sum(int x){
  int total=0;
  while(x>0){
    total+=num[x];
	x-=lowbit(x);
  }
 // printf("total=%d\n",total);
  return total;
}//sum
int find(int x,int y){
  int newsum=sum(x);
  int leftside=x+1;//最左为x+1
  int rightside=M-1;//最右为M-1
  int ans=M;
  int total=0;
  while(leftside<=rightside){
	int pos=(leftside+rightside)>>1;
    total=sum(pos)-newsum;
	if(total>=y){
	  rightside=pos-1;
	  if(pos<ans)
		  ans=pos;
	}//if
	else
		leftside=pos+1;
  }//while
  return ans;
}//find
int main(){
	freopen("1.txt","r",stdin);
	int n;
	while(~scanf("%d",&n)){
		int type;
		int x,y;
		for(int i=0;i<M;++i)
		   num[i]=0;
		while(n--){
		  scanf("%d",&type);
		  if(type==0)
		  {
		    scanf("%d",&x);
			add(x,1);
		  }//if
		  else if(type==1){
		    scanf("%d",&x);
			if(sum(x)-sum(x-1)==0)
				printf("No Elment!\n");
			else
				add(x,-1);
		  }//else if
		  else{
		    scanf("%d%d",&x,&y);
			int count=find(x,y);
			if(count==M)
				printf("Not Find!\n");
			else
				printf("%d\n",count);
		  }//else
		}//while
	}//while
  return 0;
}//main


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick Tree)是一种用于快速维护数组前缀和的数据结构。它可以在 $O(\log n)$ 的时间内完成单点修改和前缀查询操作,比线段树更加简洁高效。 下面是 Java 实现的树状数组详解: 首先,在 Java 中我们需要使用数组来表示树状数组,如下: ``` int[] tree; ``` 接着,我们需要实现两个基本操作:单点修改和前缀查询。 单点修改的实现如下: ``` void update(int index, int value) { while (index < tree.length) { tree[index] += value; index += index & -index; } } ``` 该函数的参数 `index` 表示要修改的位置,`value` 表示修改的值。在函数内部,我们使用了一个 `while` 循环不断向上更新树状数组中相应的节点,直到到达根节点为止。具体来说,我们首先将 `tree[index]` 加上 `value`,然后将 `index` 加上其最后一位为 1 的二进制数,这样就可以更新其父节点了。例如,当 `index` 为 6 时,其二进制表示为 110,最后一位为 2^1,加上后变为 111,即 7,这样就可以更新节点 7 了。 前缀查询的实现如下: ``` int query(int index) { int sum = 0; while (index > 0) { sum += tree[index]; index -= index & -index; } return sum; } ``` 该函数的参数 `index` 表示要查询的前缀的结束位置,即查询 $[1, index]$ 的和。在函数内部,我们同样使用了一个 `while` 循环不断向前查询树状数组中相应的节点,直到到达 0 为止。具体来说,我们首先将 `sum` 加上 `tree[index]`,然后将 `index` 减去其最后一位为 1 的二进制数,这样就可以查询其前一个节点了。例如,当 `index` 为 6 时,其二进制表示为 110,最后一位为 2^1,减去后变为 100,即 4,这样就可以查询节点 4 的值了。 最后,我们还需要初始化树状数组,将其全部置为 0。初始化的实现如下: ``` void init(int[] nums) { tree = new int[nums.length + 1]; for (int i = 1; i <= nums.length; i++) { update(i, nums[i - 1]); } } ``` 该函数的参数 `nums` 表示初始数组的值。在函数内部,我们首先创建一个长度为 `nums.length + 1` 的数组 `tree`,然后逐个将 `nums` 中的元素插入到树状数组中。具体来说,我们调用 `update(i, nums[i - 1])` 来将 `nums[i - 1]` 插入到树状数组的第 `i` 个位置。 到此为止,我们就完成了树状数组的实现。可以看到,树状数组的代码比线段树要简洁很多,而且效率也更高。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值