您的位置:首页 > 其它

hdu_2795_线段树入门_线段树由树状数组转坑原本数组模拟

2017-09-12 19:59 309 查看
**Billboard

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


mean:
给你一个billboard 告诉你长宽 h,w;
然后有人贴广告优先贴贴billboard 的最高靠右的地方;
每个广告的长度都是1 告诉你宽,求贴在第几层;
最高处为第一层;
answer:
维护区间最值单点更新优先查询左子数的版子题


首先线段树是可以进行

1.区间求和,

2.区间最值,

3.区间修改;

但是它快在哪里,最朴素的是n*n;

但是把可以对区间进行划分每个区间维护一个最值,这样的话对区间划分采取二分操作 复杂度就是log2(N);

对区间的划分



这个才是真正要存东西的结构



树状数组的局限性:

树状数组是利用二进制特点对区间进行压缩,数组模拟二叉树就是对点压缩,所以在区间是很大的时候树状数组是无法用的同理点特别多时朴素的数组模拟也是不行的:

假设有a数组第二图的标号就是a数组的标号;

每个a[i]都对应第一幅图的区间最值之类;
第二幅图 最底层才是代表区间每个单位的价值


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

#define L l,m,u<<1
#define R m+1,r,u<<1|1  //u*2+1

const in
4000
t N=200000+10;
int h,w,n;
int a[N<<2];

void build(int l,int r,int u)//u为根结点
{
a[u]=w;
if(l==r)
return;
int m=(l+r)>>1;
build(L);
build(R);
}

int query(int x,int l,int r,int u)
{
if(l==r)
{
a[u]-=x;
return l;
}
int m=(l+r)>>1;
int res=(a[u<<1]>=x)?query(x,L):query(x,R);
a[u]=max(a[u<<1],a[u<<1|1]);
return res;
}

int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);
while (~scanf("%d%d%d",&h,&w,&n))
{
if(h>n)
h=n;
build(1,h,1);
while(n--)
{
int x;
scanf("%d",&x);
if(a[1]<x)
printf("-1\n");
else
printf("%d\n",query(x,1,h,1));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: