寒假来了,又到了小明和女神们约会的季节。
小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。
我们已知小明一共有T的空闲时间,期间会有很多女神或者基友来找小明。
作为一个操作系统曾经怒考71分的大神,小明想到了一个算法,即“首次适应算法”,根据操作系统课本的描述,就是找一段最靠前的符合要求的连续空间分配给每个请求,由此小明做出了一个决定:
当一个基友来找小明时,小明就根据“首次适应算法”来找一段空闲的时间来和基友约好,如果找到,就说“X,let’s fly”(此处,X为开始时间),否则就说“fly with yourself”;
当女神来找小明时,先使用一次“首次适应算法”,如果没有找到,小明就冒着木叽叽的风险无视所有屌丝基友的约定,再次使用“无视基友首次适应算法”,两次只要有一次找到,就说“X,don’t put my gezi”(此处,X为开始时间),否则就说“wait for me”
当然,我们知道小明不是一个节操负无穷的人,如果和女神约会完,还有剩余时间,他还是会和原来约好的基友去dota的。(举个例子:小西(屌丝)和小明约好在1~5这个时间单位段内打dota,这时候,女神来和小明预约长度为3的时间段,那么最终就是1~3小明去和女神约会,搞定后在4~5和小西打dota)
小明偶尔也会想要学习新知识,此时小明就会把某一个时间区间的所有已经预定的时间全部清空用来学习并且怒吼“I am the hope of chinese chengxuyuan!!”,不过小明一般都是三分钟热度,再有人来预定的话,小明就会按耐不住寂寞把学习新知识的时间分配出去。
记录区间最大段的空闲和区间最大段的和基友约的时间,lazy记录被占用或被清除
然后普通的线段树区间操作,分两种查询树上二分查询即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h>
2 #include<string.h>
3 const int maxm=100005;
4 const int INF=0x3f3f3f3f;
5
6 int st[2][maxm<<2],stl[2][maxm<<2],str[2][maxm<<2],stx[2][maxm<<2];
7 int la[2][maxm<<2]; //-1 beizhanyong 1 beiqingchu
8 int sta;
9 //nvshen 0 diaosi 1
10
11 int max(int a,int b){return a>b?a:b;}
12 int min(int a,int b){return a<b?a:b;}
13
14 void build(int o,int l,int r){
15 st[0][o]=st[1][o]=stl[0][o]=stl[1][o]=str[0][o]=str[1][o]=r-l+1;
16 stx[0][o]=stx[1][o]=l;
17 la[0][o]=la[1][o]=1;
18 if(l==r)return;
19 int m=l+((r-l)>>1);
20 build(o<<1,l,m);
21 build(o<<1|1,m+1,r);
22 }
23
24 void pushup(int o,int l,int r,int t){
25 int m=l+((r-l)>>1);
26 if(stl[t][o<<1]==m-l+1)stl[t][o]=stl[t][o<<1]+stl[t][o<<1|1];
27 else stl[t][o]=stl[t][o<<1];
28 if(str[t][o<<1|1]==r-m)str[t][o]=str[t][o<<1]+str[t][o<<1|1];
29 else str[t][o]=str[t][o<<1|1];
30 if(st[t][o<<1]>st[t][o<<1|1]){
31 st[t][o]=st[t][o<<1];
32 stx[t][o]=stx[t][o<<1];
33 }
34 else{
35 st[t][o]=st[t][o<<1|1];
36 stx[t][o]=stx[t][o<<1|1];
37 }
38 if(str[t][o<<1]+stl[t][o<<1|1]>st[t][o]){
39 st[t][o]=str[t][o<<1]+stl[t][o<<1|1];
40 stx[t][o]=m-str[t][o<<1]+1;
41 }
42 }
43
44 void pushdown(int o,int l,int r,int t){
45 if(la[t][o]==-1){
46 la[t][o<<1]=la[t][o<<1|1]=-1;
47 stl[t][o<<1]=str[t][o<<1]=stl[t][o<<1|1]=str[t][o<<1|1]=st[t][o<<1]=st[t][o<<1|1]=0;
48 stx[t][o<<1]=stx[t][o<<1|1]=INF;
49 la[t][o]=0;
50 }
51 else if(la[t][o]==1){
52 la[t][o<<1]=la[t][o<<1|1]=1;
53 int m=l+((r-l)>>1);
54 stl[t][o<<1]=str[t][o<<1]=st[t][o<<1]=m-l+1;
55 stl[t][o<<1|1]=str[t][o<<1|1]=st[t][o<<1|1]=r-m;
56 stx[t][o<<1]=l;
57 stx[t][o<<1|1]=m+1;
58 la[t][o]=0;
59 }
60 }
61
62 void query(int o,int l,int r,int c,int t){
63 if(st[t][o]>=c)sta=min(sta,stx[t][o]);
64 else return;
65 if(l==r)return;
66 pushdown(o,l,r,t);
67 int m=l+((r-l)>>1);
68 if(str[t][o<<1]+stl[t][o<<1|1]>=c&&m-str[t][o<<1]+1<sta)sta=m-str[t][o<<1]+1;
69 if(l<sta)query(o<<1,l,m,c,t);
70 if(m+1<sta)query(o<<1|1,m+1,r,c,t);
71 }
72
73 void update(int o,int l,int r,int ql,int qr,int c,int t){
74 if(ql<=l&&qr>=r){
75 if(c==-1){
76 la[t][o]=-1;
77 stl[t][o]=str[t][o]=st[t][o]=0;
78 stx[t][o]=INF;
79 }
80 else if(c==1){
81 la[t][o]=1;
82 stl[t][o]=str[t][o]=st[t][o]=r-l+1;
83 stx[t][o]=l;
84 }
85 return;
86 }
87 pushdown(o,l,r,t);
88 int m=l+((r-l)>>1);
89 if(ql<=m)update(o<<1,l,m,ql,qr,c,t);
90 if(qr>=m+1)update(o<<1|1,m+1,r,ql,qr,c,t);
91 pushup(o,l,r,t);
92 }
93
94 char s[15];
95
96 int main(){
97 int T;
98 scanf("%d",&T);
99 for(int q=1;q<=T;++q){
100 int n,m;
101 scanf("%d%d",&n,&m);
102 printf("Case %d:
",q);
103 build(1,1,n);
104 for(int i=1;i<=m;++i){
105 scanf("%s",s);
106 if(s[0]=='D'){
107 int c;
108 scanf("%d",&c);
109 sta=INF;
110 query(1,1,n,c,1);
111 if(sta==INF)printf("fly with yourself
");
112 else{
113 update(1,1,n,sta,sta+c-1,-1,1);
114 printf("%d,let's fly
",sta);
115 }
116 }
117 else if(s[0]=='N'){
118 int c;
119 scanf("%d",&c);
120 sta=INF;
121 query(1,1,n,c,1);
122 if(sta==INF){
123 query(1,1,n,c,0);
124 if(sta==INF)printf("wait for me
");
125 else{
126 update(1,1,n,sta,sta+c-1,-1,1);
127 update(1,1,n,sta,sta+c-1,-1,0);
128 printf("%d,don't put my gezi
",sta);
129 }
130 }
131 else{
132 update(1,1,n,sta,sta+c-1,-1,1);
133 update(1,1,n,sta,sta+c-1,-1,0);
134 printf("%d,don't put my gezi
",sta);
135 }
136 }
137 else if(s[0]=='S'){
138 int a,b;
139 scanf("%d%d",&a,&b);
140 update(1,1,n,a,b,1,1);
141 update(1,1,n,a,b,1,0);
142 printf("I am the hope of chinese chengxuyuan!!
");
143 }
144 }
145 }
146 }