题意:有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作。1 w 表示在总区间内查询一个长度为w的可用区间,并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0 。初始时所有区间都可用,比如当n=10 , 1 3 查到的最左的长度为3的可用区间就是[1,3],返回1,并且该区间被占用了。2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出。
思路:线段树。参考http://www.cnblogs.com/scau20110726/archive/2013/05/07/30618.html
既然是要找一段长度至少为W的区间,要做到这点,其实不难,我们可以在每个线段树的节点里增加一个域tlen,表示该区间可用的区间的最大长度,
至于这个tlen区间的具体位置在哪里不知道,只是知道该区间内存在这么一段可用的区间,并且注意,这个tlen表示的是最大长度,该节点可能有多段可用的区间,但是最长的长度是tlen
记录了这个信息,至少能解决一个问题,就是能不能找到一个合适的区间。如果查询的区间长度W > 总区间的tlen,那么查询一定是失败的(总区间中可以的最大区间都不能满足那就肯定失败)
但这远远不够,其一查询是要返回区间的具体位置的,这里无法返回位置,另外是要查询最左区间,最左的且满足>=W的区间可能不是这个tlen区间
那么我们进一步思考这个问题
首先我们先增加两个域,llen,rlen
llen表示一个区间从最左端开始可用的且连续的最大长度
例如区间[1,5],覆盖情况为[0,0,0,1,1],llen = 3,从最左端有3格可以利用
区间[1,5],覆盖情况为[1,0,0,0,0],llen = 0,因为从最左端开始找不到1格可用的区间
rlen表示一个区间从最右端开始可用的且连续的最大长度
例如区间[1,5],覆盖情况为[1,0,1,0,0],rlen = 2,从最右端有2格可以利用
区间[1,5],覆盖情况为[0,0,0,0,1],rlen = 0,因为从最右端开始找不到1格可用的区间
对于一个区间,我们知道它左半区间的tlen,和右半区间的tlen,如果左半区间的tlen >= W ,那么我们一定能在左边找到(满足最左),所以可以深入到左半区间去确定该区间的具体位置
如果左端的不满足,那么我们要先考虑横跨两边的区间(因为要满足最左),因而记录的llen,rlen可以派上用场,一段横跨的区间,
那么是 左边区间rrlen + 右边区间llen ,如果满足的话,就是该区间了,它的位置也是可以确定的
如果横跨的区间不满足,那么就在右半区间找,如果右半区间的tlen >= W , 那么可以在右半区间找到,所以深入到右半区间去确定它的具体位置,否则的话,整个查询就失败了
可见查询是建立在tlen,llen,rlen这个信息之上的,而每次查询后其实伴随着修改,而且还有专门的修改操作,这些修改操作都会改变tlen,llen,rlen的值,所以在更新的时候是时刻维护这些信息
关于这3个信息的维护
当前区间的tlen = max{ 左半区间tlen , 右半区间tlen , 左半区间rlen+右半区间llen} (这个不难理解吧,取左右较大的那个,或者横跨中间的那个)
如果左半区间全部可以用: 当前区间llen = 左半区间llen(tlen) + 右半区间llen
左半区间部分能用: 当前区间llen = 左半区间llen
如果右半区间全部能用: 当前区间rlen = 右半区间rlen(tlen) + 左半区间rlen
右半区间部分能用: 当前区间rlen = 右半区间rlen
这样就全部维护好了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdlib>
using namespace std;
#define INF 0x3fffffff
#define clc(s,t) memset(s,t,sizeof(s))
#define N 50005
struct node{
int l,r;
int mlen,ml,mr,flag;
}t[N<<4];
int n,m;
int mid(int x,int y){
return (x+y)>>1;
}
void create(int root,int l,int r){
t[root].l = l;
t[root].r = r;
t[root].mlen = r-l+1;
t[root].ml = r-l+1;
t[root].mr = r-l+1;
t[root].flag = 0;
if(l != r){
create(root*2,l,mid(l,r));
create(root*2+1,mid(l,r)+1,r);
}
}
void update(int root,int l,int r,int f){//f为1表示区间置为可用,为0表示占用
int mm = mid(t[root].l,t[root].r);
if(t[root].l == l && t[root].r == r){
if(f)
t[root].mlen = t[root].ml = t[root].mr = r-l+1;
else
t[root].mlen = t[root].ml = t[root].mr = 0;
t[root].flag = 1;//lazy标志,不更新到叶子
return ;
}
if(t[root].flag){
if(t[root].mlen){//表示这整个区间是可用的
t[root*2].mlen = t[root*2].ml = t[root*2].mr = t[root*2].r-t[root*2].l+1;
t[root*2+1].mlen = t[root*2+1].ml = t[root*2+1].mr = t[root*2+1].r-t[root*2+1].l+1;
t[root*2].flag = 1;
t[root*2+1].flag = 1;
}else{
t[root*2].mlen = t[root*2].ml = t[root*2].mr = 0;
t[root*2+1].mlen = t[root*2+1].ml = t[root*2+1].mr = 0;
t[root*2].flag = 1;
t[root*2+1].flag = 1;
}
}
t[root].flag = 0;
if(r<=mm)
update(root*2,l,r,f);
else if(l>mm)
update(root*2+1,l,r,f);
else{
update(root*2,l,mm,f);
update(root*2+1,mm+1,r,f);
}
t[root].mlen = max(max(t[root*2].mlen,t[root*2+1].mlen),t[root*2].mr+t[root*2+1].ml);
t[root].ml = t[root*2].ml + ((t[root*2].r-t[root*2].l+1)==t[root*2].mlen?t[root*2+1].ml:0);
t[root].mr = t[root*2+1].mr + ((t[root*2+1].r-t[root*2+1].l+1)==t[root*2+1].mlen?t[root*2].mr:0);
}
int query(int root,int x){
int mm = mid(t[root].l,t[root].r);
if(t[root].mlen < x)//最长区间都放不下,定然无解
return 0;
if(t[root].ml >= x)//左端开始能放下,那么就放咯
return t[root].l;
else if(t[root*2].mlen>=x)//去左端里找
return query(root*2,x);
else if(t[root*2].mr + t[root*2+1].ml >= x)//只在左一半区间里放不下,那么如果跨区间能够放下,那么就放
return mm-t[root*2].mr+1;
else if(t[root*2+1].mlen >= x)//都不行,有一半必须能够放下啦
return query(root*2+1,x);
return 0;
}
int main(){
while(scanf("%d %d",&n,&m) != EOF){
int a,b,cmd;
create(1,1,n);
while(m--){
scanf("%d",&cmd);
if(cmd == 1){
scanf("%d",&a);
b = query(1,a);
printf("%d\n",b);
if(b)
update(1,b,b+a-1,0);
}else{
scanf("%d %d",&a,&b);
update(1,a,a+b-1,1);
}
}
}
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务