题意:

有一个H*W的广告牌,当插入一个广告时(1*Wi),问最靠前的插入方式是什么

新生赛有个类似的题目,可惜当时居然没水过去。

果断用线段树做 以H为线段 建树,存[l,r]中最大的宽度,因为区间最大值满足区间和性质。

所以线段树几个要素如下:

线段:H

区间和性质:最大值

代码:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 222222
using namespace std;
int h,w,n;
int tree[maxn*4];
int A[maxn];
void PushUp(int rt)
{
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int build(int l,int r,int rt)
{
if(l==r) {tree[rt]=w;return 0;}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
} void input()
{
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
}
int updata(int p,int k,int l,int r,int rt)
{
int m;
if(l==r) {tree[rt]+=k;return 0;}
m=(l+r)>>1;
if(p<=m) updata(p,k,lson);
else updata(p,k,rson);
PushUp(rt);
}
int query(int p,int l,int r,int rt)
{
if(p>tree[rt]) return -1;
if(l==r) return l;
int m=(l+r)>>1;
if(p<=tree[rt<<1]) return query(p,lson);
else return query(p,rson);
}
void solve()
{
for(int i=1;i<=n;i++)
{
int t=query(A[i],1,h,1);
printf("%d\n",t);
if(t!=-1)
{
updata(t,-A[i],1,h,1);
}
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
// init();
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
memset(tree,0,sizeof(tree));
h=min(h,200000);
build(1,h,1);
input();
solve();
}
return 0;
}

【线段树求最靠前】【HDU2795】【Billboard】的更多相关文章

  1. xdoj-1324 (区间离散化-线段树求区间最值)

    思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i]  覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...

  2. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  3. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

  4. BNU 2418 Ultra-QuickSort (线段树求逆序对)

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...

  5. hdu 1394 (线段树求逆序数)

    <题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...

  6. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

  7. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  8. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  9. HDU_1394_Minimum Inversion Number_线段树求逆序数

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. SVN:冲突解决 合并别人的修改

    在项目中,基本不可避免多个人同时参与一个项目,因此就可能会出现多个人同时修改一个文件的情况,就不可避免的会出现冲突.svn已经很聪明了,如 果你和别人对于同一个文件的修改之间不存在重叠(比如你在文件最 ...

  2. asp.net 错误处理

    一.从客户端(...)中检测到有潜在危险的 Request.Form 值.(如图) 解决办法: 1.为 c:/windows/temp 文件夹 设置 IIS_Iusers 可读写权限 (可解决部分问题 ...

  3. poj 3894 System Engineer (二分图最大匹配--匈牙利算法)

    System Engineer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 507   Accepted: 217 Des ...

  4. Web App 聊天样式

    意见反馈: @using CommonDB.EF @model IEnumerable<Pub_ChatLog> @{ ViewBag.Title = "意见反馈"; ...

  5. 简单的FTP上传下载(java实现 swing界面)

    /** *阅读前请自己在win7上建立FTP主机 *具体步骤如:http://jingyan.baidu.com/article/574c5219d466c36c8d9dc138.html * 然后将 ...

  6. bootstrap之 formgroup表单布局样式

    <form class="form-horizontal" role="form"> <fieldset> <legend> ...

  7. css模板之 web模板一

    效果 <html><head><style type="text/css">div.container{width:100%;margin:0p ...

  8. G - Bullseye

    Description A simple dartboard consists of a flat, circular piece of cork with concentric rings draw ...

  9. html中的表格 bootstrap-table

    1 bootstrap-table 官网: http://bootstrap-table.wenzhixin.net.cn/ 2

  10. AngularJS如何使用ngRepeat过滤排序

    NG重复指令,带过滤器,像这样: <li ng-repeat="item in items | orderBy:'order_prop' | filter:query | limitT ...