3667

1 // include file
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cmath>
6 #include <cctype>
7 #include <ctime>
8
9 #include <iostream>
10 #include <sstream>
11 #include <fstream>
12 #include <iomanip>
13 #include <bitset>
14 #include <strstream>
15
16 #include <algorithm>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <set>
21 #include <list>
22 #include <functional>
23
24 using namespace std;
25
26 // typedef
27 typedef long long LL;
28 typedef unsigned long long ULL;
29
30 //
31 #define read freopen("in.txt","r",stdin)
32 #define write freopen("out.txt","w",stdout)
33 #define FOR(i,a,b) for(int i=(a);i<(b);i++)
34 #define FF(i,a) for(int i=0;i<(a);i+++)
35 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
36 #define Z(a) (a<<1)
37 #define Y(a) (a>>1)
38
39 const double eps = 1e-11;
40 const double Pi = acos(-1.0);
41
42 template<class T> inline T sqr(T a){return a*a;}
43 template<class T> inline T TMAX(T x,T y)
44 {
45 if(x>y) return x;
46 return y;
47 }
48 template<class T> inline T MMAX(T x,T y,T z)
49 {
50 return TMAX(TMAX(x,y),z);
51 }
52
53 // code begin
54
55 #define MAXN 50005
56
57 struct segTree
58 {
59 int left;
60 int right;
61 int lmax;
62 int rmax;
63 int mmax;
64 int state;
65
66 int mid()
67 {
68 return Y(left+right);
69 }
70 int len()
71 {
72 return right-left+1;
73 }
74 void Set(int f) //只设置全0,全1
75 {
76 if(f==1) //全满
77 {
78 state = 1;
79 lmax = rmax = mmax = 0;
80 }
81 else if(f==0)
82 {
83 state = 0;
84 lmax = rmax = mmax = len();
85 }
86 }
87 };
88
89 segTree mem[MAXN<<2];
90
91 void CreateTree(int l,int r,int dx=1)
92 {
93 mem[dx].left = l;
94 mem[dx].right = r;
95 mem[dx].Set(0);
96 if(l==r) return;
97 CreateTree( l,mem[dx].mid(),Z(dx));
98 CreateTree( mem[dx].mid()+1,r,Z(dx)+1);
99 }
100
101 int Query(int len,int dx=1)
102 {
103 if(mem[dx].state==0 && mem[dx].mmax>=len)
104 {
105 return mem[dx].left;
106 }
107 if(mem[dx].mmax<len) return 0;
108 int sz = Z(dx),sy = Z(dx)+1,mid=mem[dx].mid();
109 if( mem[sz].mmax>=len) return Query(len,sz);
110 else if( mem[sz].rmax+mem[sy].lmax>=len) return mid-mem[sz].rmax+1;
111 else if( mem[sy].mmax>=len) return Query(len,sy);
112
113 return 0;
114 }
115
116 void Update(int l,int r,int flag,int dx=1)
117 {
118 if( mem[dx].left==l && mem[dx].right==r)
119 {
120 mem[dx].Set(flag);
121 return;
122 }
123
124 // 如果不是刚好就要状态往下压
125 int sz = Z(dx), sy = Z(dx)+1,mid = mem[dx].mid();
126 if( mem[dx].state!=-1)
127 {
128 mem[sz].Set( mem[dx].state );
129 mem[sy].Set( mem[dx].state );
130 }
131
132 // 然后更新孩子
133 if( r <= mid )
134 {
135 Update(l,r,flag,sz);
136 }
137 else if( l>mid) Update(l,r,flag,sy);
138 else
139 {
140 Update( l,mid,flag,sz);
141 Update( mid+1,r,flag,sy);
142 }
143
144 // 更新状态
145 if( mem[sz].state==0 && mem[sy].state==0 ) mem[dx].state = 0;
146 else if( mem[sz].state ==1 && mem[sy].state==1) mem[dx].state = 1;
147 else mem[dx].state = -1;
148
149 // 更新数据
150 mem[dx].mmax = MMAX( mem[sz].mmax,mem[sy].mmax, mem[sz].rmax+mem[sy].lmax );
151 mem[dx].lmax = mem[sz].lmax + ( mem[sz].mmax==mem[sz].len()?mem[sy].lmax:0 );
152 mem[dx].rmax = mem[sy].rmax + ( mem[sy].mmax==mem[sy].len()?mem[sz].rmax:0 );
153 }
154
155 int main()
156 {
157 int N,M,cmd,len,st;
158 while(scanf("%d %d",&N,&M)!=-1)
159 {
160 CreateTree(1,N);
161 while(M--)
162 {
163 scanf("%d",&cmd);
164 if(cmd==1)
165 {//in
166 scanf("%d",&len);
167 st = Query(len);
168 printf("%d\n",st);
169 if(st)
170 {
171 Update(st,st+len-1,1);
172 }
173 }
174 else
175 {
176 scanf("%d %d",&st,&len);
177 Update(st,st+len-1,0);
178 }
179 }
180 }
181 return 0;
182 }
posted @ 2011-02-23 09:11  AC2012  阅读(331)  评论(0编辑  收藏  举报