题目大意:还是一个旅馆里有N个房间,开始均为空。有两种操作:
(1)查询是否有d个连续空房间,有则输出序号最小的房间号,即最靠左的房间号。否则输出0
(2)清空从a房间开始的连续b个房间。
代码如下:
View Code
1 #include <stdio.h> 2 3 #define lson l,m,rt<<1 4 5 #define rson m+1,r,rt<<1|1 6 7 #define maxn 50000 8 9 struct node 10 11 { 12 13 int llen,rlen,maxlen,c; 14 15 }setree[maxn<<2]; 16 17 int max(int a,int b) 18 19 { 20 21 return a>b?a:b; 22 23 } 24 25 void build(int l,int r,int rt) 26 27 { 28 29 setree[rt].llen=setree[rt].rlen=setree[rt].maxlen=r-l+1; 30 31 setree[rt].c=-1; 32 33 if(l==r) 34 35 return; 36 37 int m=(l+r)>>1; 38 39 build(lson); 40 41 build(rson); 42 43 } 44 45 void pushdown(int rt,int m) 46 47 { 48 49 if(setree[rt].c!=-1){ 50 51 if(setree[rt].c==1){ 52 53 setree[rt<<1].llen=setree[rt<<1].rlen=setree[rt<<1].maxlen=0; 54 55 setree[rt<<1|1].llen=setree[rt<<1|1].rlen=setree[rt<<1|1].maxlen=0; 56 57 setree[rt<<1].c=setree[rt<<1|1].c=1; 58 59 } 60 61 else if(setree[rt].c==0){ 62 63 setree[rt<<1].llen=setree[rt<<1].rlen=setree[rt<<1].maxlen=m-m/2; 64 65 setree[rt<<1|1].llen=setree[rt<<1|1].rlen=setree[rt<<1|1].maxlen=m/2; 66 67 setree[rt<<1].c=setree[rt<<1|1].c=0; 68 69 } 70 71 setree[rt].c=-1; 72 73 } 74 75 } 76 77 void pushup(int rt,int m) 78 79 { 80 81 setree[rt].llen=setree[rt<<1].llen; 82 83 if(setree[rt<<1].llen==m-m/2) 84 85 setree[rt].llen+=setree[rt<<1|1].llen; 86 87 setree[rt].rlen=setree[rt<<1|1].rlen; 88 89 if(setree[rt<<1|1].rlen==m/2) 90 91 setree[rt].rlen+=setree[rt<<1].rlen; 92 93 setree[rt].maxlen=max(setree[rt<<1].rlen+setree[rt<<1|1].llen,max(setree[rt<<1].maxlen,setree[rt<<1|1].maxlen)); 94 95 } 96 97 void update(int l,int r,int rt,int L,int R,int c) 98 99 { 100 101 if(L<=l&&r<=R){ 102 103 setree[rt].llen=setree[rt].rlen=setree[rt].maxlen=c?0:r-l+1; 104 105 setree[rt].c=c?1:0; 106 107 return; 108 109 } 110 111 pushdown(rt,r-l+1); 112 113 int m=(l+r)>>1; 114 115 if(L<=m) 116 117 update(lson,L,R,c); 118 119 if(R>m) 120 121 update(rson,L,R,c); 122 123 pushup(rt,r-l+1); 124 125 } 126 127 int query(int l,int r,int rt,int num) 128 129 { 130 131 if(l==r) 132 133 return l; 134 135 pushdown(rt,r-l+1); 136 137 int m=(l+r)>>1; 138 139 if(setree[rt<<1].maxlen>=num) 140 141 return query(lson,num); 142 143 if(setree[rt<<1].rlen+setree[rt<<1|1].llen>=num) 144 145 return m-setree[rt<<1].rlen+1; 146 147 return query(rson,num); 148 149 } 150 151 int main() 152 153 { 154 155 int n,m; 156 157 scanf("%d%d",&n,&m); 158 159 build(1,n,1); 160 161 while(m--){ 162 163 int op,a,b; 164 165 scanf("%d",&op); 166 167 if(op==1){ 168 169 scanf("%d",&a); 170 171 if(a>setree[1].maxlen){ 172 173 printf("0\n"); 174 175 continue; 176 177 } 178 179 int temp=query(1,n,1,a); 180 181 printf("%d\n",temp); 182 183 update(1,n,1,temp,temp+a-1,1); 184 185 } 186 187 else if(op==2){ 188 189 scanf("%d%d",&a,&b); 190 191 update(1,n,1,a,a+b-1,0); 192 193 } 194 195 } 196 197 }