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.
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.
#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; }