Poj 3667

题目链接:

http://poj.org/problem?id=3667

题意:

有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作

1 w 表示在总区间内查询一个长度为w的可用区间,并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0

2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出

题解:

线段树– 区间更新 区间合并 区间查询
lazy为-1表示没有更新到这个区间,所以不用pushdown; 为0表示这个区间没有被占用,所以传下去的长度就是当前区间的一半,左右各一半; 为1表示这个区间被占用了,给儿子区间的长度传成0就好了。
注意每次更新都要pushdown的是当前区间的长度

http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 写的挺好
http://www.cnblogs.com/yewei/archive/2012/05/05/2484471.html 参考代码

代码:

  1 #include <stdio.h>
  2 #include <algorithm>
  3 #include <cstring>
  4 using namespace std;
  5 typedef long long ll;
  6 #define MS(a) memset(a,0,sizeof(a))
  7 #define MP make_pair
  8 #define PB push_back
  9 const int INF = 0x3f3f3f3f;
 10 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 11 inline ll read(){
 12     ll x=0,f=1;char ch=getchar();
 13     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 14     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 15     return x*f;
 16 }
 17 //////////////////////////////////////////////////////////////////////////
 18 const int maxn = 1e5+10;
 19 
 20 struct node{
 21     int l,r,lm,rm,mm,len,lazy;
 22     void update(int val,int len){
 23         lazy = val;
 24         if(val) lm=rm=mm = 0;
 25         else lm=rm=mm = len;
 26     }
 27 }tree[maxn<<2];
 28 
 29 void pushup(int rt){
 30     tree[rt].lm = tree[rt<<1].lm;
 31     tree[rt].rm = tree[rt<<1|1].rm;
 32     tree[rt].mm = max(max(tree[rt<<1].mm,tree[rt<<1|1].mm),tree[rt<<1].rm+tree[rt<<1|1].lm);
 33     if(tree[rt<<1].lm == tree[rt<<1].len) tree[rt].lm = tree[rt<<1].lm + tree[rt<<1|1].lm;
 34     if(tree[rt<<1|1].rm == tree[rt<<1|1].len) tree[rt].rm = tree[rt<<1].rm+tree[rt<<1|1].rm;
 35 }
 36 
 37 void pushdown(int rt, int len){
 38     int lazyval = tree[rt].lazy;
 39     if(lazyval != -1){
 40         tree[rt<<1].lazy = tree[rt<<1|1].lazy = tree[rt].lazy;
 41         if(lazyval) {
 42             tree[rt<<1].lm=tree[rt<<1].rm=tree[rt<<1].mm = 0;
 43             tree[rt<<1|1].lm=tree[rt<<1|1].rm=tree[rt<<1|1].mm = 0;
 44         }else{
 45             tree[rt<<1].lm = tree[rt<<1].rm = tree[rt<<1].mm = len-(len/2);
 46             tree[rt<<1|1].lm = tree[rt<<1|1].rm = tree[rt<<1|1].mm = len/2;
 47         }
 48         tree[rt].lazy = -1;
 49     }
 50 }
 51 
 52 void build(int rt,int l,int r){
 53     tree[rt].l = l, tree[rt].r = r;
 54     tree[rt].lm = tree[rt].rm = tree[rt].mm = tree[rt].len = r-l+1;
 55     tree[rt].lazy = -1;
 56     if(l != r){
 57         int mid = (l+r)/2;
 58         build(rt<<1,l,mid);
 59         build(rt<<1|1,mid+1,r);
 60         pushup(rt);
 61     }
 62 }
 63 
 64 void update(int l,int r,int val,int rt){
 65     int L = tree[rt].l, R = tree[rt].r;
 66     if(l<=L && R<=r){
 67         tree[rt].update(val,R-L+1);
 68         return ;
 69     }
 70     pushdown(rt,R-L+1);
 71     int mid = (L+R)/2;
 72     if(l<=mid) update(l,r,val,rt<<1);
 73     if(r>mid) update(l,r,val,rt<<1|1);
 74     pushup(rt);
 75 }
 76 
 77 ll query(int rt,int len){
 78     int L = tree[rt].l, R = tree[rt].r;
 79     if(L==R)
 80         return L;
 81     pushdown(rt,R-L+1);
 82     if(tree[rt<<1].mm >= len)
 83         return query(rt<<1,len);
 84     else if(tree[rt<<1].rm+tree[rt<<1|1].lm >= len)
 85         return tree[rt<<1].r-tree[rt<<1].rm+1;
 86     else if(tree[rt<<1|1].mm >= len)
 87         return query(rt<<1|1,len);
 88     else
 89         return 0;
 90 }
 91 
 92 int main(){
 93      int n = read(), m = read();
 94      build(1,1,n);
 95      while(m--){
 96         int op = read();
 97         if(op == 1){
 98             int x = read();
 99             int ans = query(1,x);
100             printf("%d
",ans);
101             if(ans)
102                 update(ans,ans+x-1,1,1);
103         }else{
104             int x=read(),len=read();
105             update(x,x+len-1,0,1);
106         }
107      }
108 
109     return 0;
110 }
原文地址:https://www.cnblogs.com/yxg123123/p/6827653.html