1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int maxn=100005; struct Node { int l,r; int ll,rl,ml; int ll1,rl1,ml1; }; Node seg[maxn*3]; void build(int i,int l,int r) { seg[i].l=l; seg[i].r=r; seg[i].ml=seg[i].ll=seg[i].rl=r-l+1; seg[i].ml1=seg[i].ll1=seg[i].rl1=r-l+1; if(l==r)return; int mid=(l+r)/2; build(i<<1,l,mid); build((i<<1)|1,mid+1,r); } int query(int i,int x) { if(seg[i].ml<x)return 0; if(seg[i].ll>=x)return seg[i].l; if(seg[i<<1].ml>=x)return query(i<<1,x); if(seg[i<<1].rl+seg[(i<<1)|1].ll>=x)return seg[i<<1].r-seg[i<<1].rl+1; return query((i<<1)|1,x); } int query1(int i,int x) { if(seg[i].ml1<x)return 0; if(seg[i].ll1>=x)return seg[i].l; if(seg[i<<1].ml1>=x)return query1(i<<1,x); if(seg[i<<1].rl1+seg[(i<<1)|1].ll1>=x)return seg[i<<1].r-seg[i<<1].rl1+1; return query1((i<<1)|1,x); } void pushup(int x) { if(seg[x].l==seg[x].r)return; int lson=2*x; int rson=2*x+1; seg[x].ll=seg[lson].ll; if(seg[lson].ll==seg[lson].r-seg[lson].l+1)seg[x].ll+=seg[rson].ll; seg[x].rl=seg[rson].rl; if(seg[rson].rl==seg[rson].r-seg[rson].l+1)seg[x].rl+=seg[lson].rl; seg[x].ml=max(seg[lson].ml,seg[rson].ml); seg[x].ml=max(seg[x].ml,max(seg[x].ll,seg[x].rl)); seg[x].ml=max(seg[x].ml,seg[lson].rl+seg[rson].ll);
seg[x].ll1=seg[lson].ll1; if(seg[lson].ll1==seg[lson].r-seg[lson].l+1)seg[x].ll1+=seg[rson].ll1; seg[x].rl1=seg[rson].rl1; if(seg[rson].rl1==seg[rson].r-seg[rson].l+1)seg[x].rl1+=seg[lson].rl1; seg[x].ml1=max(seg[lson].ml1,seg[rson].ml1); seg[x].ml1=max(seg[x].ml1,max(seg[x].ll1,seg[x].rl1)); seg[x].ml1=max(seg[x].ml1,seg[lson].rl1+seg[rson].ll1); }
void pushdown(int i) { if(seg[i].l==seg[i].r)return; if(seg[i].ml==0) { seg[2*i].ml=seg[2*i].ll=seg[2*i].rl=0; seg[2*i+1].ml=seg[2*i+1].ll=seg[2*i+1].rl=0; } if(seg[i].ml==seg[i].r-seg[i].l+1) { seg[2*i].ml=seg[2*i].ll=seg[2*i].rl=seg[2*i].r-seg[2*i].l+1; seg[2*i+1].ml=seg[2*i+1].ll=seg[2*i+1].rl=seg[2*i+1].r-seg[2*i+1].l+1; }
if(seg[i].ml1==0) { seg[2*i].ml1=seg[2*i].ll1=seg[2*i].rl1=0; seg[2*i+1].ml1=seg[2*i+1].ll1=seg[2*i+1].rl1=0; } if(seg[i].ml1==seg[i].r-seg[i].l+1) { seg[2*i].ml1=seg[2*i].ll1=seg[2*i].rl1=seg[2*i].r-seg[2*i].l+1; seg[2*i+1].ml1=seg[2*i+1].ll1=seg[2*i+1].rl1=seg[2*i+1].r-seg[2*i+1].l+1; }
} void update(int i,int l,int r) { if(seg[i].l==l && seg[i].r==r) { seg[i].ml=seg[i].ll=seg[i].rl=seg[i].r-seg[i].l+1; seg[i].ml1=seg[i].ll1=seg[i].rl1=seg[i].r-seg[i].l+1; return; } pushdown(i); int mid=(seg[i].l+seg[i].r)/2; if(r<=mid)update(i<<1,l,r); else if(l>mid) update((i<<1)|1,l,r); else { update(i<<1,l,mid); update((i<<1)|1,mid+1,r); } pushup(i); } void update1(int i,int l,int r) { if(seg[i].l==l && seg[i].r==r) { seg[i].ml=seg[i].ll=seg[i].rl=0; return; } pushdown(i); int mid=(seg[i].l+seg[i].r)/2; if(r<=mid)update1(i<<1,l,r); else if(l>mid) update1((i<<1)|1,l,r); else { update1(i<<1,l,mid); update1((i<<1)|1,mid+1,r); } pushup(i); } void update2(int i,int l,int r) { if(seg[i].l==l && seg[i].r==r) { seg[i].ml=seg[i].ll=seg[i].rl=0; seg[i].ml1=seg[i].ll1=seg[i].rl1=0; return; } pushdown(i); int mid=(seg[i].l+seg[i].r)/2; if(r<=mid)update2(i<<1,l,r); else if(l>mid) update2((i<<1)|1,l,r); else { update2(i<<1,l,mid); update2((i<<1)|1,mid+1,r); } pushup(i); }
int main() { int t; int cnt=1; scanf("%d",&t); while(t--){ int n,m; printf("Case %d:\n",cnt++); scanf("%d%d",&n,&m); build(1,1,n); char op[10]; int x; for(int i=0;i<m;i++){ scanf("%s",op); int l,r; if(op[0]=='D'){ scanf("%d",&x); int tmp=query(1,x); if(tmp==0) printf("fly with yourself\n"); else{ update1(1,tmp,tmp+x-1); printf("%d,let's fly\n",tmp); } } else if(op[0]=='N'){ scanf("%d",&x); int tmp=query(1,x); if(tmp!=0){ update2(1,tmp,tmp+x-1); printf("%d,don't put my gezi\n",tmp); continue; } tmp=query1(1,x); if(tmp!=0){ update2(1,tmp,tmp+x-1); printf("%d,don't put my gezi\n",tmp); continue; } printf("wait for me\n"); } else{ scanf("%d%d",&l,&r); printf("I am the hope of chinese chengxuyuan!!\n"); update(1,l,r); } } } return 0; }
|