线段树的区间最大值问题,边界特殊处理一下。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define maxn 222222
int tree[maxn << 2];
int w,h,m,ok;
void BuildTree(int L,int R,int pos){
tree[pos] = w;
if(L == R)
return ;
int m = (L + R) >> 1;
BuildTree(L,m,pos << 1);
BuildTree(m + 1 , R , (pos << 1)|1);
return ;
}
void UpDate(int len,int L,int R,int pos){
if(L == R){
if(tree[pos] >= len){
printf("%d\n",L);
tree[pos] -= len;
}
else
printf("-1\n");
return ;
}
int m = (L + R) >> 1;
int e1 = tree[pos << 1]; //左边最大值
int e2 = tree[(pos << 1)|1]; //右边最大值
if(e1 >= len)
UpDate(len,L,m,pos << 1);
else if(e2 >= len)
UpDate(len,m + 1,R,(pos << 1)|1);
else{
printf("-1\n");
return ;
}
tree[pos] = max(tree[pos << 1],tree[(pos <<1)|1]);
}
int main(){
int x;
while(scanf("%d%d%d",&h,&w,&m) != EOF){
if(h > m) h = m;
BuildTree(1,h,1);
for(int i = 0 ; i < m ; i++){
scanf("%d",&x);
UpDate(x,1,h,1);
}
}
return 0;
}
原文:http://blog.csdn.net/u013451221/article/details/39255909