HDU 2795

线段树递归查询,取最大值建树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
#define LL  __int64
using namespace std;
const int N=200050;
const int root=1;
struct Node {
	int val,left,right;
}tree[N*4];
int h,w,n,wi;
	
void build(int now,int l,int r){
	tree[now].val=w;
	tree[now].left=l;
	tree[now].right=r;
	if(l==r) return ;
	int m=(l+r)/2;
	build(now*2,l,m);
	build(now*2+1,m+1,r);
}

int query(int now,int l,int r){
	if(l==r){
		tree[now].val-=wi;
		return l;
	}
	int m=(tree[now].left+tree[now].right)/2;
	int ret;
	if(wi<=tree[now*2].val){
		ret=query(now*2,l,m);
	}
	else if(wi<=tree[now*2+1].val)
	ret=query(now*2+1,m+1,r);
	tree[now].val=max(tree[now*2].val,tree[now*2+1].val);
	return ret;
}

int main(){
	while(scanf("%d%d%d",&h,&w,&n)!=EOF){
		int l=min(h,n);
		build(root,1,l);
		for(int i=1;i<=n;i++){
			scanf("%d",&wi);
			if(wi>tree[root].val) printf("-1\n");
			else{
				printf("%d\n",query(root,1,l));
			}
		}
	}
	return 0;
}

  

posted @ 2015-03-05 10:37  chenjunjie1994  阅读(134)  评论(0编辑  收藏  举报