hdu4553

  1 /*
  2 题意: 中文题
  3 
  4 分析:就是区间覆盖,然后求最前连续长度为len的0串或01串的起始位置;
  5 conl[rt][0]表示区间从左开始最长0串长度,
  6 conr[rt][0]表示区间从右开始最长0串长度;
  7 conl[rt][1]表示区间从左开始最长01串长度,
  8 conr[rt][1]表示区间从右开始最长01串长度;
  9 然后就是模拟; 
 10  
 11 */
 12 #include<cstdio>
 13 #include<cstring>
 14 #include<iostream>
 15 #include<algorithm>
 16 #include<cmath>
 17 #include<cstdlib>
 18 #include<queue>
 19 #include<vector>
 20 #define lson l,m,rt<<1
 21 #define rson m+1,r,rt<<1|1
 22 using namespace std;
 23 const int N=100000+10;
 24 
 25 int conl[N<<2][2],conr[N<<2][2],mx[N<<2][2],col[N<<2];
 26 //0表示空,1表示DS+kong 
 27 void pushup(int l,int m,int r,int rt){
 28     for (int i=0;i<2;i++){
 29         if (conl[rt<<1][i]==m-l+1){
 30             conl[rt][i]=conl[rt<<1][i]+conl[rt<<1|1][i];
 31         }else conl[rt][i]=conl[rt<<1][i];
 32         if (conr[rt<<1|1][i]==r-m){
 33             conr[rt][i]=conr[rt<<1|1][i]+conr[rt<<1][i];
 34         }else conr[rt][i]=conr[rt<<1|1][i];
 35         
 36         int t=conr[rt<<1][i]+conl[rt<<1|1][i];
 37         mx[rt][i]=max(t,max(mx[rt<<1][i],mx[rt<<1|1][i]));
 38     }
 39 }
 40 void pushdown(int l,int m,int r,int rt){
 41     if (col[rt]!=-1){
 42         if (col[rt]==0){
 43             conl[rt<<1][0]=conr[rt<<1][0]=mx[rt<<1][0]=m-l+1;
 44             conl[rt<<1][1]=conr[rt<<1][1]=mx[rt<<1][1]=m-l+1;
 45             
 46             conl[rt<<1|1][0]=conr[rt<<1|1][0]=mx[rt<<1|1][0]=r-m;
 47             conl[rt<<1|1][1]=conr[rt<<1|1][1]=mx[rt<<1|1][1]=r-m;
 48             
 49         }else if (col[rt]==1){
 50             conl[rt<<1][0]=conr[rt<<1][0]=mx[rt<<1][0]=0;
 51             conl[rt<<1][1]=conr[rt<<1][1]=mx[rt<<1][1]=m-l+1;
 52             
 53             conl[rt<<1|1][0]=conr[rt<<1|1][0]=mx[rt<<1|1][0]=0;
 54             conl[rt<<1|1][1]=conr[rt<<1|1][1]=mx[rt<<1|1][1]=r-m;
 55             
 56         }else if (col[rt]==2){
 57             conl[rt<<1][0]=conr[rt<<1][0]=mx[rt<<1][0]=0;
 58             conl[rt<<1][1]=conr[rt<<1][1]=mx[rt<<1][1]=0;
 59             
 60             conl[rt<<1|1][0]=conr[rt<<1|1][0]=mx[rt<<1|1][0]=0;
 61             conl[rt<<1|1][1]=conr[rt<<1|1][1]=mx[rt<<1|1][1]=0;
 62             
 63         }
 64         col[rt<<1]=col[rt<<1|1]=col[rt];
 65         col[rt]=-1;
 66     }
 67 }
 68 void build(int l,int r,int rt){
 69     mx[rt][0]=conl[rt][0]=conr[rt][0]=r-l+1;
 70     mx[rt][1]=conl[rt][1]=conr[rt][1]=r-l+1;
 71     col[rt]=-1;
 72     if (l==r) return ;
 73     int m=(l+r)>>1;
 74     build(lson);
 75     build(rson);
 76     pushup(l,m,r,rt);
 77 }
 78 void update(int L,int R,int z,int l,int r,int rt){
 79     if (L<=l && r<=R){
 80         if (z==0){
 81             conl[rt][0]=conr[rt][0]=mx[rt][0]=r-l+1;
 82             conl[rt][1]=conr[rt][1]=mx[rt][1]=r-l+1;
 83         }else if (z==1){
 84             conl[rt][0]=conr[rt][0]=mx[rt][0]=0;
 85             conl[rt][1]=conr[rt][1]=mx[rt][1]=r-l+1;
 86             
 87         }else if (z==2){
 88             conl[rt][0]=conr[rt][0]=mx[rt][0]=0;
 89             conl[rt][1]=conr[rt][1]=mx[rt][1]=0;
 90         }
 91         col[rt]=z;
 92         return;
 93     }
 94     int m=(l+r)>>1;
 95     pushdown(l,m,r,rt);
 96     if (L<=m) update(L,R,z,lson);
 97     if (m< R) update(L,R,z,rson);
 98     pushup(l,m,r,rt);
 99 }
100 int  query(int k,int len,int l,int r,int rt){
101     if (mx[rt][k]<len) return -1;
102     int m=(l+r)>>1;
103     pushdown(l,m,r,rt);
104     if (mx[rt<<1][k]>=len) return query(k,len,lson);
105     else if (conr[rt<<1][k]+conl[rt<<1|1][k]>=len) return m-conr[rt<<1][k]+1;
106     else if (mx[rt<<1|1][k]>=len) return query(k,len,rson);
107 }
108 int n,m;
109 int main(){
110     int T,cas=0;
111     char s[15];
112     int len;
113     scanf("%d",&T);
114     while (T--){
115         scanf("%d%d",&n,&m);
116         printf("Case %d:\n",++cas);
117         build(1,n,1);
118         for (int i=0;i<m;i++){
119             scanf("%s",s);
120             if (s[0]=='D'){
121                 scanf("%d",&len);
122                 int t=query(0,len,1,n,1); 
123                 if (t==-1){
124                     printf("fly with yourself\n");
125                 }else {
126                     printf("%d,let's fly\n",t);
127                     update(t,t+len-1,1,1,n,1);
128                 }
129             }else if (s[0]=='N'){
130                 scanf("%d",&len);
131                 int t=query(0,len,1,n,1); 
132                 if (t==-1){
133                     t=query(1,len,1,n,1);
134                     if (t==-1) printf("wait for me\n");
135                     else {
136                         printf("%d,don't put my gezi\n",t);
137                         update(t,t+len-1,2,1,n,1);
138                     }
139                 }else {
140                     printf("%d,don't put my gezi\n",t);
141                     update(t,t+len-1,2,1,n,1);
142                 }
143             
144             }else if (s[0]=='S'){
145                 int x,y;
146                 scanf("%d%d",&x,&y);
147                 printf("I am the hope of chinese chengxuyuan!!\n");
148                 update(x,y,0,1,n,1);
149             }
150             
151         }
152     }
153     return 0;
154 }

 

转载于:https://www.cnblogs.com/Rlemon/archive/2013/05/18/3086198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值