线段树------贴海报
来源:互联网 发布:split赋值给数组 编辑:程序博客网 时间:2024/05/17 04:33
今天做了一道看起来不像线段树的题,但是确实是。
题目大致如下:
贴海报,贴的时候往左上角贴,给出区域h*w,而海报的高度默认为1,给出宽度w,问能贴到的最高的位置在哪一行。
思路:
没想到这道题竟然可以用线段树解决。
要找到最靠上的海报只需要查询一次线段树就好了,从根节点往下走,如果左子树的可用的最大长度满足就往左走,否则往右走。
附上代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct
{
int l,r,val;
}Tree;
Tree tree[1000000];
int m;
void Build(int t,int l,int r)
{
tree[t].l=l;
tree[t].r=r;
tree[t].val=m;
if (l==r) return;
int mid=(l+r)/2;
Build(2*t+1,l,mid);
Build(2*t+2,mid+1,r);
}
int max(int a,int b)
{
if(a<b)
a=b;
return a;
}
int min(int a,int b)
{
if(a>b)
a=b;
return a;
}
void Find(int t,int val)
{
if (tree[t].l==tree[t].r)
{
tree[t].val-=val;
printf("%d\n",tree[t].l+1);
return;
}
if (tree[2*t+1].val>=val) Find(2*t+1,val);
else Find(2*t+2,val);
tree[t].val=max(tree[2*t+1].val,tree[2*t+2].val);
}
int main()
{
int i,j,n,x,T;
while(scanf("%d%d%d",&n,&m,&T)!=EOF)
{
n=min(n,200000);
Build(0,0,n-1);
while(T--)
{
scanf("%d",&x);
if (x>tree[0].val) printf("-1\n");
else Find(0,x);
}
}
return 0;
}
3 5 524333
1213-1
- 线段树;贴海报~
- 线段树------贴海报
- 线段树-hdu2795 Billboard(贴海报)
- poj2528(贴海报)线段树离散化
- POJ2528 海报问题(线段树+离散化)
- poj 2528/3277 线段树(海报覆盖)
- POJ 2528 Mayor's posters(离散化 线段树 贴海报)
- poj 2528 Mayor's posters(线段树 离散化 区间更新 贴海报)
- POJ 2528(市长的海报)线段树+离散化
- Billboard(海报粘贴&简单的线段树)
- poj2528 贴海报 区间树离散化
- poj 2528 贴海报
- poj2528 贴海报
- 海报
- haoi2014贴海报解题报告
- poj 2528(贴海报)矩形分割。
- 单点更新区间求和贴海报问题 hdu2795 billbord
- 海报详解
- JavaScript函数,递归
- 数组与矩阵---不包含本位置值的累乘数组
- Gym
- ACM-9月21日周四周中训练心得
- Android中对话框Dialog的创建
- 线段树------贴海报
- 392. Is Subsequence (greedy)
- jQuery单选框的回显
- 关于ehcache配置文件
- Apache的BeanUtils组件学习
- 2017/9/21训练日记
- django 外键操作
- HTTP常见状态码及表示意义
- DFS序+线段树