hdoj 2795 Billboard 【线段树】

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define P(x) (x>>1)
#define PAUSE system("pause)
using namespace std;
#define MAXN 200005
int s[MAXN<<2];
int w, h;
void build(int l, int r, int p)
{
	s[p] = w;
	if(l == r)	return;
	int m = (l+r) >> 1;
	build(l, m, L(p));
	build(m+1, r, R(p));
}
int updata(int l, int r, int v, int p)
{
	if(s[p] < v)	return -1;
	if(l == r)	{	s[p] -= v;	return l;	}
	int m = (l+r)>>1;
	int ans;
	if(s[L(p)] >= v) ans =  updata(l, m, v, L(p));
	else if(s[R(p)] >= v) ans = updata(m+1, r, v, R(p));
	s[p] = max(s[L(p)], s[R(p)]);
	return ans;
}

int main()
{
	int n, t, m;
	while(scanf("%d%d%d", &h, &w, &n) != EOF)
	{
		m = min(n, h);
		build(1, m, 1);
		for(int i=1; i<=n; i++)
		{
			scanf("%d", &t);
			printf("%d\n", updata(1, m, t, 1));
		}
	}
	return 0;
}


你可能感兴趣的:(HDOJ,build,include,system)