hdoj2795(线段树)

这题要用scanf,不然runtime error

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int sz = 200000;

int sum[sz<<2],ok;

void build(int l,int r,int rt,int res){ 
    sum[rt] = res;
	if(l == r){
		return;
	}
	int mid = (l+r)>>1;
	build(l , mid , rt << 1,res);
	build(mid + 1 , r , rt << 1 | 1,res);
}

void fadd(int l,int r,int rt,int res){
	if(l == r){
		cout<<l<<endl;
		sum[rt] -= res;
		return;
	}
	int mid = (l+r)>>1;
	if(sum[rt<<1] >= res) {
	fadd(l , mid , rt << 1,res);}
	
	else
	fadd(mid + 1 , r , rt << 1 | 1,res);
	
	sum[rt] = max(sum[rt<<1],sum[rt<<1|1]);
}

int main(){
	int h,w,n,x;
	while(~scanf("%d%d%d",&h,&w,&n)){
		if(h > n)
        h = n;
		build(1,h,1,w);
		for(int i = 1;i<=n;++i){
			scanf("%d",&x);
			if(sum[1] >= x) 
			fadd(1,h,1,x);
			else
			cout<<"-1\n";
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/dukig/article/details/89207982
今日推荐