题意:
给一个hw的格子,然后给出多个1w的板子往格子里面填,如果有空间尽量往上一行填满,输出行数,无法填补,则输出-1;
思路:
初始化将每一排的格子数目w放到每一个叶子节点上。每输入一个板子宽度wi,查询>wi的最小的下标(二分),并用线段树更新该点的剩余宽度。
这样写O(nlognlogn)
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid+1, r
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 200000+5;
using namespace std;
int h, w;
int Tree[maxn*4]; // 线段树
// 向上更新根节点
void push_up(int root){ Tree[root] = max(Tree[root<<1], Tree[root<<1 | 1]);}
// 线段树建树
void Stree_build(int root, int l, int r){
if(l == r){
Tree[root] = w;
//Tree[root] = a[cnt++];
//printf("cnt = %d, %d\n",cnt-1,a[cnt-1]);
return;
}
int mid = (l+r)>>1;
Stree_build(lson);
Stree_build(rson);
push_up(root);
}
// la、rb为需更新的区间左、右端点,l、r为当前区间左、右端点,root为当前l、r对应的根存储位置
void update(int la, int rb, int l, int r, int root, int val){
if(rb < l||la > r) return ; // 两区间无交集
if(la <= l&&rb >= r){ // [la,rb]包含[l,r]
Tree[root] -= val; // 这里是覆盖更新,也可以选择其他的更新操作
return ;
}
int mid = (l+r)>>1;
if(la <= mid)
update(la, rb, l, mid, root<<1, val);
if(rb > mid)
update(la, rb, mid+1, r, root<<1 | 1, val);
push_up(root);
}
//查询区间[la,rb]的值
void Query(int la, int rb, int l, int r, int root, int& ans){
if(rb < l||la > r) return; // 两区间无交集
if(la <= l&&rb >= r){ ans = max(ans, Tree[root]); return; } // [la,rb]包含[l,r]
int mid = (l+r) >> 1;
if(la <= mid)
Query(la, rb, l, mid, root<<1, ans);
if(rb > mid)
Query(la, rb, mid+1, r, root<<1 | 1, ans);
}
int Bsearch(int l, int r, int val){
int c;
while(l < r){
int mid = (l+r) >> 1;
c = 0;
Query(l, mid, 1, h, 1, c);
if(c < val) l = mid + 1;
else r = mid;
}
return l;
}
int main()
{
freopen("in.txt","r",stdin);
int n , x;
while(scanf("%d%d%d",&h,&w,&n) == 3){
if(h > n) h = n;
Stree_build(1, 1 ,h);
for(int i = 1; i <= n; ++i){
scanf("%d", &x);
if(Tree[1] < x) { puts("-1"); continue; }
int k = Bsearch(1, h, x);
printf("%d\n", k);
update(k, k, 1, h, 1, x);
}
}
fclose(stdin);
return 0;
}
还可以在查询的时候直接更新并就找到最早>=wi的下标,省去了二分查找和。O(nlogn)
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
#define lson root<<1, l, mid
#define rson root<<1 | 1, mid+1, r
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 200000+50;
using namespace std;
int h, w, Tree[maxn*4];
void push_up(int root){
Tree[root] = max(Tree[root<<1], Tree[root<<1 |1]);
}
void Stree_build(int root, int l, int r){
if(l == r){
Tree[root] = w;
return;
}
int mid = (l+r) >> 1;
Stree_build(lson);
Stree_build(rson);
push_up(root);
}
int update(int root , int l, int r,int val){
if(l == r){
Tree[root]-= val;
return l;
}
int mid = (l+r) >> 1, ans;
if(Tree[root<<1] >= val)
ans = update(lson, val);
else
ans = update(rson, val);
push_up(root);
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
int n , x;
while(scanf("%d%d%d",&h,&w,&n) == 3){
if(h > n) h = n;
Stree_build(1, 1 ,h);
for(int i = 1; i <= n; ++i){
scanf("%d", &x);
if(Tree[1] < x) {puts("-1"); continue;}
printf("%d\n", update(1, 1, h, x));
}
}
fclose(stdin);
return 0;
}