您的位置:首页 > 其它

[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的范围。

线段树叶子节点维护公告板行的剩余宽度,尽可能地往左子树走。更新在查询的时候完成,不要忘记向上更新线段树。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: