首页 > 其他 > 详细

HUD2795 线段树(单点更新)

时间:2016-08-08 00:47:18      阅读:207      评论:0      收藏:0      [点我收藏+]

     题目中给出的h和w范围均大,其实n的最大范围才200000,所以我们建立的线段树大小为min(h,n),线段树的每一个节点包含一个变量c,记录当前区间内还剩下的可以put on的最大长度。插入一个数时,如果该数大于该区间最大值,则返回-1,说明put on不了。否则将它插入到页节点,并返回插入的下标,接着一定不要忘记更新父节点的c值。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 200001;
struct Tnode{
	int e, b;
	int c;
};
int h, w, n,ans;;
Tnode tree[4 * MAXN];
void Create(int v, int b, int e){
	tree[v].b = b;
	tree[v].e = e;
	tree[v].c = w;
	if (e > b){
		int mid = (b + e) >> 1;
		Create(2 * v + 1, b, mid);
		Create(2 * v + 2, mid + 1, e);
	}
}
void Insert(int v,int x){
	if (x > tree[v].c){
		ans = -1;
		return;
	}
	if (tree[v].b==tree[v].e){
		tree[v].c -= x;
		ans = tree[v].b;
		return;
	}
	if (x <= tree[2 * v + 1].c)
		Insert(2 * v + 1, x);
	else
		Insert(2 * v + 2, x);
	tree[v].c = max(tree[2 * v + 1].c, tree[2 * v + 2].c);
}
int main(){
	int x;
	while (~scanf("%d%d%d", &h, &w, &n)){
		int len = min(h, n);
		Create(0, 1, len);
		for (int i = 0; i < n; i++){
			scanf("%d",&x);
			Insert(0, x);
			printf("%d\n", ans);
		}
	}
	return 0;
}

  

 

HUD2795 线段树(单点更新)

原文:http://www.cnblogs.com/td15980891505/p/5747735.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!