hdu 2795 线段树

时间:2019-11-20
本文章向大家介绍hdu 2795 线段树,主要包括hdu 2795 线段树使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.

Input
There are multiple cases (no more than 40 cases).
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
 
Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
 
Sample Input
3 5 5
2
4
3
3
3
 
Sample Output
1
2
1
3
-1
思路:线段树维护区间剩余空间最大值,并且每次先判断左子树保证左子树先满。
   该题有一个小坑 h,n的范围1e9  但是当h>n的时候 无实际意义了,即可h=n  缩小数组开销
#include <iostream>  
using namespace std;  
const int N = 1e6+10;  
const int maxh = 4*N;     
struct node      
{      
    int l;      //左边界     
    int r;      // 右边界     
    int val, lazy;    //节点所维护的区间[l, r]的权值, 懒惰标记      
}t[maxh];//N为总节点数      
//int val[maxh]; //原数组  
int h,w,n;
void pushup(int n)  
{  
    t[n].val = max(t[2*n].val,t[2*n+1].val);    //总区间和 = 左区间和+右区间和   
}  
void build(int n,int l,int r)  
{  
    t[n].l = l;     //记录维护的原数组的左端点   
    t[n].r = r;     //记录维护的右端点   
    t[n].lazy = 0;  //标记下懒惰数组   
    if(l==r){       //l==r,表示叶子节点,  
        t[n].val = w;    //因为l==r,那么这个节点维护的值就是原数组val[l]的值   
        return;  
    }         
    int mid = (l+r)>>1;  
    build(n*2,l,mid);      //递归建左子树   
    build(n*2+1,mid+1,r);  //递归建左子树  
    pushup(n);             //求该点的权值   
}  
void updateOne(int n,int C)  
{  
    int l = t[n].l;//左端点   
    int r = t[n].r;//右端点   
    if(l==r)    //l==r,到了叶子节点   
    {  
		printf("%d\n",l);
		t[n].val -= C;    //更新权值  
        return;   
    }     
    
    if( t[2*n].val>=C )     //val[idx]由左节点维护   
        updateOne(n*2,C);  
    else          //val[idx]由右节点维护   
        updateOne(n*2+1,C);  
    pushup(n);    //向上更新   
}   
int main()  
{  
	while(~scanf("%d%d%d",&h,&w,&n)){
		if(h>n)   //小坑 
		 h=n;
		build(1,1,h);
		int ww;
		for(int i=1;i<=n;i++){
			scanf("%d",&ww);
			//cout<<" "<<t[1].val<<endl;
			if(t[1].val>=ww)
				updateOne(1,ww);
			else 
				puts("-1");
		}
	}
    return 0;   
}  

  

原文地址:https://www.cnblogs.com/lyj1/p/11897068.html