HDU 2795 Billboard (线段树,单点查询)
来源:互联网 发布:万能淘宝网商城 编辑:程序博客网 时间:2024/06/05 13:33
HDU 2795
一个h*w的公告牌,要在其上贴公告,尽量往上,同一高度尽量靠左。求第n个广告所在的行数。没有合适的位置贴了则输出-1。
参考博客:http://blog.csdn.net/qiqi_skystar/article/details/50299743
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node {int l, r, m;int val;};int h, w, n;node T[200100 << 2];void build(int rt, int l, int r) {T[rt].val = w;T[rt].l = l;T[rt].r = r;T[rt].m = (l + r) >> 1;if(l == r) return;build(rt << 1, l, T[rt].m);build(rt << 1 | 1, T[rt].m + 1, r);}void query(int rt, int x) {if(T[rt].l == T[rt].r) {printf("%d\n", T[rt].l);T[rt].val -= x;return;}if(x <= T[rt << 1].val) {query(rt << 1, x);}else {query(rt << 1 | 1, x);}T[rt].val = max(T[rt << 1].val, T[rt << 1 | 1].val);}int main() { while(~scanf("%d %d %d", &h, &w, &n)) { if(h > n) h = n; build(1, 1, h); while(n--) { int x; scanf("%d", &x); if(x > T[1].val) { printf("-1\n");}else {query(1, x);}}} return 0;}
#include <bits/stdc++.h>using namespace std;struct node{int l, r, m;int val;};const int maxn = 200100;int h, w, n, ans;node T[maxn << 2];int a[maxn];void pushup(int rt) {T[rt].val = max(T[rt << 1].val, T[rt << 1 | 1].val);}void build(int begin, int end, int rt) {T[rt].l = begin;T[rt].r = end;T[rt].m = (T[rt].l + T[rt].r) >> 1;if(begin == end) {T[rt].val = w;return ;}build(T[rt].l, T[rt].m, rt << 1);build(T[rt].m + 1, T[rt].r, rt << 1 | 1);pushup(rt);}void update(int add, int rt) {if(T[rt].l == T[rt].r) {T[rt].val -= add;printf("%d\n", T[rt].l);return ;}if(add <= T[rt << 1].val) update(add, rt << 1);else update(add, rt << 1 | 1);pushup(rt);}int main() {while(~scanf("%d %d %d", &h, &w, &n)) {if(h > n) h = n; //解题关键 build(1, h, 1);int l;for(int i = 0; i < n; i++) {scanf("%d", &l);if(T[1].val < l) {puts("-1");continue;}update(l, 1);}}return 0;}
0 0
- HDU 2795 Billboard(线段树,单点查询)
- HDU 2795 Billboard (线段树,单点查询)
- 线段树 单点更新查询 区间最大值 hdu 2795 Billboard
- HDU 2795 Billboard(线段树区间最值查询和单点更新)
- HDU 2795 Billboard [线段树-单点更新]
- hdu 2795 Billboard 线段树 单点更新
- [HDU 2795] Billboard[线段树][单点增删]
- HDU 2795 Billboard / 线段树单点更新
- HDU-2795 Billboard 线段树单点更新
- hdu 2795 Billboard 线段树 单点更新
- hdu 2795 Billboard (线段树,单点更新)
- HDU-2795-Billboard-线段树单点更新
- HDU 2795 Billboard // 线段树 单点更新
- HDU 2795 Billboard 线段树单点更新
- hdu-2795-Billboard- 线段树-单点
- HDU 2795 Billboard 线段树单点更新
- HDU 2795 Billboard (线段树单点更新)
- HDU 2795 Billboard (线段树单点更新)
- 决策森林和卷积神经网络二道归一
- Python3基本数据类型
- 解密QQ
- 第三方框架FMDB使用
- Thread详解3:线程的状态及几个简单方法
- HDU 2795 Billboard (线段树,单点查询)
- Spring学习——(二)IOC、DI以及常用xml配置
- 浅析java中的关键字:final和static
- 以太网,IP,TCP,UDP数据包分析
- linxu系统引导&运行级别&chkconfig&/etc/rc/init.d&/etc/rc/rc*.d
- 全排列
- Spark实战-Spark SQL(二)
- 访问者模式
- error C2899: typename cannot be used outside a template declaration