设为首页 加入收藏

TOP

HDU 2795 Billboard.(线段树)
2015-01-22 21:21:27 来源: 作者: 【 】 浏览:43
Tags:HDU 2795 Billboard. 线段

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2795


~~~~

开始学习数据结构,从简单的开始吧,刷题累了就写解题报告,哈哈,沸腾吧,Segment tree!

~~~~

大致题意:一块 h*w的广告牌,蓝后就是n块1*wi大小的广告,依次贴到广告牌上,而且总是贴到最左上角,问贴完所有广告后所占的高度是多少?

#include
  
   
#include
   
     #define lson rt<<1,s,mid #define rson rt<<1|1,mid+1,e using namespace std; const int maxn=222222; int tre[maxn<<2]; int h,w,n; void pushup(int rt) { tre[rt]=max(tre[rt<<1],tre[rt<<1|1]); } void build(int rt,int s,int e) { tre[rt]=w; if(s==e) return ; int mid=(s+e)>>1; build(lson); build(rson); } int query(int rt,int s,int e,int x) { if(s==e) { tre[rt]-=x; return s; } int mid=(s+e)>>1; int ans=(tre[rt<<1]>=x)?query(lson,x):query(rson,x); pushup(rt); //直接把更新操作写到query函数里。 return ans; } int main() { while(scanf("%d%d%d",&h,&w,&n)==3) { if(h>n) h=n; //h>n的话,多余的就空间就浪费掉了,而且貌似会RE build(1,1,h); while(n--) { int q; scanf("%d",&q); if(tre[1]
    
     

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇uva:10670 - Work Reduction(贪.. 下一篇对两个奇葩的C语言程序的思考

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: