[HDOJ2795]Billboard(线段树,单点更新)
2016-05-13 21:09
507 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高,问每个公告最高贴多高。不能贴就输出-1。特别“不”需要注意的是,题目给的n数据范围很大,但是n只有200000。所以说可以不用关心h的范围。
线段树叶子节点维护公告板行的剩余宽度,尽可能地往左子树走。更新在查询的时候完成,不要忘记向上更新线段树。
题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高,问每个公告最高贴多高。不能贴就输出-1。特别“不”需要注意的是,题目给的n数据范围很大,但是n只有200000。所以说可以不用关心h的范围。
线段树叶子节点维护公告板行的剩余宽度,尽可能地往左子树走。更新在查询的时候完成,不要忘记向上更新线段树。
#include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> using namespace std; #define fr first #define sc second #define pb(a) push_back(a) #define Rint(a) scanf("%d", &a) #define Rll(a) scanf("%I64d", &a) #define Rs(a) scanf("%s", a) #define Fread() freopen("in", "r", stdin) #define Fwrite() freopen("out", "w", stdout) #define Rep(i, n) for(int i = 0; i < (n); i++) #define For(i, a, n) for(int i = (a); i < (n); i++) #define Cls(a) memset((a), 0, sizeof(a)) #define Full(a) memset((a), 0w7f7f, sizeof(a)) #define lrt rt << 1 #define rrt rt << 1 | 1 const int mawn = 2222221; int sum[mawn<<1]; int w, h, n, t; void pushUP(int rt) { sum[rt] = max(sum[lrt], sum[rrt]); } void build(int l, int r, int rt) { sum[rt] = w; if(l == r) return; int m = (l + r) >> 1; build(l, m ,lrt); build(m+1, r, rrt); // pushUP(rt); } void update(int p, int w, int l, int r, int rt) { if(l == r) { sum[rt] -= w; return; } int m = (l + r) >> 1; if(p <= m) update(p, w, l, m, lrt); else update(p, w, m+1, r, rrt); pushUP(rt); } int query(int l, int r, int rt, int w) { if(l == r) { sum[rt] -= w; // update(l, w, 1, n, 1); return l; } int m = (l + r) >> 1; int ret = sum[rt<<1] >= w ? query(l, m, lrt, w) : query(m+1, r, rrt, w); pushUP(rt); return ret; } int main() { // Fread(); while(~scanf("%d%d%d", &h, &w, &n)) { h = min(h, n); build(1, h, 1); while(n--) { Rint(t); if(sum[1] < t) printf("-1\n"); else printf("%d\n", query(1, h, 1, t)); } } return 0; }
相关文章推荐
- git status显示中文
- MSSQL之二 Sql Server中管理库与表
- MSSQL之二 Sql Server中管理库与表
- Android SpannableString浅析
- Java数组最值,以最小值为例
- LintCode 落单的数
- HDU 2594 Simpsons’Hidden Talents(KMP)
- MSSQL之二 Sql Server中管理库与表
- 找素数程序
- 重载赋值运算符
- Problem3-1003
- DOS命令下netstat -an输出信息的解释
- Android天气开源项目之添加城市
- Android中ListView添加头部
- 2013年第四届蓝桥杯C/C++程序设计本科B组决赛 农场阳光(编程大题)
- 十二、 Bmob免费移动后方服务,你不用再搭php+MySQL移动后端 Android studio+Bmob后台服务feedback简单实现
- HDU jrmz and angles
- UIWindow简单介绍
- 将自己得到的数据从txt文档中读入,生成pcd文件
- php的打印sql语句的方法