hdu 2795 Billboard 线段树
2015-08-01 16:30
316 查看
题解看代码。
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2795
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2795
#include <cstdio> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 200005; struct Node { int x,y; int maxlen; //维护一个区间的最大长度; }t[MAXN<<2]; int h,w,n; void Push_Up(int rt) { t[rt].maxlen = max(t[rt<<1].maxlen,t[rt<<1|1].maxlen); } void Build(int x,int y ,int rt) { t[rt].x = x; t[rt].y = y; t[rt].maxlen = w; if(x == y) { return ; } int mid = (x + y) >> 1; Build(x,mid,rt<<1); Build(mid+1,y,rt<<1|1); //Push_Up(rt);可以不用; } int Query(int rt,int len) { //printf("%d %d %d\n",x,y,rt); if(t[rt].x == t[rt].y) { t[rt].maxlen -= len; return t[rt].x; } int ans = -1; if(t[rt<<1].maxlen >= len) { ans = Query(rt<<1,len); } else { ans = Query(rt<<1|1,len); } Push_Up(rt); return ans; } void Debug(int rt) { printf("x : %d y : %d maxlen : %d\n",t[rt].x,t[rt].y,t[rt].maxlen); if(t[rt].x==t[rt].y) return ; Debug(rt<<1); Debug(rt<<1|1); } void Deal_with() { while(~scanf("%d %d %d",&h,&w,&n)) { //printf("%d %d %d\n",h,w,n); h = min(h,n); //为什么min(h,n)不可以,min(h,n+1)就可以; //出现这种问题是因为Query()写丑了; //n = 1 的时候就有可能出问题。 Build(1,h,1); int len; for(int i = 1; i <= n ; i++) { scanf("%d",&len); if(t[1].maxlen >= len) printf("%d\n",Query(1,len)); else puts("-1"); //Debug(1); } } } int main(void) { //freopen("a.in","r",stdin); Deal_with(); return 0; } //h*w的木板,每次放下1*wi; //思路:从上往下找到第一个区域放下1*wi即可,长度减掉wi; //虽然h小于1e9,但是最多只有min(h,n)行;
相关文章推荐
- 计算满月的时间
- 8.属性和继承
- [Objective-C] 014_Objective-C 代码规范指南
- 34款Firefox渗透测试插件
- http各种状态码详解
- DesignPattern之Command
- 看病要排队
- 两种生成全排列序列的方法
- 深入理解Java:内省(Introspector)
- 关于mschart在显示百分比时需要注意的情况
- 黑马程序员——多线程2
- android开发常用基础操作
- 解压.tar.bz2文件
- boa服务器移植
- Best Time to Buy and Sell Stock
- MySQL中基本的用户和权限管理方法小结
- CSS 整理(3)
- Web service是什么?
- MATLAB 求图像的极大值极小值,平均值
- CImageList