转自:https://www.kancloud.cn/digest/acarthur/166619
题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边
2 a b:将[a,a+b-1]的房间清空
思路:记录区间中最长的空房间
线段树操作:update:区间替换 query:询问满足条件的最左断点
//msum最大一段连续区间 lsum,rsum从左,右边起最大连续区间
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 55555;
int lsum[maxn << 2], rsum[maxn << 2], msum[maxn << 2];
int cover[maxn << 2];
void PushDown(int rt, int m) {
if (cover[rt] != -1) {
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];//标志下移
msum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = cover[rt] ? 0 : m - (m >> 1);
msum[rt << 1 | 1] = lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = cover[rt] ? 0 : (m >> 1);
cover[rt] = -1;
}
}
void PushUp(int rt, int m) {
lsum[rt] = lsum[rt << 1];
rsum[rt] = rsum[rt << 1 | 1];
//关键
if (lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt << 1 | 1];
if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1];
msum[rt] = max(lsum[rt << 1 | 1] + rsum[rt << 1], max(msum[rt << 1], msum[rt << 1 | 1]));
}
void build(int l, int r, int rt) {
msum[rt] = lsum[rt] = rsum[rt] = r - l + 1;
cover[rt] = -1;
if (l == r) return;
int m = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
msum[rt] = lsum[rt] = rsum[rt] = c ? 0 : r - l + 1;
cover[rt] = c;
return;
}
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
PushUp(rt, r - l + 1);
}
int query(int w, int l, int r, int rt) {
//要返回的是左坐标
if (l == r) return l;
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;//1.完全在左儿子2.部分在左儿子,部分在右儿子3.完全在右儿子
if (msum[rt << 1] >= w) return query(w, lson);
else if (rsum[rt << 1] + lsum[rt << 1 | 1] >= w) return m - rsum[rt << 1] + 1;//这一步return最为经典
return query(w, rson);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
build(1, n, 1);
while (m--) {
int op, a, b;
scanf("%d", &op);
if (op == 1) {
scanf("%d", &a);
if (msum[1] < a) puts("0");//利用根结点判断是否有大于b的
else {
int p = query(a, 1, n, 1);
printf("%d\n", p);
update(p, p + a - 1, 1, 1, n, 1);
}
}
else {
scanf("%d%d", &a, &b);
update(a, a + b - 1, 0, 1, n, 1);
}
}
return 0;
}