不断更新中
模拟
- [Luogu P3952 时间复杂度]
1 #include<cstdio> 2 #include<map> 3 using namespace std; 4 struct node{ 5 char ch; 6 bool N; 7 }sk[100]; 8 char c,name,b1,b2,tmp,be,fake[50]; 9 int m,n,p,num1,num2,FF,cp,cnt,rec,top; 10 bool F,n1,n2,enter,ERR; 11 int main() { 12 scanf("%d\n",&n); 13 for(register int ii=1; ii<=n; ii++) { 14 map<int,bool>M; 15 F=FF=n1=n2=cp=p=top=cnt=0; 16 ERR=0; 17 scanf("%d O(%c",&m,&c); 18 if(c=='n') { 19 scanf("^%d",&p); 20 F=1; 21 } 22 gets(fake); 23 enter=1; 24 for(register int i=1; i<=m; i++) { 25 n1=n2=0; 26 scanf("%c",&be); 27 if(be=='F') { 28 if(top<=rec)enter=1; 29 scanf(" %c ",&name); 30 if(!M[name])M[name]=1; 31 else { 32 puts("ERR"); 33 ERR=1; 34 for(register int j=i; j<=m; j++)gets(fake); 35 break; 36 } 37 b1=tmp=getchar(); 38 num1=num2=0; 39 while(tmp>='0'&&tmp<='9') { 40 n1=1; 41 num1=(num1<<1)+(num1<<3)+(tmp^48); 42 tmp=getchar(); 43 } 44 b2=tmp=getchar(); 45 if(!n1)b2=tmp=getchar(); 46 while(tmp>='0'&&tmp<='9') { 47 n2=1; 48 num2=(num2<<1)+(num2<<3)+(tmp^48); 49 tmp=getchar(); 50 } 51 if(top<=rec)enter=1; 52 if(b2=='n'&&b1!='n'){ 53 sk[++top].N=1; 54 sk[top].ch=name; 55 cnt++; 56 } 57 else { 58 sk[++top].N=0; 59 sk[top].ch=name; 60 } 61 if((b2=='n'&&b1!='n')&&n1&&enter&&cnt>cp)cp=cnt; 62 else if((b1=='n'&&n2)||(num2<num1&&n1&&n2)){ 63 enter=0; 64 rec=FF; 65 } 66 } else if(be=='E'){ 67 if(sk[top].N==1)cnt--; 68 M[sk[top].ch]=0; 69 top--; 70 } 71 scanf("\n"); 72 } 73 if(ERR)continue; 74 if(top!=0)puts("ERR"); 75 else if((cp>0&&cp==p)||(cp==0&&p==0))puts("Yes"); 76 else puts("No"); 77 } 78 }
- [Luogu P2482 猪国杀]
1 #include<cstdio> 2 #include<vector> 3 #include<cstdlib> 4 #define zhugong 1 5 #define zhongchen 2 6 #define fanzei 3 7 #define leifan 4 8 using namespace std; 9 struct node { 10 bool dead,zgln; 11 int hp,jump,shenfen; 12 vector<char>handcard; 13 } pig[11]; 14 char aa,bb,cc,dd,iidd,cardheap[2001]; 15 int m,n,loop,cnt,to,battlecnt,wxkjcnt,pointer,tmp; 16 bool GoodPigsWin,AlrKill,GoodPigsAsk,FF; 17 inline void Delete(int pignum,int position) { 18 pig[pignum].handcard.erase(pig[pignum].handcard.begin()+position); 19 } 20 inline int WuXieKeJiFromLoyalty(int begin) { 21 FF=true; 22 for(register int i=begin; true; i++) { 23 if(i>n)i=1; 24 if(i==begin&&!FF)break; 25 FF=false; 26 if(pig[i].dead||pig[i].shenfen==fanzei)continue; 27 for(register int j=0; j<pig[i].handcard.size(); j++) { 28 if(pig[i].handcard[j]=='J') { 29 Delete(i,j); 30 pig[i].jump=zhongchen; 31 return i; 32 } 33 } 34 } 35 return 0; 36 } 37 inline int WuXieKeJiFromThief(int begin) { 38 FF=true; 39 for(register int i=begin; true; i++) { 40 if(i>n)i=1; 41 if(i==begin&&!FF)break; 42 FF=false; 43 if(pig[i].dead||pig[i].shenfen==zhongchen||i==1)continue; 44 for(register int j=0; j<pig[i].handcard.size(); j++) { 45 if(pig[i].handcard[j]=='J') { 46 Delete(i,j); 47 pig[i].jump=fanzei; 48 return i; 49 } 50 } 51 } 52 return 0; 53 } 54 inline bool AskForWuXieKeJi(int pignum,int carduser) { 55 if(pig[pignum].jump==leifan||!pig[pignum].jump)return false; 56 if(pignum==1||pig[pignum].jump==zhongchen)GoodPigsAsk=true; 57 else GoodPigsAsk=false; 58 wxkjcnt=0; 59 while(true){ 60 if(wxkjcnt%2==0){ 61 if(GoodPigsAsk){ 62 tmp=WuXieKeJiFromLoyalty(carduser); 63 if(!tmp)return false; 64 carduser=tmp; 65 }else{ 66 tmp=WuXieKeJiFromThief(carduser); 67 if(!tmp)return false; 68 carduser=tmp; 69 } 70 }else{ 71 if(GoodPigsAsk){ 72 tmp=WuXieKeJiFromThief(carduser); 73 if(!tmp)return true; 74 carduser=tmp; 75 }else{ 76 tmp=WuXieKeJiFromLoyalty(carduser); 77 if(!tmp)return true; 78 carduser=tmp; 79 } 80 } 81 wxkjcnt++; 82 } 83 } 84 inline void getcard(int pignum,char carddd) { 85 pig[pignum].handcard.push_back(carddd); 86 } 87 inline void CardFromHeap(int pignum,int AmountOfCard) { 88 while(AmountOfCard--){ 89 if(pointer!=m)getcard(pignum,cardheap[++pointer]); 90 else getcard(pignum,cardheap[m]); 91 } 92 } 93 inline void EatPeach(int pignum,int cardpos) { 94 Delete(pignum,cardpos); 95 pig[pignum].hp++; 96 } 97 inline void PrintResult() { 98 if(GoodPigsWin)puts("MP"); 99 else puts("FP"); 100 for(register int i=1; i<=n; i++) { 101 if(pig[i].dead)puts("DEAD"); 102 else { 103 for(register int j=0; j<pig[i].handcard.size(); j++) { 104 putchar(pig[i].handcard[j]); 105 putchar(' '); 106 } 107 putchar('\n'); 108 } 109 } 110 exit(0); 111 } 112 inline void AboutToDie(int pignum,int HurtFrom) { 113 for(register int i=0; i<pig[pignum].handcard.size(); i++) { 114 if(pig[pignum].handcard[i]=='P') { 115 EatPeach(pignum,i); 116 return; 117 } 118 } 119 pig[pignum].dead=1; 120 if(pignum==1)PrintResult(); 121 if(pig[pignum].shenfen==fanzei) { 122 if(--cnt==0) { 123 GoodPigsWin=1; 124 PrintResult(); 125 } 126 CardFromHeap(HurtFrom,3); 127 } 128 if(HurtFrom==1&&pig[pignum].shenfen==zhongchen) { 129 pig[1].handcard.erase(pig[1].handcard.begin(),pig[1].handcard.end()); 130 pig[1].zgln=0; 131 } 132 } 133 inline bool Shan(int pignum) { 134 for(register int i=0; i<pig[pignum].handcard.size(); i++) { 135 if(pig[pignum].handcard[i]=='D') { 136 Delete(pignum,i); 137 return true; 138 } 139 } 140 return false; 141 } 142 inline bool Sha(int pignum) { 143 for(register int i=0; i<pig[pignum].handcard.size(); i++) { 144 if(pig[pignum].handcard[i]=='K') { 145 Delete(pignum,i); 146 return true; 147 } 148 } 149 return false; 150 } 151 inline void AOE(int pignum,int cardpos,char cardkind) { 152 Delete(pignum,cardpos); 153 if(cardkind=='N') { 154 for(register int i=pignum+1; i!=pignum; i++) { 155 if(i>n)i=1; 156 if(pignum==1&&i==1)break; 157 if(pig[i].dead||AskForWuXieKeJi(i,pignum))continue; 158 if(!Sha(i)) { 159 pig[i].hp--; 160 if(i==1&&!pig[pignum].jump)pig[pignum].jump=leifan; 161 if(pig[i].hp==0)AboutToDie(i,pignum); 162 } 163 } 164 } else { 165 for(register int i=pignum+1; i!=pignum; i++) { 166 if(i>n)i=1; 167 if(pignum==1&&i==1)break; 168 if(pig[i].dead||AskForWuXieKeJi(i,pignum))continue; 169 if(!Shan(i)) { 170 pig[i].hp--; 171 if(i==1&&!pig[pignum].jump)pig[pignum].jump=leifan; 172 if(pig[i].hp==0)AboutToDie(i,pignum); 173 } 174 } 175 } 176 } 177 inline void Battle(int pignum,int against) { 178 if(AskForWuXieKeJi(against,pignum))return; 179 if(pig[against].shenfen==zhongchen&&pignum==1) { 180 pig[against].hp--; 181 if(pig[against].hp==0)AboutToDie(against,pignum); 182 return; 183 } 184 battlecnt=0; 185 while(true) { 186 if(battlecnt%2==0) { 187 if(!Sha(against)) { 188 pig[against].hp--; 189 if(pig[against].hp==0)AboutToDie(against,pignum); 190 return; 191 } 192 } else { 193 if(!Sha(pignum)) { 194 pig[pignum].hp--; 195 if(pig[pignum].hp==0)AboutToDie(pignum,against); 196 return; 197 } 198 } 199 ++battlecnt; 200 } 201 } 202 inline int FindThief(int pignum) { 203 for(register int i=pignum+1; i!=pignum; i++) { 204 if(i>n)i=1; 205 if(i==1&&pignum==1)return 0; 206 if((!pig[i].dead)&&(pig[i].jump==fanzei||(pignum==1&&pig[i].jump==leifan)))return i; 207 } 208 return 0; 209 } 210 inline int killthief(int pignum) { 211 for(register int i=pignum+1; i!=pignum; i++) { 212 if(i>n)i=1; 213 if(i==1&&pignum==1)return 0; 214 if(!pig[i].dead) { 215 if(pig[i].jump==fanzei||(pignum==1&&pig[i].jump==leifan))return i; 216 return 0; 217 } 218 } 219 } 220 inline int killloyalty(int pignum) { 221 for(register int i=pignum+1; i!=pignum; i++) { 222 if(i>n)i=1; 223 if(!pig[i].dead) { 224 if(i==1||pig[i].jump==zhongchen)return i; 225 return 0; 226 } 227 } 228 } 229 int main() { 230 scanf("%d%d\n",&n,&m); 231 pig[1].shenfen=zhugong; 232 for(register int i=1; i<=n; i++) { 233 scanf("%cP %c %c %c %c\n",&iidd,&aa,&bb,&cc,&dd); 234 if(iidd=='Z')pig[i].shenfen=zhongchen; 235 else if(iidd=='F'){ 236 cnt++; 237 pig[i].shenfen=fanzei; 238 } 239 getcard(i,aa); 240 getcard(i,bb); 241 getcard(i,cc); 242 getcard(i,dd); 243 pig[i].hp=4; 244 } 245 pig[1].jump=zhugong; 246 for(register int i=1; i<=m; i++)scanf("%c ",&cardheap[i]); 247 while(true) { 248 if(++loop>n)loop=1; 249 if(pig[loop].dead)continue; 250 CardFromHeap(loop,2); 251 if(loop==1) { 252 AlrKill=false; 253 for(register int j=0; j<pig[1].handcard.size(); j++) { 254 if(pig[1].handcard[j]=='P'&&pig[1].hp<4) { 255 EatPeach(1,j); 256 j=-1; 257 } else if(pig[1].handcard[j]=='N'||pig[1].handcard[j]=='W') { 258 AOE(1,j,pig[1].handcard[j]); 259 j=-1; 260 } else if(pig[1].handcard[j]=='K'&&((!AlrKill)||pig[1].zgln)) { 261 to=killthief(1); 262 if(!to)continue; 263 AlrKill=true; 264 Delete(1,j); 265 if(!Shan(to)) { 266 pig[to].hp--; 267 if(!pig[to].hp)AboutToDie(to,1); 268 } 269 j=-1; 270 } else if(pig[1].handcard[j]=='F') { 271 to=FindThief(1); 272 if(!to)continue; 273 Delete(1,j); 274 Battle(1,to); 275 j=-1; 276 } else if(pig[1].handcard[j]=='Z') { 277 Delete(1,j); 278 pig[1].zgln=true; 279 j=-1; 280 } 281 } 282 } else if(pig[loop].shenfen==zhongchen) { 283 AlrKill=false; 284 for(register int j=0; j<pig[loop].handcard.size(); j++) { 285 if(pig[loop].dead)break; 286 if(pig[loop].handcard[j]=='P'&&pig[loop].hp<4) { 287 EatPeach(loop,j); 288 j=-1; 289 } else if(pig[loop].handcard[j]=='N'||pig[loop].handcard[j]=='W') { 290 AOE(loop,j,pig[loop].handcard[j]); 291 j=-1; 292 } else if(pig[loop].handcard[j]=='K'&&(!AlrKill||pig[loop].zgln)) { 293 to=killthief(loop); 294 if(!to)continue; 295 AlrKill=true; 296 pig[loop].jump=zhongchen; 297 Delete(loop,j); 298 if(!Shan(to)) { 299 pig[to].hp--; 300 if(!pig[to].hp)AboutToDie(to,loop); 301 } 302 j=-1; 303 } else if(pig[loop].handcard[j]=='F') { 304 to=FindThief(loop); 305 if(!to)continue; 306 pig[loop].jump=zhongchen; 307 Delete(loop,j); 308 Battle(loop,to); 309 j=-1; 310 } else if(pig[loop].handcard[j]=='Z') { 311 Delete(loop,j); 312 pig[loop].zgln=1; 313 j=-1; 314 } 315 } 316 } else { 317 AlrKill=false; 318 for(register int j=0; j<pig[loop].handcard.size(); j++) { 319 if(pig[loop].dead)break; 320 if(pig[loop].handcard[j]=='P'&&pig[loop].hp<4) { 321 EatPeach(loop,j); 322 j=-1; 323 } else if(pig[loop].handcard[j]=='N'||pig[loop].handcard[j]=='W') { 324 AOE(loop,j,pig[loop].handcard[j]); 325 j=-1; 326 } else if(pig[loop].handcard[j]=='K'&&(!AlrKill||pig[loop].zgln)) { 327 to=killloyalty(loop); 328 if(!to)continue; 329 AlrKill=1; 330 pig[loop].jump=fanzei; 331 Delete(loop,j); 332 if(!Shan(to)) { 333 pig[to].hp--; 334 if(!pig[to].hp)AboutToDie(to,loop); 335 } 336 j=-1; 337 } else if(pig[loop].handcard[j]=='F') { 338 pig[loop].jump=fanzei; 339 Delete(loop,j); 340 Battle(loop,1); 341 j=-1; 342 } else if(pig[loop].handcard[j]=='Z') { 343 Delete(loop,j); 344 pig[loop].zgln=1; 345 j=-1; 346 } 347 } 348 } 349 } 350 }
- [Luogu P3693 琪露诺的冰雪小屋]
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #define leftup 1 5 #define leftdown 2 6 #define rightup 3 7 #define rightdown 4 8 #define I_WILL_AK_IOI___I_WILL_AK_IOI___I_WILL_AK_IOI___I_WILL_AK_IOI 9 using namespace std; 10 struct node { 11 bool h[25]; 12 int cold; 13 } freeze[17][17]; 14 struct Node { 15 int x,y,z; 16 } queue[258]; 17 int cnt,n,hm,hr,hc,hx,hy,m,r,c,d,s,h,bf5ans,high,hole,head,tail,ans1,ans2_1,ans2_2,roofneed,k1,k2,CornerCount,DoorPos,preplus; 18 string opt; 19 bool havedoor,breakcorner,DoorinMiddle,DoorAtSide,DoorMakeSide,CanMakeADoor,bf5[17][17][25]; 20 inline bool canput() { 21 if(freeze[r][c].h[h])return false; 22 if(!h)return true; 23 if(freeze[r][c].h[h+1])return true; 24 if(freeze[r][c].h[h-1])return true; 25 if(c<n-1&&freeze[r][c+1].h[h])return true; 26 if(r<n-1&&freeze[r+1][c].h[h])return true; 27 if(c&&freeze[r][c-1].h[h])return true; 28 if(r&&freeze[r-1][c].h[h])return true; 29 return false; 30 } 31 inline Node makeNode(int xx,int yy,int zz) { 32 Node nn; 33 nn.x=xx; 34 nn.y=yy; 35 nn.z=zz; 36 return nn; 37 } 38 inline int CountCorner(int xx,int yy,int Height) { 39 int ret=0; 40 for(register int i=0; i<Height; i++)if(!freeze[xx][yy].h[i])ret++; 41 return ret; 42 } 43 inline bool bf5_bf5(bool make_roof) { 44 memset(bf5,0,sizeof(bf5)); 45 bf5ans=0; 46 for(int i=0; i<n; i++) { 47 for(int j=0; j<n; j++) { 48 if(freeze[i][j].h[0]&&!bf5[i][j][0]) { 49 bf5[i][j][0]=true; 50 queue[1]=makeNode(i,j,0); 51 tail=head=1; 52 while(tail<=head) { 53 if(freeze[queue[tail].x][queue[tail].y].h[queue[tail].z+1]&&!bf5[queue[tail].x][queue[tail].y][queue[tail].z+1]) { 54 bf5[queue[tail].x][queue[tail].y][queue[tail].z+1]=true; 55 queue[++head]=makeNode(queue[tail].x,queue[tail].y,queue[tail].z+1); 56 } 57 if(queue[tail].z&&freeze[queue[tail].x][queue[tail].y].h[queue[tail].z-1]&&!bf5[queue[tail].x][queue[tail].y][queue[tail].z-1]) { 58 bf5[queue[tail].x][queue[tail].y][queue[tail].z-1]=true; 59 queue[++head]=makeNode(queue[tail].x,queue[tail].y,queue[tail].z+1); 60 } 61 if(queue[tail].y<n-1&&freeze[queue[tail].x][queue[tail].y+1].h[queue[tail].z]&&!bf5[queue[tail].x][queue[tail].y+1][queue[tail].z]) { 62 bf5[queue[tail].x][queue[tail].y+1][queue[tail].z]=true; 63 queue[++head]=makeNode(queue[tail].x,queue[tail].y+1,queue[tail].z); 64 } 65 if(queue[tail].y&&freeze[queue[tail].x][queue[tail].y-1].h[queue[tail].z]&&!bf5[queue[tail].x][queue[tail].y-1][queue[tail].z]) { 66 bf5[queue[tail].x][queue[tail].y-1][queue[tail].z]=true; 67 queue[++head]=makeNode(queue[tail].x,queue[tail].y-1,queue[tail].z); 68 } 69 if(queue[tail].x<n-1&&freeze[queue[tail].x+1][queue[tail].y].h[queue[tail].z]&&!bf5[queue[tail].x+1][queue[tail].y][queue[tail].z]) { 70 bf5[queue[tail].x+1][queue[tail].y][queue[tail].z]=true; 71 queue[++head]=makeNode(queue[tail].x+1,queue[tail].y,queue[tail].z); 72 } 73 if(queue[tail].x&&freeze[queue[tail].x-1][queue[tail].y].h[queue[tail].z]&&!bf5[queue[tail].x-1][queue[tail].y][queue[tail].z]) { 74 bf5[queue[tail].x-1][queue[tail].y][queue[tail].z]=true; 75 queue[++head]=makeNode(queue[tail].x-1,queue[tail].y,queue[tail].z); 76 } 77 tail++; 78 } 79 } 80 } 81 } 82 for(register int i=0; i<n; i++) { 83 for(register int j=0; j<n; j++) { 84 for(register int hh=1; hh<=hm; hh++) { 85 if(!bf5[i][j][hh]&&freeze[i][j].h[hh]) { 86 if(!(make_roof&&(i==hr||i==hr+hx-1||j==hc||j==hc+hy-1))){ 87 freeze[i][j].h[hh]=false; 88 bf5ans++; 89 } 90 } 91 } 92 } 93 } 94 return bf5ans; 95 } 96 int main() { 97 ios::sync_with_stdio(false); 98 cin>>n>>hm>>hr>>hc>>hx>>hy>>m; 99 while(m--) { 100 cin>>opt; 101 if(opt=="ICE_BARRAGE") { 102 cin>>r>>c>>d>>s; 103 ans1=0; 104 ++s; 105 switch(d) { 106 case 0: 107 for(register int i=r,ccnt=1; i>=0&&ccnt<=s; i--,ccnt++) { 108 if(!freeze[i][c].h[0]) { 109 if(freeze[i][c].cold<4) { 110 ans1++; 111 freeze[i][c].cold++; 112 } 113 } else break; 114 } 115 break; 116 case 1: 117 for(register int i=r,j=c,ccnt=1; i>=0&&j>=0&&ccnt<=s; i--,j--,ccnt++) { 118 if(!freeze[i][j].h[0]) { 119 if(freeze[i][j].cold<4) { 120 ans1++; 121 freeze[i][j].cold++; 122 } 123 } else break; 124 } 125 break; 126 case 2: 127 for(register int j=c,ccnt=1; j>=0&&ccnt<=s; j--,ccnt++) { 128 if(!freeze[r][j].h[0]) { 129 if(freeze[r][j].cold<4) { 130 ans1++; 131 freeze[r][j].cold++; 132 } 133 } else break; 134 } 135 break; 136 case 3: 137 for(register int i=r,j=c,ccnt=1; i<n&&j>=0&&ccnt<=s; i++,j--,ccnt++) { 138 if(!freeze[i][j].h[0]) { 139 if(freeze[i][j].cold<4) { 140 ans1++; 141 freeze[i][j].cold++; 142 } 143 } else break; 144 } 145 break; 146 case 4: 147 for(register int i=r,ccnt=1; i<n&&ccnt<=s; i++,ccnt++) { 148 if(!freeze[i][c].h[0]) { 149 if(freeze[i][c].cold<4) { 150 ans1++; 151 freeze[i][c].cold++; 152 } 153 } else break; 154 } 155 break; 156 case 5: 157 for(register int i=r,j=c,ccnt=1; i<n&&j<n&&ccnt<=s; i++,j++,ccnt++) { 158 if(!freeze[i][j].h[0]) { 159 if(freeze[i][j].cold<4) { 160 ans1++; 161 freeze[i][j].cold++; 162 } 163 } else break; 164 } 165 break; 166 case 6: 167 for(register int j=c,ccnt=1; j<n&&ccnt<=s; j++,ccnt++) { 168 if(!freeze[r][j].h[0]) { 169 if(freeze[r][j].cold<4) { 170 ans1++; 171 freeze[r][j].cold++; 172 } 173 } else break; 174 } 175 break; 176 default: 177 for(register int i=r,j=c,ccnt=1; i>=0&&j<n&&ccnt<=s; i--,j++,ccnt++) { 178 if(!freeze[i][j].h[0]) { 179 if(freeze[i][j].cold<4) { 180 ans1++; 181 freeze[i][j].cold++; 182 } 183 } else break; 184 } 185 break; 186 } 187 cout<<"CIRNO FREEZED "<<ans1<<" BLOCK(S)\n"; 188 } else if(opt=="MAKE_ICE_BLOCK") { 189 ans2_1=0; 190 for(register int i=0; i<n; i++) { 191 for(register int j=0; j<n; j++) { 192 if(freeze[i][j].cold==4) { 193 freeze[i][j].cold=0; 194 ans2_1++; 195 ans2_2++; 196 } 197 } 198 } 199 cout<<"CIRNO MADE "<<ans2_1<<" ICE BLOCK(S),NOW SHE HAS "<<ans2_2<<" ICE BLOCK(S)\n"; 200 } else if(opt=="PUT_ICE_BLOCK") { 201 cin>>r>>c>>h; 202 if(!ans2_2) cout<<"CIRNO HAS NO ICE_BLOCK\n"; 203 else if(!canput())cout<<"BAKA CIRNO,CAN'T PUT HERE\n"; 204 else if(r<hr||r>hr+hx-1||c<hc||c>hc+hy-1) { 205 cout<<"CIRNO MISSED THE PLACE\n"; 206 ans2_2--; 207 freeze[r][c].h[h]=true; 208 if(!h)freeze[r][c].cold=0; 209 } else if(hr+1<=r&&r<=hr+hx-2&&hc+1<=c&&c<=hc+hy-2) { 210 cout<<"CIRNO PUT AN ICE_BLOCK INSIDE THE HOUSE\n"; 211 ans2_2--; 212 freeze[r][c].h[h]=true; 213 if(!h)freeze[r][c].cold=0; 214 } else { 215 cout<<"CIRNO SUCCESSFULLY PUT AN ICE_BLOCK,NOW SHE HAS "<<--ans2_2<<" ICE_BLOCK(S)\n"; 216 freeze[r][c].h[h]=true; 217 if(!h)freeze[r][c].cold=0; 218 } 219 } else if(opt=="REMOVE_ICE_BLOCK") { 220 cin>>r>>c>>h; 221 if(!freeze[r][c].h[h])cout<<"BAKA CIRNO,THERE IS NO ICE_BLOCK\n"; 222 else { 223 freeze[r][c].h[h]=false; 224 ans2_2++; 225 if(bf5_bf5(false))cout<<"CIRNO REMOVED AN ICE_BLOCK,AND "<<bf5ans<<" BLOCK(S) ARE BROKEN\n"; 226 else cout<<"CIRNO REMOVED AN ICE_BLOCK\n"; 227 } 228 } else if(opt=="MAKE_ROOF") { 229 for(register int j=hc; j<hc+hy; j++) { 230 for(register int k=0; k<=hm; k++) { 231 if(freeze[hr][j].h[k])high=max(high,k); 232 if(freeze[hr+hx-1][j].h[k])high=max(high,k); 233 } 234 } 235 for(register int i=hr+1; i<hr+hy-1; i++) { 236 for(register int k=0; k<=hm; k++) { 237 if(freeze[i][hc].h[k])high=max(high,k); 238 if(freeze[i][hc+hy-1].h[k])high=max(high,k); 239 } 240 } 241 roofneed=0; 242 for(register int i=hr; i<hr+hx; i++) { 243 for(register int j=hc; j<hc+hy; j++) { 244 if(!freeze[i][j].h[high+1]) { 245 roofneed++; 246 freeze[i][j].h[high+1]=true; 247 } 248 } 249 } 250 if(ans2_2<roofneed) { 251 cout<<"SORRY CIRNO,NOT ENOUGH ICE_BLOCK(S) TO MAKE ROOF"; 252 return 0; 253 } 254 if(high<1||high*(hx-2)*(hy-2)<2) { 255 cout<<"SORRY CIRNO,HOUSE IS TOO SMALL"; 256 return 0; 257 } 258 ans2_2-=roofneed; 259 for(register int i=hr+1; i<hr+hx-1; i++) { 260 for(register int j=hc+1; j<hc+hy-1; j++) { 261 for(register int k=0; k<=high; k++) { 262 if(freeze[i][j].h[k]) { 263 k1++; 264 ans2_2++; 265 freeze[i][j].h[k]=false; 266 } 267 } 268 } 269 } 270 for(register int i=0; i<n; i++) { 271 for(register int j=0; j<n; j++) { 272 for(register int k=0; k<=hm; k++) { 273 if((!(i>=hr&&i<hr+hx&&j>=hc&&j<hc+hy&&k<=(high+1)))&&freeze[i][j].h[k]) { 274 k2++; 275 ans2_2++; 276 freeze[i][j].h[k]=false; 277 } 278 } 279 } 280 } 281 cout<<k1<<" ICE_BLOCK(S) INSIDE THE HOUSE NEED TO BE REMOVED\n"<<k2<<" ICE_BLOCK(S) OUTSIDE THE HOUSE NEED TO BE REMOVED\n"; 282 bf5_bf5(true); 283 for(register int i=hr; i<hr+hx; i++) { 284 for(register int j=hc; j<hc+hy; j++) { 285 if(!bf5[i][j][high+1]) { 286 cout<<"SORRY CIRNO,HOUSE IS BROKEN WHEN REMOVING BLOCKS\n"; 287 return 0; 288 } 289 } 290 } 291 DoorMakeSide=true; 292 for(register int j=hc+1; j<hc+hy-1; j++) { 293 if((!freeze[hr][j].h[0])||(!freeze[hr+hx-1][j].h[0])||(!freeze[hr][j].h[1])||(!freeze[hr+hx-1][j].h[1])) { 294 CanMakeADoor=true; 295 if(j!=hc+1&&j!=hc+hy-2)DoorMakeSide=false; 296 } 297 for(register int k=0; k<=high; k++) { 298 if(!freeze[hr][j].h[k])hole++; 299 if(!freeze[hr+hx-1][j].h[k])hole++; 300 } 301 } 302 for(register int i=hr+1; i<hr+hx-1; i++) { 303 if((!freeze[i][hc].h[0])||(!freeze[i][hc+hy-1].h[0])||(!freeze[i][hc].h[1])||(!freeze[i][hc+hy-1].h[1])) { 304 CanMakeADoor=true; 305 if(i!=hr+1&&i!=hr+hx-2)DoorMakeSide=false; 306 } 307 for(register int k=0; k<=high; k++) { 308 if(!freeze[i][hc].h[k])hole++; 309 if(!freeze[i][hc+hy-1].h[k])hole++; 310 } 311 } 312 DoorAtSide=true; 313 for(register int j=hc+1; j<hc+hy-1; j++) { 314 if((!freeze[hr][j].h[0])&&(!freeze[hr][j].h[1])) { 315 havedoor=true; 316 if(j!=hc+1&&j!=hc+hy-2)DoorAtSide=false; 317 if((j-hc+1)*2==hy||((j-hc+1)*2==(hy+1)))DoorinMiddle=true; 318 } 319 if((!freeze[hr+hx-1][j].h[0])&&(!freeze[hr+hx-1][j].h[1])) { 320 havedoor=true; 321 if(j!=hc+1&&j!=hc+hy-2)DoorAtSide=false; 322 if((j-hc+1)*2==hy||((j-hc+1)*2==(hy+1)))DoorinMiddle=true; 323 } 324 } 325 for(register int i=hr+1; i<hr+hx-1; i++) { 326 if((!freeze[i][hc].h[0])&&(!freeze[i][hc].h[1])) { 327 havedoor=true; 328 if(i!=hr+1&&i!=hr+hx-2)DoorAtSide=false; 329 if((i+hx+1)*2==hy||((i+hx+1)*2==hy))DoorinMiddle=true; 330 } 331 if((!freeze[i][hc+hy-1].h[0])&&(!freeze[i][hc+hy-1].h[1])) { 332 havedoor=true; 333 if(i!=hr+1&&i!=hr+hx-2)DoorAtSide=false; 334 if((i+hx+1)*2==hy||((i+hx+1)*2==hy))DoorinMiddle=true; 335 } 336 } 337 CornerCount=20021115; 338 if(havedoor&&DoorAtSide) { 339 if((((!freeze[hr][hc+1].h[0])&&!freeze[hr][hc+1].h[1])||((!freeze[hr+1][hc].h[0])&&!freeze[hr+1][hc].h[1]))&&CornerCount>CountCorner(hr,hc,2)) { 340 CornerCount=CountCorner(hr,hc,2); 341 DoorPos=leftup; 342 } 343 if((((!freeze[hr+hx-2][hc].h[0])&&!freeze[hr+hx-2][hc].h[1])||((!freeze[hr+hx-1][hc+1].h[0])&&!freeze[hr+hx-1][hc+1].h[1]))&&CornerCount>CountCorner(hr+hx-1,hc,2)) { 344 CornerCount=CountCorner(hr+hx-1,hc,2); 345 DoorPos=leftdown; 346 } 347 if((((!freeze[hr][hc+hy-2].h[0])&&!freeze[hr][hc+hy-2].h[1])||((!freeze[hr+1][hc+hy-1].h[0])&&!freeze[hr+1][hc+hy-1].h[1]))&&CornerCount>CountCorner(hr,hc+hy-1,2)) { 348 CornerCount=CountCorner(hr,hc+hy-1,2); 349 DoorPos=rightup; 350 } 351 if((((!freeze[hr+hx-1][hc+hy-2].h[0])&&!freeze[hr+hx-1][hc+hy-2].h[1])||((!freeze[hr+hx-2][hc+hy-1].h[0])&&!freeze[hr+hx-2][hc+hy-1].h[1]))&&CornerCount>CountCorner(hr+hx-1,hc+hy-1,2)) { 352 CornerCount=CountCorner(hr+hx-1,hc+hy-1,2); 353 DoorPos=rightdown; 354 } 355 } else if(CanMakeADoor&&DoorMakeSide&&!havedoor) { 356 if((!freeze[hr][hc+1].h[0])||(!freeze[hr+1][hc].h[0])) { 357 if(freeze[hr][hc].h[0]&&CornerCount) { 358 CornerCount=0; 359 DoorPos=leftup; 360 } 361 if((!freeze[hr][hc].h[0])&&CornerCount>1) { 362 CornerCount=1; 363 DoorPos=leftup; 364 } 365 } 366 if((!freeze[hr][hc+1].h[1])||(!freeze[hr+1][hc].h[1])) { 367 if(freeze[hr][hc].h[1]&&CornerCount) { 368 CornerCount=0; 369 DoorPos=leftup; 370 } 371 if((!freeze[hr][hc].h[1])&&CornerCount>1) { 372 CornerCount=1; 373 DoorPos=leftup; 374 } 375 } 376 if((!freeze[hr+hx-2][hc].h[0])||(!freeze[hr+hx-1][hc+1].h[0])) { 377 if(freeze[hr+hx-1][hc].h[0]&&CornerCount) { 378 CornerCount=0; 379 DoorPos=leftdown; 380 } 381 if((!freeze[hr+hx-1][hc].h[0])&&CornerCount>1) { 382 CornerCount=1; 383 DoorPos=leftdown; 384 } 385 } 386 if((!freeze[hr+hx-2][hc].h[1])||(!freeze[hr+hx-1][hc+1].h[1])) { 387 if(freeze[hr+hx-1][hc].h[1]&&CornerCount) { 388 CornerCount=0; 389 DoorPos=leftdown; 390 } 391 if((!freeze[hr+hx-1][hc].h[1])&&CornerCount>1) { 392 CornerCount=1; 393 DoorPos=leftdown; 394 } 395 } 396 if((!freeze[hr][hc+hy-2].h[0])||(!freeze[hr+1][hc+hy-1].h[0])) { 397 if(freeze[hr][hc+hy-1].h[0]&&CornerCount) { 398 CornerCount=0; 399 DoorPos=rightup; 400 } 401 if((!freeze[hr][hc+hy-1].h[0])&&CornerCount>1) { 402 CornerCount=1; 403 DoorPos=rightup; 404 } 405 } 406 if((!freeze[hr][hc+hy-2].h[1])||(!freeze[hr+1][hc+hy-1].h[1])) { 407 if(freeze[hr][hc+hy-1].h[1]&&CornerCount) { 408 CornerCount=0; 409 DoorPos=rightup; 410 } 411 if((!freeze[hr][hc+hy-1].h[1])&&CornerCount>1) { 412 CornerCount=1; 413 DoorPos=rightup; 414 } 415 } 416 if((!freeze[hr+hx-1][hc+hy-2].h[0])||(!freeze[hr+hx-2][hc+hy-1].h[0])) { 417 if(freeze[hr+hx-1][hc+hy-1].h[0]&&CornerCount) { 418 CornerCount=0; 419 DoorPos=rightdown; 420 } 421 if((!freeze[hr+hx-1][hc+hy-1].h[0])&&CornerCount>1) { 422 CornerCount=1; 423 DoorPos=rightdown; 424 } 425 } 426 if((!freeze[hr+hx-1][hc+hy-2].h[1])||(!freeze[hr+hx-2][hc+hy-1].h[1])) { 427 if(freeze[hr+hx-1][hc+hy-1].h[1]&&CornerCount) { 428 CornerCount=0; 429 DoorPos=rightdown; 430 } 431 if((!freeze[hr+hx-1][hc+hy-1].h[1])&&CornerCount>1) { 432 CornerCount=1; 433 DoorPos=rightdown; 434 } 435 } 436 } 437 if(havedoor)hole-=2; 438 else if(CanMakeADoor) { 439 hole--; 440 preplus=1; 441 if(DoorMakeSide) { 442 hole+=CornerCount; 443 if(CornerCount) { 444 switch(DoorPos) { 445 case leftup: 446 if((!freeze[hr+1][hc].h[0])||(!freeze[hr][hc+1].h[0]))freeze[hr][hc].h[0]=true; 447 if((!freeze[hr+1][hc].h[1])||(!freeze[hr][hc+1].h[1]))freeze[hr][hc].h[1]=true; 448 break; 449 case leftdown: 450 if((!freeze[hr+hx-2][hc].h[0])||(!freeze[hr+hx-1][hc+1].h[0]))freeze[hr+hx-1][hc].h[0]=true; 451 if((!freeze[hr+hx-2][hc].h[1])||(!freeze[hr+hx-1][hc+1].h[1]))freeze[hr+hx-1][hc].h[1]=true; 452 break; 453 case rightup: 454 if((!freeze[hr+1][hc+hy-1].h[0])||(!freeze[hr][hc+hy-2].h[0]))freeze[hr][hc+hy-1].h[0]=true; 455 if((!freeze[hr+1][hc+hy-1].h[1])||(!freeze[hr][hc+hy-2].h[1]))freeze[hr][hc+hy-1].h[1]=true; 456 break; 457 default: 458 if((!freeze[hr+hx-2][hc+hy-1].h[0])||(!freeze[hr+hx-1][hc+hy-2].h[0]))freeze[hr+hx-1][hc+hy-1].h[0]=true; 459 if((!freeze[hr+hx-2][hc+hy-1].h[0])||(!freeze[hr+hx-1][hc+hy-2].h[0]))freeze[hr+hx-1][hc+hy-1].h[1]=true; 460 } 461 } 462 } 463 } else preplus=2; 464 if(ans2_2<hole) { 465 cout<<"SORRY CIRNO,NOT ENOUGH ICE_BLOCKS TO FIX THE WALL\n"; 466 return 0; 467 } 468 ans2_2+=preplus; 469 cout<<"GOOD JOB CIRNO,SUCCESSFULLY BUILT THE HOUSE\n"; 470 if(havedoor)cout<<"DOOR IS OK\n"; 471 else cout<<"HOUSE HAS NO DOOR\n"; 472 if(hole)cout<<"WALL NEED TO BE FIXED\n"; 473 else cout<<"WALL IS OK\n"; 474 for(register int k=0; k<=high; k++) { 475 if(!freeze[hr][hc].h[k]){ 476 breakcorner=true; 477 hole++; 478 } 479 if(!freeze[hr][hc+hy-1].h[k]){ 480 breakcorner=true; 481 hole++; 482 } 483 if(!freeze[hr+hx-1][hc].h[k]){ 484 breakcorner=true; 485 hole++; 486 } 487 if(!freeze[hr+hx-1][hc+hy-1].h[k]){ 488 breakcorner=true; 489 hole++; 490 } 491 } 492 ans2_2-=hole; 493 if(ans2_2<0)ans2_2=0; 494 if(breakcorner)cout<<"CORNER NEED TO BE FIXED\n"; 495 else cout<<"CORNER IS OK\n"; 496 cout<<"CIRNO FINALLY HAS "<<ans2_2<<" ICE_BLOCK(S)\n"; 497 if(hole==0&&k1==0&&k2==0&&DoorinMiddle&&havedoor)cout<<"CIRNO IS PERFECT!"; 498 } 499 } 500 }
- [Luogu P2586 杀蚂蚁]
1 #include"cstdio" 2 #include"cmath" 3 #include"vector" 4 #include"cstdlib" 5 #define east 1 6 #define south 2 7 #define west 3 8 #define north 4 9 #define eps 1e-7 10 using namespace std; 11 struct Ant{ 12 int borntime,hp,bornhp,fromx,fromy,posx,posy,level,old; 13 bool havecake; 14 }newant; 15 vector<Ant>ant; 16 vector<int>aim; 17 struct node{ 18 int info; 19 bool haveant,haveweapon; 20 }mp[9][9]; 21 int maxinfo,goingpos,n,m,s,d,r,t,totant,cnttime,cnt,target,x[21],y[21],wayx,wayy,AA,BB,CC,AA1,BB1,CC1,tmp,ftremmin; 22 double xxx,yyy; 23 inline double dis2(double x1,double y1,double x2,double y2){ 24 return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); 25 } 26 inline void FindWay(int antnum){ 27 maxinfo=-1; 28 wayx=ant[antnum].posx; 29 wayy=ant[antnum].posy; 30 if(ant[antnum].posy<m&&ant[antnum].fromy!=ant[antnum].posy+1&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveweapon)&&mp[ant[antnum].posx][ant[antnum].posy+1].info>maxinfo){ 31 maxinfo=mp[ant[antnum].posx][ant[antnum].posy+1].info; 32 wayx=ant[antnum].posx; 33 wayy=ant[antnum].posy+1; 34 goingpos=east; 35 } 36 if(ant[antnum].posx<n&&ant[antnum].fromx!=ant[antnum].posx+1&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveweapon)&&mp[ant[antnum].posx+1][ant[antnum].posy].info>maxinfo){ 37 maxinfo=mp[ant[antnum].posx+1][ant[antnum].posy].info; 38 wayx=ant[antnum].posx+1; 39 wayy=ant[antnum].posy; 40 goingpos=south; 41 } 42 if(ant[antnum].posy&&ant[antnum].fromy!=ant[antnum].posy-1&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveweapon)&&mp[ant[antnum].posx][ant[antnum].posy-1].info>maxinfo){ 43 maxinfo=mp[ant[antnum].posx][ant[antnum].posy-1].info; 44 wayx=ant[antnum].posx; 45 wayy=ant[antnum].posy-1; 46 goingpos=west; 47 } 48 if(ant[antnum].posx&&ant[antnum].fromx!=ant[antnum].posx-1&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveweapon)&&mp[ant[antnum].posx-1][ant[antnum].posy].info>maxinfo){ 49 maxinfo=mp[ant[antnum].posx-1][ant[antnum].posy].info; 50 wayx=ant[antnum].posx-1; 51 wayy=ant[antnum].posy; 52 goingpos=north; 53 } 54 if((ant[antnum].old+1)%5==0){ 55 if(goingpos==east){ 56 if(ant[antnum].posx&&ant[antnum].fromx!=ant[antnum].posx-1&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveweapon)){ 57 wayx=ant[antnum].posx-1; 58 wayy=ant[antnum].posy; 59 }else if(ant[antnum].posy&&ant[antnum].fromy!=ant[antnum].posy-1&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveweapon)){ 60 wayx=ant[antnum].posx; 61 wayy=ant[antnum].posy-1; 62 }else if(ant[antnum].posx<n&&ant[antnum].fromx!=ant[antnum].posx+1&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveweapon)){ 63 wayx=ant[antnum].posx+1; 64 wayy=ant[antnum].posy; 65 } 66 }else if(goingpos==south){ 67 if(ant[antnum].posy<m&&ant[antnum].fromy!=ant[antnum].posy+1&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveweapon)){ 68 wayx=ant[antnum].posx; 69 wayy=ant[antnum].posy+1; 70 }else if(ant[antnum].posx&&ant[antnum].fromx!=ant[antnum].posx-1&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveweapon)){ 71 wayx=ant[antnum].posx-1; 72 wayy=ant[antnum].posy; 73 }else if(ant[antnum].posy&&ant[antnum].fromy!=ant[antnum].posy-1&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveweapon)){ 74 wayx=ant[antnum].posx; 75 wayy=ant[antnum].posy-1; 76 } 77 }else if(goingpos==west){ 78 if(ant[antnum].posx<n&&ant[antnum].fromx!=ant[antnum].posx+1&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveweapon)){ 79 wayx=ant[antnum].posx+1; 80 wayy=ant[antnum].posy; 81 }else if(ant[antnum].posy<m&&ant[antnum].fromy!=ant[antnum].posy+1&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveweapon)){ 82 wayx=ant[antnum].posx; 83 wayy=ant[antnum].posy+1; 84 }else if(ant[antnum].posx&&ant[antnum].fromx!=ant[antnum].posx-1&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx-1][ant[antnum].posy].haveweapon)){ 85 wayx=ant[antnum].posx-1; 86 wayy=ant[antnum].posy; 87 } 88 }else{ 89 if(ant[antnum].posy&&ant[antnum].fromy!=ant[antnum].posy-1&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy-1].haveweapon)){ 90 wayx=ant[antnum].posx; 91 wayy=ant[antnum].posy-1; 92 }else if(ant[antnum].posx<n&&ant[antnum].fromx!=ant[antnum].posx+1&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveant)&&(!mp[ant[antnum].posx+1][ant[antnum].posy].haveweapon)){ 93 wayx=ant[antnum].posx+1; 94 wayy=ant[antnum].posy; 95 }else if(ant[antnum].posy<m&&ant[antnum].fromy!=ant[antnum].posy+1&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveant)&&(!mp[ant[antnum].posx][ant[antnum].posy+1].haveweapon)){ 96 wayx=ant[antnum].posx; 97 wayy=ant[antnum].posy+1; 98 } 99 } 100 } 101 } 102 inline void FindTarget(int weaponnum){ 103 if(target!=-1&&dis2(ant[target].posx,ant[target].posy,x[weaponnum],y[weaponnum])<=r*r){ 104 aim.push_back(target); 105 AA=y[weaponnum]-ant[target].posy; 106 BB=ant[target].posx-x[weaponnum]; 107 CC=(ant[target].posy-y[weaponnum])*ant[target].posx-(ant[target].posx-x[weaponnum])*ant[target].posy; 108 for(register int i=0;i<ant.size();i++){ 109 if(i==target)continue; 110 AA1=BB; 111 BB1=-AA; 112 CC1=AA*ant[i].posy-BB*ant[i].posx; 113 xxx=(CC*BB1-CC1*BB*1.0)/(AA1*BB-AA*BB1); 114 yyy=(AA*CC1-AA1*CC*1.0)/(AA1*BB-AA*BB1); 115 if(((xxx>=ant[target].posx&&xxx<=x[weaponnum])||(xxx<=ant[target].posx&&xxx>=x[weaponnum]))&&((yyy>=ant[target].posy&&yyy<=y[weaponnum])||(yyy<=ant[target].posy&&yyy>=y[weaponnum]))&&(dis2(xxx,yyy,ant[i].posx,ant[i].posy)-0.25<=eps)) 116 aim.push_back(i); 117 } 118 }else{ 119 ftremmin=20021115; 120 tmp=-1; 121 for(register int i=0;i<ant.size();i++){ 122 if(dis2(ant[i].posx,ant[i].posy,x[weaponnum],y[weaponnum])<ftremmin&&dis2(ant[i].posx,ant[i].posy,x[weaponnum],y[weaponnum])<=r*r){ 123 ftremmin=dis2(ant[i].posx,ant[i].posy,x[weaponnum],y[weaponnum]); 124 tmp=i; 125 } 126 } 127 if(tmp>=0)aim.push_back(tmp); 128 } 129 } 130 inline void Print_Result(){ 131 printf("%d\n",ant.size()); 132 for(register int i=0;i<ant.size();i++)printf("%d %d %d %d %d\n",ant[i].old,ant[i].level,ant[i].hp,ant[i].posx,ant[i].posy); 133 exit(0); 134 } 135 int main(){ 136 scanf("%d%d%d%d%d",&n,&m,&s,&d,&r); 137 for(register int i=1;i<=s;i++){ 138 scanf("%d%d",&x[i],&y[i]); 139 mp[x[i]][y[i]].haveweapon=true; 140 } 141 scanf("%d",&t); 142 while(++cnttime<=t){ 143 if(ant.size()<6&&!mp[0][0].haveant){ 144 ++totant; 145 newant.level=ceil(totant/6.0); 146 newant.bornhp=newant.hp=4*pow(1.1,newant.level); 147 newant.borntime=cnttime; 148 ant.push_back(newant); 149 mp[0][0].haveant=true; 150 } 151 for(register int i=0;i<ant.size();i++){ 152 if(ant[i].havecake)mp[ant[i].posx][ant[i].posy].info+=5; 153 else mp[ant[i].posx][ant[i].posy].info+=2; 154 } 155 for(register int i=0;i<ant.size();i++){ 156 FindWay(i); 157 mp[ant[i].posx][ant[i].posy].haveant=false; 158 ant[i].fromx=ant[i].posx; 159 ant[i].fromy=ant[i].posy; 160 ant[i].posx=wayx; 161 ant[i].posy=wayy; 162 mp[wayx][wayy].haveant=true; 163 } 164 target=-1; 165 for(register int i=0;i<ant.size();i++){ 166 if(ant[i].havecake){ 167 target=i; 168 break; 169 } 170 } 171 if(target==-1){ 172 for(register int i=0;i<ant.size();i++){ 173 if(ant[i].posx==n&&ant[i].posy==m){ 174 ant[i].havecake=true; 175 ant[i].hp=min(ant[i].bornhp,ant[i].hp+(ant[i].bornhp>>1)); 176 target=i; 177 break; 178 } 179 } 180 } 181 for(register int i=1;i<=s;i++){ 182 aim.clear(); 183 FindTarget(i); 184 for(register int j=0;j<aim.size();j++)ant[aim[j]].hp-=d; 185 } 186 for(register int i=0;i<ant.size();i++){ 187 if(ant[i].hp<0){ 188 mp[ant[i].posx][ant[i].posy].haveant=false; 189 if(ant[i].havecake)target=-1; 190 ant.erase(ant.begin()+i); 191 i=-1; 192 } 193 } 194 for(register int i=0;i<ant.size();i++){ 195 if(ant[i].havecake){ 196 if((!ant[i].posx)&&(!ant[i].posy)){ 197 printf("Game over after %d seconds\n",cnttime); 198 Print_Result(); 199 } 200 break; 201 } 202 } 203 for(register int i=0;i<=n;i++)for(register int j=0;j<=m;j++)if(mp[i][j].info)mp[i][j].info--; 204 for(register int i=0;i<ant.size();i++)ant[i].old++; 205 } 206 printf("The game is going on\n"); 207 Print_Result(); 208 }
- [Luogu P5380 鸭棋]
1 #include<cstdio> 2 #define none 0 3 #define blue 1 4 #define red 2 5 #define soldier 1 6 #define duck 2 7 #define car 3 8 #define horse 4 9 #define elephant 5 10 #define guard 6 11 #define captain 7 12 inline int read(){ 13 char ch=getchar(); 14 int o=0; 15 while(ch<'0'||ch>'9')ch=getchar(); 16 while(ch>='0'&&ch<='9'){ 17 o=(o<<3)+(o<<1)+(ch^48); 18 ch=getchar(); 19 } 20 return o; 21 } 22 struct node{ 23 int kind,color; 24 }a[10][9]; 25 int q,x1,x2,y1,y2,rx,ry,bx,by; 26 bool GameOver,redturn; 27 inline void PrintInfo(int xx,int yy){ 28 if(a[xx][yy].color==red)printf("red "); 29 else printf("blue "); 30 if(a[xx][yy].kind==soldier)printf("soldier;"); 31 else if(a[xx][yy].kind==captain)printf("captain;"); 32 else if(a[xx][yy].kind==elephant)printf("elephant;"); 33 else if(a[xx][yy].kind==guard)printf("guard;"); 34 else if(a[xx][yy].kind==horse)printf("horse;"); 35 else if(a[xx][yy].kind==car)printf("car;"); 36 else printf("duck;"); 37 } 38 inline bool Captain(int px1,int py1,int px2,int py2){ 39 return((px2==px1+1||px2==px1-1)&&py1==py2)||(px1==px2&&(py2==py1+1||py1==py2+1)); 40 } 41 inline bool Guard(int px1,int py1,int px2,int py2){ 42 return((px2==px1+1||px2==px1-1)&&(py1==py2+1||py1==py2-1))||((px2==px1+1||px2==px1-1)&&(py2==py1+1||py1==py2+1)); 43 } 44 inline bool Elephant(int px1,int py1,int px2,int py2){ 45 if(px2==px1+2&&py2==py1-2)return !a[px1+1][py1-1].kind; 46 if(px2==px1+2&&py2==py1+2)return !a[px1+1][py1+1].kind; 47 if(px2==px1-2&&py2==py1-2)return !a[px1-1][py1-1].kind; 48 if(px2==px1-2&&py2==py1+2)return !a[px1-1][py1+1].kind; 49 return false; 50 } 51 inline bool Horse(int px1,int py1,int px2,int py2){ 52 if(px2==px1+2&&(py2==py1-1||py2==py1+1))return !a[px1+1][py1].kind; 53 if(px2==px1-2&&(py2==py1-1||py2==py1+1))return !a[px1-1][py1].kind; 54 if((px2==px1+1||px2==px1-1)&&py2==py1-2)return !a[px1][py1-1].kind; 55 if((px2==px1+1||px2==px1-1)&&py2==py1+2)return !a[px1][py1+1].kind; 56 return false; 57 } 58 inline bool Car(int px1,int py1,int px2,int py2){ 59 if(px1==px2){ 60 for(register int j=(py2<py1?py2:py1)+1;j<(py2<py1?py1:py2);j++)if(a[px1][j].kind)return false; 61 return true; 62 } 63 if(py1==py2){ 64 for(register int i=(px2<px1?px2:px1)+1;i<(px2<px1?px1:px2);i++)if(a[i][py1].kind)return false; 65 return true; 66 } 67 return false; 68 } 69 inline bool Duck(int px1,int py1,int px2,int py2){ 70 if(px2==px1+3&&py2==py1+2)return(!a[px1+2][py1+1].kind)&&(!a[px1+1][py1].kind); 71 if(px2==px1+3&&py2==py1-2)return(!a[px1+2][py1-1].kind)&&(!a[px1+1][py1].kind); 72 if(px2==px1-3&&py2==py1+2)return(!a[px1-2][py1+1].kind)&&(!a[px1-1][py1].kind); 73 if(px2==px1-3&&py2==py1-2)return(!a[px1-2][py1-1].kind)&&(!a[px1-1][py1].kind); 74 if(px2==px1+2&&py2==py1+3)return(!a[px1+1][py1+2].kind)&&(!a[px1][py1+1].kind); 75 if(px2==px1+2&&py2==py1-3)return(!a[px1+1][py1-2].kind)&&(!a[px1][py1-1].kind); 76 if(px2==px1-2&&py2==py1+3)return(!a[px1-1][py1+2].kind)&&(!a[px1][py1+1].kind); 77 if(px2==px1-2&&py2==py1-3)return(!a[px1-1][py1-2].kind)&&(!a[px1][py1-1].kind); 78 return false; 79 } 80 inline bool Soldier(int px1,int py1,int px2,int py2){ 81 return((px2==px1+1&&(py2==py1-1||py2==py1||py2==py1+1))||(px2==px1&&(py2==py1-1||py2==py1+1))||(px2==px1-1&&(py2==py1-1||py2==py1||py2==py1+1))); 82 } 83 inline bool JiangJun(){ 84 for(register int i=0;i<=9;i++){ 85 for(register int j=0;j<=8;j++){ 86 if(a[i][j].color==red){ 87 switch(a[i][j].kind){ 88 case captain: 89 if(Captain(i,j,bx,by))return true; 90 break; 91 case guard: 92 if(Guard(i,j,bx,by))return true; 93 break; 94 case elephant: 95 if(Elephant(i,j,bx,by))return true; 96 break; 97 case horse: 98 if(Horse(i,j,bx,by))return true; 99 break; 100 case car: 101 if(Car(i,j,bx,by))return true; 102 break; 103 case duck: 104 if(Duck(i,j,bx,by))return true; 105 break; 106 default: 107 if(Soldier(i,j,bx,by))return true; 108 break; 109 } 110 }else if(a[i][j].color==blue){ 111 switch(a[i][j].kind){ 112 case captain: 113 if(Captain(i,j,rx,ry))return true; 114 break; 115 case guard: 116 if(Guard(i,j,rx,ry))return true; 117 break; 118 case elephant: 119 if(Elephant(i,j,rx,ry))return true; 120 break; 121 case horse: 122 if(Horse(i,j,rx,ry))return true; 123 break; 124 case car: 125 if(Car(i,j,rx,ry))return true; 126 break; 127 case duck: 128 if(Duck(i,j,rx,ry))return true; 129 break; 130 default: 131 if(Soldier(i,j,rx,ry))return true; 132 break; 133 } 134 } 135 } 136 } 137 return false; 138 } 139 int main(){ 140 redturn=true; 141 rx=0; 142 ry=4; 143 bx=9; 144 by=4; 145 a[0][0].color=red; 146 a[0][0].kind=car; 147 a[0][1].color=red; 148 a[0][1].kind=horse; 149 a[0][2].color=red; 150 a[0][2].kind=elephant; 151 a[0][3].color=red; 152 a[0][3].kind=guard; 153 a[0][4].color=red; 154 a[0][4].kind=captain; 155 a[0][5].color=red; 156 a[0][5].kind=guard; 157 a[0][6].color=red; 158 a[0][6].kind=elephant; 159 a[0][7].color=red; 160 a[0][7].kind=horse; 161 a[0][8].color=red; 162 a[0][8].kind=car; 163 a[2][0].color=red; 164 a[2][0].kind=duck; 165 a[2][8].color=red; 166 a[2][8].kind=duck; 167 a[3][0].color=red; 168 a[3][0].kind=soldier; 169 a[3][2].color=red; 170 a[3][2].kind=soldier; 171 a[3][4].color=red; 172 a[3][4].kind=soldier; 173 a[3][6].color=red; 174 a[3][6].kind=soldier; 175 a[3][8].color=red; 176 a[3][8].kind=soldier; 177 a[9][0].color=blue; 178 a[9][0].kind=car; 179 a[9][1].color=blue; 180 a[9][1].kind=horse; 181 a[9][2].color=blue; 182 a[9][2].kind=elephant; 183 a[9][3].color=blue; 184 a[9][3].kind=guard; 185 a[9][4].color=blue; 186 a[9][4].kind=captain; 187 a[9][5].color=blue; 188 a[9][5].kind=guard; 189 a[9][6].color=blue; 190 a[9][6].kind=elephant; 191 a[9][7].color=blue; 192 a[9][7].kind=horse; 193 a[9][8].color=blue; 194 a[9][8].kind=car; 195 a[7][0].color=blue; 196 a[7][0].kind=duck; 197 a[7][8].color=blue; 198 a[7][8].kind=duck; 199 a[6][0].color=blue; 200 a[6][0].kind=soldier; 201 a[6][2].color=blue; 202 a[6][2].kind=soldier; 203 a[6][4].color=blue; 204 a[6][4].kind=soldier; 205 a[6][6].color=blue; 206 a[6][6].kind=soldier; 207 a[6][8].color=blue; 208 a[6][8].kind=soldier; 209 q=read(); 210 for(register int i=1;i<=q;i++){ 211 if(GameOver){ 212 puts("Invalid command"); 213 continue; 214 } 215 x1=read(); 216 y1=read(); 217 x2=read(); 218 y2=read(); 219 if(redturn&&a[x1][y1].color==blue){ 220 puts("Invalid command"); 221 continue; 222 } 223 if((!redturn)&&a[x1][y1].color==red){ 224 puts("Invalid command"); 225 continue; 226 } 227 switch(a[x1][y1].kind){ 228 case captain: 229 if(Captain(x1,y1,x2,y2)){ 230 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 231 else{ 232 redturn^=1; 233 PrintInfo(x1,y1); 234 if(a[x2][y2].kind){ 235 PrintInfo(x2,y2); 236 if(a[x2][y2].kind==captain)GameOver=true; 237 }else printf("NA;"); 238 a[x2][y2]=a[x1][y1]; 239 a[x1][y1].color=a[x1][y1].kind=none; 240 if(a[x2][y2].color==red){ 241 rx=x2; 242 ry=y2; 243 }else{ 244 bx=x2; 245 by=y2; 246 } 247 if(JiangJun())printf("yes;"); 248 else printf("no;"); 249 if(GameOver)puts("yes"); 250 else puts("no"); 251 } 252 }else puts("Invalid command"); 253 break; 254 case guard: 255 if(Guard(x1,y1,x2,y2)){ 256 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 257 else { 258 redturn^=1; 259 PrintInfo(x1,y1); 260 if(a[x2][y2].kind){ 261 PrintInfo(x2,y2); 262 if(a[x2][y2].kind==captain)GameOver=true; 263 }else printf("NA;"); 264 a[x2][y2]=a[x1][y1]; 265 a[x1][y1].color=a[x1][y1].kind=none; 266 if(JiangJun())printf("yes;"); 267 else printf("no;"); 268 if(GameOver)puts("yes"); 269 else puts("no"); 270 } 271 }else puts("Invalid command"); 272 break; 273 case elephant: 274 if(Elephant(x1,y1,x2,y2)){ 275 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 276 else{ 277 redturn^=1; 278 PrintInfo(x1,y1); 279 if(a[x2][y2].kind){ 280 PrintInfo(x2,y2); 281 if(a[x2][y2].kind==captain)GameOver=true; 282 }else printf("NA;"); 283 a[x2][y2]=a[x1][y1]; 284 a[x1][y1].color=a[x1][y1].kind=none; 285 if(JiangJun())printf("yes;"); 286 else printf("no;"); 287 if(GameOver)puts("yes"); 288 else puts("no"); 289 } 290 }else puts("Invalid command"); 291 break; 292 case horse: 293 if(Horse(x1,y1,x2,y2)){ 294 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 295 else{ 296 redturn^=1; 297 PrintInfo(x1,y1); 298 if(a[x2][y2].kind){ 299 PrintInfo(x2,y2); 300 if(a[x2][y2].kind==captain)GameOver=true; 301 }else printf("NA;"); 302 a[x2][y2]=a[x1][y1]; 303 a[x1][y1].color=a[x1][y1].kind=none; 304 if(JiangJun())printf("yes;"); 305 else printf("no;"); 306 if(GameOver)puts("yes"); 307 else puts("no"); 308 } 309 }else puts("Invalid command"); 310 break; 311 case car: 312 if(Car(x1,y1,x2,y2)){ 313 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 314 else{ 315 redturn^=1; 316 PrintInfo(x1,y1); 317 if(a[x2][y2].kind){ 318 PrintInfo(x2,y2); 319 if(a[x2][y2].kind==captain)GameOver=true; 320 }else printf("NA;"); 321 a[x2][y2]=a[x1][y1]; 322 a[x1][y1].color=a[x1][y1].kind=none; 323 if(JiangJun())printf("yes;"); 324 else printf("no;"); 325 if(GameOver)puts("yes"); 326 else puts("no"); 327 } 328 }else puts("Invalid command"); 329 break; 330 case duck: 331 if(Duck(x1,y1,x2,y2)){ 332 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 333 else{ 334 redturn^=1; 335 PrintInfo(x1,y1); 336 if(a[x2][y2].kind){ 337 PrintInfo(x2,y2); 338 if(a[x2][y2].kind==captain)GameOver=true; 339 }else printf("NA;"); 340 a[x2][y2]=a[x1][y1]; 341 a[x1][y1].color=a[x1][y1].kind=none; 342 if(JiangJun())printf("yes;"); 343 else printf("no;"); 344 if(GameOver)puts("yes"); 345 else puts("no"); 346 } 347 }else puts("Invalid command"); 348 break; 349 case soldier: 350 if(Soldier(x1,y1,x2,y2)){ 351 if(a[x1][y1].color==a[x2][y2].color)puts("Invalid command"); 352 else{ 353 redturn^=1; 354 PrintInfo(x1,y1); 355 if(a[x2][y2].kind){ 356 PrintInfo(x2,y2); 357 if(a[x2][y2].kind==captain)GameOver=true; 358 }else printf("NA;"); 359 a[x2][y2]=a[x1][y1]; 360 a[x1][y1].color=a[x1][y1].kind=none; 361 if(JiangJun())printf("yes;"); 362 else printf("no;"); 363 if(GameOver)puts("yes"); 364 else puts("no"); 365 } 366 }else puts("Invalid command"); 367 break; 368 default: 369 puts("Invalid command"); 370 break; 371 } 372 } 373 }
- [Luogu P3585 [POI2015]PIE]
1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 using namespace std; 5 char a[1001][1001],ch; 6 int q,n,m,ox,oy,aa,bb; 7 struct node{ 8 int xx,yy; 9 }; 10 vector<node>v; 11 inline node makenode(int xx,int yy){ 12 node tmp; 13 tmp.xx=xx; 14 tmp.yy=yy; 15 return tmp; 16 } 17 bool WA,f[1001][1001]; 18 int main(){ 19 scanf("%d",&q); 20 while(q--){ 21 memset(f,false,sizeof(f)); 22 WA=false; 23 scanf("%d%d%d%d\n",&n,&m,&aa,&bb); 24 ox=-1; 25 oy=-1; 26 for(register int i=1;i<=n;i++){ 27 for(register int j=1;j<=m;j++)scanf("%c",&a[i][j]); 28 scanf("\n"); 29 } 30 v.clear(); 31 for(register int i=1;i<=aa;i++){ 32 for(register int j=1;j<=bb;j++){ 33 scanf("%c",&ch); 34 if(ch=='x'){ 35 if(ox==-1){ 36 ox=i; 37 oy=j; 38 v.push_back(makenode(0,0)); 39 } 40 else v.push_back(makenode(i-ox,j-oy)); 41 } 42 } 43 scanf("\n"); 44 } 45 // for(register int i=0;i<v.size();i++)printf("FF:%d %d\n",v[i].xx,v[i].yy); 46 for(register int i=1;i<=n;i++){ 47 for(register int j=1;j<=m;j++){ 48 if(a[i][j]=='x'&&!f[i][j]){ 49 for(register int k=0;k<v.size();k++){ 50 int tx=i+v[k].xx; 51 int ty=j+v[k].yy; 52 // printf("%d %d\n",tx,ty); 53 if(tx>n||tx<=0||ty>m||ty<=0||a[tx][ty]=='.'||f[tx][ty]){ 54 puts("NIE"); 55 WA=true; 56 break; 57 } 58 f[i+v[k].xx][j+v[k].yy]=true; 59 } 60 if(WA)break; 61 } 62 } 63 if(WA)break; 64 } 65 if(!WA)puts("TAK"); 66 } 67 }
贪心
- [Luogu P2672 推销员]
1 #include<cstdio> 2 #include<queue> 3 using namespace std; 4 priority_queue<pair<int,int> >ql,qr; 5 inline int read(){ 6 char ch=getchar(); 7 int o=0; 8 while(ch<'0'||ch>'9')ch=getchar(); 9 while(ch>='0'&&ch<='9'){ 10 o=(o<<3)+(o<<1)+(ch^48); 11 ch=getchar(); 12 } 13 return o; 14 } 15 inline void write(int xx){ 16 if(xx>9)write(xx/10); 17 putchar(xx%10^48); 18 } 19 int n,ans,pos[100001],far,lb,rb,tir[100001]; 20 int main(){ 21 n=read(); 22 for(register int i=1;i<=n;i++)pos[i]=read(); 23 for(register int i=1;i<=n;i++){ 24 tir[i]=read(); 25 if(ans<(pos[i]<<1)+tir[i]){ 26 far=i; 27 ans=(pos[i]<<1)+tir[i]; 28 } 29 } 30 write(ans); 31 putchar('\n'); 32 for(register int i=1;i<far;i++)ql.push(make_pair(tir[i],i)); 33 for(register int i=far+1;i<=n;i++)qr.push(make_pair((pos[i]<<1)+tir[i],i)); 34 for(register int i=2;i<=n;i++){ 35 if(!ql.empty())lb=ql.top().second; 36 while((!qr.empty())&&qr.top().second<far)qr.pop(); 37 if(!qr.empty())rb=qr.top().second; 38 if(tir[lb]<(pos[rb]<<1)-(pos[far]<<1)+tir[rb]){ 39 qr.pop(); 40 ans+=(pos[rb]<<1)-(pos[far]<<1)+tir[rb]; 41 for(register int i=far+1;i<rb;i++)ql.push(make_pair(tir[i],i)); 42 far=rb; 43 }else{ 44 ans+=tir[lb]; 45 ql.pop(); 46 } 47 write(ans); 48 putchar('\n'); 49 } 50 }
- [Luogu P1717 钓鱼]
1 #include<cstdio> 2 #include<queue> 3 #include<algorithm> 4 using namespace std; 5 int n,t,ans,tmp,b[100]; 6 struct node { 7 int x,y; 8 } a[101],p; 9 priority_queue<node>q; 10 bool operator <(node x,node y) { 11 return x.x<y.x; 12 } 13 int main() { 14 scanf("%d%d",&n,&t); 15 t*=12; 16 for(int i=1; i<=n; i++)scanf("%d",&a[i].x); 17 for(int i=1; i<=n; i++)scanf("%d",&a[i].y); 18 for(int i=1; i<n; i++)scanf("%d",&b[i]); 19 for(int i=1; i<=n; i++) { 20 t-=b[i-1]; 21 tmp=0; 22 while(!q.empty())q.pop(); 23 for(int j=1; j<=i; j++)q.push(a[j]); 24 for(int j=1; j<=t; j++) { 25 p=q.top(); 26 q.pop(); 27 if(p.x>0)tmp+=p.x; 28 else break; 29 p.x-=p.y; 30 q.push(p); 31 } 32 ans=max(ans,tmp); 33 } 34 printf("%d",ans); 35 }
- [Luogu P1090 合并果子]
1 #include<bits/stdc++.h> 2 using namespace std; 3 priority_queue<int,vector<int>,greater<int> >minq; 4 int n,a,s; 5 int main(){ 6 scanf("%d",&n); 7 for(int i=1;i<=n;i++){ 8 scanf("%d",&a); 9 minq.push(a); 10 } 11 while(minq.size()>1){ 12 int t=0; 13 s+=minq.top(); 14 t+=minq.top(); 15 minq.pop(); 16 s+=minq.top(); 17 t+=minq.top(); 18 minq.pop(); 19 minq.push(t); 20 } 21 printf("%d",s); 22 }
树状数组
- [Luogu P3374 【模板】树状数组 1]
1 #include<cstdio> 2 #define lowbit(x) ((x)&(-x)) 3 int n,m,x,y,tree[500001]; 4 inline int read(){ 5 char ch=getchar(); 6 int o=0; 7 bool sign=false; 8 while(ch<'0'||ch>'9'){ 9 if(ch=='-')sign=true; 10 ch=getchar(); 11 } 12 while(ch>='0'&&ch<='9'){ 13 o=(o<<3)+(o<<1)+(ch^48); 14 ch=getchar(); 15 } 16 return sign?-o:o; 17 } 18 inline void write(int xx){ 19 if(xx<0){ 20 putchar('-'); 21 xx=-xx; 22 } 23 if(xx>9)write(xx/10); 24 putchar(xx%10^48); 25 } 26 inline void change(int _begin,int _val){ 27 for(register int i=_begin;i<=n;i+=lowbit(i))tree[i]+=_val; 28 } 29 inline int query(int _end){ 30 register int ret=0; 31 for(register int i=_end;i;i-=lowbit(i))ret+=tree[i]; 32 return ret; 33 } 34 int main(){ 35 n=read(); 36 m=read(); 37 for(register int i=1;i<=n;i++)change(i,read()); 38 for(register int i=1;i<=m;i++){ 39 if(read()==1){ 40 x=read(); 41 y=read(); 42 change(x,y); 43 }else{ 44 x=read(); 45 y=read(); 46 write(query(y)-query(x-1)); 47 putchar('\n'); 48 } 49 } 50 }
- [Luogu P3368 【模板】树状数组 2]
1 #include<cstdio> 2 #define lowbit(x) ((x)&(-(x))) 3 inline int read(){ 4 char ch=getchar(); 5 int o=0; 6 bool sign=false; 7 while(ch<'0'||ch>'9'){ 8 if(ch=='-')sign=true; 9 ch=getchar(); 10 } 11 while(ch>='0'&&ch<='9'){ 12 o=(o<<3)+(o<<1)+(ch^48); 13 ch=getchar(); 14 } 15 return sign?-o:o; 16 } 17 inline void write(int xx){ 18 if(xx<0){ 19 putchar('-'); 20 xx=-xx; 21 } 22 if(xx>9)write(xx/10); 23 putchar(xx%10^48); 24 } 25 int n,m,tree[500001],x,y,z,last; 26 inline void update(int _begin,int _val){ 27 for(register int i=_begin;i<=n;i+=lowbit(i))tree[i]+=_val; 28 } 29 inline int query(int _end){ 30 register int ret=0; 31 for(register int i=_end;i;i-=lowbit(i))ret+=tree[i]; 32 return ret; 33 } 34 int main(){ 35 n=read(); 36 m=read(); 37 for(register int i=1;i<=n;i++){ 38 x=read(); 39 update(i,x-last); 40 last=x; 41 } 42 for(register int i=1;i<=m;i++){ 43 if(read()==1){ 44 x=read(); 45 y=read(); 46 z=read(); 47 update(x,z); 48 update(y+1,-z); 49 }else{ 50 x=read(); 51 write(query(x)); 52 putchar('\n'); 53 } 54 } 55 }
- [Luogu P3353 在你窗外闪耀的星星]
1 #include<cstdio> 2 #include<algorithm> 3 #define lowbit(x) (x&(-x)) 4 using namespace std; 5 inline int read(){ 6 char ch=getchar(); 7 int o=0; 8 while(ch<'0'||ch>'9')ch=getchar(); 9 while(ch>='0'&&ch<='9'){ 10 o=(o<<3)+(o<<1)+(ch^48); 11 ch=getchar(); 12 } 13 return o; 14 } 15 int n,w,x,ans,maxx,tree[100001]; 16 inline void update(int _pos,int _val){ 17 for(register int i=_pos;i<=n;i+=lowbit(i))tree[i]+=_val; 18 } 19 inline int query(int _pos){ 20 register int ret=0; 21 for(register int i=_pos;i;i-=lowbit(i))ret+=tree[i]; 22 return ret; 23 } 24 int main(){ 25 n=read(); 26 w=read(); 27 for(register int i=1;i<=n;i++){ 28 x=read(); 29 if(x>maxx)maxx=x; 30 update(x,read()); 31 } 32 for(register int i=0;i+w<=maxx;i++)ans=max(ans,query(i+w)-query(i)); 33 printf("%d",ans); 34 }
- [Luogu AT2829 転倒数]
1 #include<cstdio> 2 #include<map> 3 #include<algorithm> 4 #define lowbit(x) ((x)&(-(x))) 5 using namespace std; 6 inline int read(){ 7 char ch=getchar(); 8 int o=0; 9 bool sign=false; 10 while(ch<'0'||ch>'9'){ 11 if(ch=='-')sign=true; 12 ch=getchar(); 13 } 14 while(ch>='0'&&ch<='9'){ 15 o=(o<<3)+(o<<1)+(ch^48); 16 ch=getchar(); 17 } 18 return sign?-o:o; 19 } 20 //map<int,int>M; 21 int n,rk,tree[500001],sum[500001]; 22 int M[500001]; 23 long long ans; 24 inline bool cmp(int x,int y) { 25 if(sum[x]==sum[y]) return x>y; 26 return sum[x]>sum[y]; 27 } 28 inline void update(int _begin){ 29 for(register int i=_begin;i<=n;i+=lowbit(i))tree[i]++; 30 } 31 inline int query(int _end){ 32 register int ret=0; 33 for(register int i=_end;i;i-=lowbit(i))ret+=tree[i]; 34 return ret; 35 } 36 int main(){ 37 n=read(); 38 for(register int i=1;i<=n;i++){ 39 sum[i]=read(); 40 M[i]=i; 41 } 42 sort(M+1,M+1+n,cmp); 43 // for(register int i=1;i<=n;i++)if(M.find(sum[i])==M.end())M.insert(make_pair(sum[i],++rk)); 44 for(register int i=1;i<=n;i++){ 45 update(M[i]); 46 ans+=query(M[i]-1); 47 } 48 printf("%lld",ans); 49 }
- [Luogu P1908 逆序对]
1 #include<cstdio> 2 #include<algorithm> 3 #define lowbit(x) ((x)&(-(x))) 4 using namespace std; 5 inline int read(){ 6 char ch=getchar(); 7 int o=0; 8 bool sign=false; 9 while(ch<'0'||ch>'9'){ 10 if(ch=='-')sign=true; 11 ch=getchar(); 12 } 13 while(ch>='0'&&ch<='9'){ 14 o=(o<<3)+(o<<1)+(ch^48); 15 ch=getchar(); 16 } 17 return sign?-o:o; 18 } 19 int n,rk,tree[500001],sum[500001]; 20 int M[500001]; 21 long long ans; 22 inline bool cmp(int x,int y) { 23 if(sum[x]==sum[y]) return x>y; 24 return sum[x]>sum[y]; 25 } 26 inline void update(int _begin){ 27 for(register int i=_begin;i<=n;i+=lowbit(i))tree[i]++; 28 } 29 inline int query(int _end){ 30 register int ret=0; 31 for(register int i=_end;i;i-=lowbit(i))ret+=tree[i]; 32 return ret; 33 } 34 int main(){ 35 n=read(); 36 for(register int i=1;i<=n;i++){ 37 sum[i]=read(); 38 M[i]=i; 39 } 40 sort(M+1,M+1+n,cmp); 41 for(register int i=1;i<=n;i++){ 42 update(M[i]); 43 ans+=query(M[i]-1); 44 } 45 printf("%lld",ans); 46 }
- [Luogu P4939 Agent2]
1 #include<cstdio> 2 #define lowbit(x) ((x)&(-x)) 3 inline int read(){ 4 char ch=getchar(); 5 int o=0; 6 while(ch<'0'||ch>'9')ch=getchar(); 7 while(ch>='0'&&ch<='9'){ 8 o=(o<<3)+(o<<1)+(ch^48); 9 ch=getchar(); 10 } 11 return o; 12 } 13 inline void write(int xx){ 14 if(xx>9)write(xx/10); 15 putchar(xx%10^48); 16 } 17 int n,m,x,tree[10000001]; 18 inline void Update(int xx,int _val){ 19 for(register int i=xx;i<=n;i+=lowbit(i))tree[i]+=_val; 20 } 21 inline int Query(int pos){ 22 register int ret=0; 23 for(register int i=pos;i;i-=lowbit(i))ret+=tree[i]; 24 return ret; 25 } 26 int main(){ 27 n=read(); 28 m=read(); 29 for(register int i=1;i<=m;i++){ 30 if(read()){ 31 write(Query(read())); 32 putchar('\n'); 33 }else{ 34 Update(read(),1); 35 Update(read()+1,-1); 36 } 37 } 38 }
线段树
- [Luogu P3372 【模板】线段树1]
1 #include<cstdio> 2 #define int long long 3 inline int read(){ 4 char ch=getchar(); 5 int o=0; 6 bool sign=false; 7 while(ch<'0'||ch>'9'){ 8 if(ch=='-')sign=true; 9 ch=getchar(); 10 } 11 while(ch>='0'&&ch<='9'){ 12 o=(o<<3)+(o<<1)+(ch^48); 13 ch=getchar(); 14 } 15 return sign?-o:o; 16 } 17 inline void write(int xx){ 18 if(xx<0){ 19 putchar('-'); 20 xx=-xx; 21 } 22 if(xx>9)write(xx/10); 23 putchar(xx%10^48); 24 } 25 int m,n,x,y,a[100001],tree[400001],tag[400001]; 26 void Build(int num,int l,int r){ 27 if(l==r){ 28 tree[num]+=a[l]; 29 return; 30 } 31 Build(num<<1,l,(l+r)>>1); 32 Build((num<<1)+1,((l+r)>>1)+1,r); 33 tree[num]=tree[num<<1]+tree[(num<<1)+1]; 34 } 35 inline void Add(int num,int l,int r,int val){ 36 tag[num]+=val; 37 tree[num]+=(r-l+1)*val; 38 } 39 inline void Push_Down(int num,int l,int r,int val){ 40 Add(num<<1,l,(l+r)>>1,val); 41 Add((num<<1)+1,((l+r)>>1)+1,r,val); 42 tag[num]=0; 43 } 44 void Update(int num,int l,int r,int targetl,int targetr,int val){ 45 if(r<targetl||l>targetr)return; 46 if(l>=targetl&&r<=targetr){ 47 Add(num,l,r,val); 48 return; 49 } 50 if(tag[num])Push_Down(num,l,r,tag[num]); 51 Update(num<<1,l,(l+r)>>1,targetl,targetr,val); 52 Update((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr,val); 53 tree[num]=tree[num<<1]+tree[(num<<1)+1]; 54 } 55 int Query(int num,int l,int r,int targetl,int targetr){ 56 if(r<targetl||l>targetr)return 0; 57 if(r<=targetr&&l>=targetl)return tree[num]; 58 if(tag[num])Push_Down(num,l,r,tag[num]); 59 return Query(num<<1,l,(l+r)>>1,targetl,targetr)+Query((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr);; 60 } 61 signed main(){ 62 n=read(); 63 m=read(); 64 for(register int i=1;i<=n;i++)a[i]=read(); 65 Build(1,1,n); 66 for(register int i=1;i<=m;i++){ 67 if(read()==1){ 68 x=read(); 69 y=read(); 70 Update(1,1,n,x,y,read()); 71 }else{ 72 x=read(); 73 write(Query(1,1,n,x,read())); 74 putchar('\n'); 75 } 76 } 77 }
- [Luogu P3373 【模板】线段树2]
1 #include<cstdio> 2 inline int read(){ 3 char ch=getchar(); 4 int o=0; 5 while(ch<'0'||ch>'9')ch=getchar(); 6 while(ch>='0'&&ch<='9'){ 7 o=(o<<3)+(o<<1)+(ch^48); 8 ch=getchar(); 9 } 10 return o; 11 } 12 inline void write(int xx){ 13 if(xx>9)write(xx/10); 14 putchar(xx%10^48); 15 } 16 int n,m,p,x,y,a[100001]; 17 long long tree[1000001],plus[1000001],mult[1000001]; 18 void Build(int num,int l,int r){ 19 if(l==r){ 20 tree[num]=a[l]%p; 21 return; 22 } 23 Build(num<<1,l,(l+r)>>1); 24 Build((num<<1)+1,((l+r)>>1)+1,r); 25 tree[num]=(tree[num<<1]+tree[(num<<1)+1])%p; 26 } 27 inline void Mult(int num,int l,int r,int _val){ 28 tree[num]=tree[num]*_val%p; 29 mult[num]=mult[num]*_val%p; 30 } 31 inline void Plus(int num,int l,int r,int _val,int extra){ 32 tree[num]=(tree[num]+_val*(r-l+1ll))%p; 33 plus[num]=(plus[num]*extra+_val)%p; 34 } 35 inline void Push_Down(int num,int l,int r){ 36 Mult(num<<1,l,(l+r)>>1,mult[num]); 37 Mult((num<<1)+1,((l+r)>>1)+1,r,mult[num]); 38 Plus(num<<1,l,(l+r)>>1,plus[num],mult[num]); 39 Plus((num<<1)+1,((l+r)>>1)+1,r,plus[num],mult[num]); 40 mult[num]=1; 41 plus[num]=0; 42 } 43 void Update(int num,int l,int r,int targetl,int targetr,int _val,bool isPlus){ 44 if(l>targetr||r<targetl)return; 45 Push_Down(num,l,r); 46 if(l>=targetl&&r<=targetr){ 47 if(isPlus)Plus(num,l,r,_val,1); 48 else Mult(num,l,r,_val); 49 return; 50 } 51 Update(num<<1,l,(l+r)>>1,targetl,targetr,_val,isPlus); 52 Update((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr,_val,isPlus); 53 tree[num]=(tree[num<<1]+tree[(num<<1)+1])%p; 54 } 55 int Query(int num,int l,int r,int targetl,int targetr){ 56 if(l>targetr||r<targetl)return 0; 57 Push_Down(num,l,r); 58 if(l>=targetl&&r<=targetr)return tree[num]; 59 return(Query(num<<1,l,(l+r)>>1,targetl,targetr)+Query((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr))%p; 60 } 61 int main(){ 62 n=read(); 63 for(register int i=1;i<=n*10;i++)mult[i]=1; 64 m=read(); 65 p=read(); 66 for(register int i=1;i<=n;i++)a[i]=read(); 67 Build(1,1,n); 68 for(register int i=1;i<=m;i++){ 69 switch(read()){ 70 case 1: 71 x=read(); 72 y=read(); 73 Update(1,1,n,x,y,read(),false); 74 break; 75 case 2: 76 x=read(); 77 y=read(); 78 Update(1,1,n,x,y,read(),true); 79 break; 80 default: 81 x=read(); 82 write(Query(1,1,n,x,read())); 83 putchar('\n'); 84 } 85 } 86 }
- [Luogu P1198 [JSOI2008]最大数]
1 #pragma GCC optimize(3) 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 inline char Get(){ 7 char ch=getchar(); 8 while(ch!='Q'&&ch!='A')ch=getchar(); 9 return ch; 10 } 11 inline long long read(){ 12 char ch=getchar(); 13 long long o=0; 14 bool sign=false; 15 while(ch<'0'||ch>'9'){ 16 if(ch=='-')sign=true; 17 ch=getchar(); 18 } 19 while(ch>='0'&&ch<='9'){ 20 o=(o<<3)+(o<<1)+(ch^48); 21 ch=getchar(); 22 } 23 return sign?-o:o; 24 } 25 inline void write(int xx){ 26 if(xx>9)write(xx/10); 27 putchar(xx%10^48); 28 } 29 int m,d,t,cnt,ans,tree[800001]; 30 inline void Update(int num,int l,int r,int targetl,int targetr,int _val){ 31 if(l>targetr||r<targetl)return; 32 if(l==r){ 33 tree[num]=_val; 34 return; 35 } 36 Update(num<<1,l,(l+r)>>1,targetl,targetr,_val); 37 Update((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr,_val); 38 tree[num]=max(tree[num<<1],tree[(num<<1)+1]); 39 } 40 inline int Query(int num,int l,int r,int targetl,int targetr){ 41 if(r<targetl||l>targetr)return -2000000000; 42 if(l>=targetl&&r<=targetr)return tree[num]; 43 return max(Query(num<<1,l,(l+r)>>1,targetl,targetr),Query((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr)); 44 } 45 int main(){ 46 memset(tree,-0x7f,sizeof(tree)); 47 m=read(); 48 d=read(); 49 for(register int i=1;i<=m;i++){ 50 if(Get()=='A'){ 51 ++cnt; 52 Update(1,1,m,cnt,cnt,(read()+t)%d); 53 } 54 else{ 55 write(t=Query(1,1,m,cnt-read()+1,cnt)); 56 putchar('\n'); 57 } 58 } 59 }
- [Luogu P1531 I Hate It]
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 inline char Get(){ 5 char ch=getchar(); 6 while(ch!='Q'&&ch!='U')ch=getchar(); 7 return ch; 8 } 9 inline int read(){ 10 char ch=getchar(); 11 int o=0; 12 while(ch<'0'||ch>'9')ch=getchar(); 13 while(ch>='0'&&ch<='9'){ 14 o=(o<<3)+(o<<1)+(ch^48); 15 ch=getchar(); 16 } 17 return o; 18 } 19 inline void write(int xx){ 20 if(xx>9)write(xx/10); 21 putchar(xx%10^48); 22 } 23 int n,m,x,y,a[200001],tree[2000001]; 24 void Build(int num,int l,int r){ 25 if(l==r){ 26 tree[num]=a[l]; 27 return; 28 } 29 Build(num<<1,l,(l+r)>>1); 30 Build((num<<1)+1,((l+r)>>1)+1,r); 31 tree[num]=max(tree[num<<1],tree[(num<<1)+1]); 32 } 33 void Update(int num,int l,int r,int target,int _val){ 34 if(l>target||r<target)return; 35 if(l==r&&r==target){ 36 tree[num]=_val; 37 return; 38 } 39 Update(num<<1,l,(l+r)>>1,target,_val); 40 Update((num<<1)+1,((l+r)>>1)+1,r,target,_val); 41 tree[num]=max(tree[num<<1],tree[(num<<1)+1]); 42 } 43 int Query(int num,int l,int r,int targetl,int targetr){ 44 if(l>targetr||r<targetl)return 0; 45 if(l>=targetl&&r<=targetr)return tree[num]; 46 return max(Query(num<<1,l,(l+r)>>1,targetl,targetr),Query((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr)); 47 } 48 int main(){ 49 n=read(); 50 m=read(); 51 for(register int i=1;i<=n;i++)a[i]=read(); 52 Build(1,1,n); 53 for(register int i=1;i<=m;i++){ 54 if(Get()=='U'){ 55 x=read(); 56 y=read(); 57 if(Query(1,1,n,x,x)<y)Update(1,1,n,x,y); 58 }else{ 59 x=read(); 60 write(Query(1,1,n,x,read())); 61 putchar('\n'); 62 } 63 } 64 }
- [Luogu P2023 [AHOI2009]维护序列]
1 #include<cstdio> 2 #define int long long 3 inline int read(){ 4 char ch=getchar(); 5 int o=0; 6 while(ch<'0'||ch>'9')ch=getchar(); 7 while(ch>='0'&&ch<='9'){ 8 o=(o<<3)+(o<<1)+(ch^48); 9 ch=getchar(); 10 } 11 return o; 12 } 13 inline void write(int xx){ 14 if(xx>9)write(xx/10); 15 putchar(xx%10^48); 16 } 17 int n,p,m,x,y,a[100001],tree[1000001],plus[1000001],mult[1000001]; 18 void Build(int num,int l,int r){ 19 if(l==r){ 20 tree[num]=a[l]%p; 21 return; 22 } 23 Build(num<<1,l,(l+r)>>1); 24 Build((num<<1)+1,((l+r)>>1)+1,r); 25 tree[num]=(tree[num<<1]+tree[(num<<1)+1])%p; 26 } 27 inline void Mult(int num,int l,int r,int _val){ 28 mult[num]=mult[num]*_val%p; 29 tree[num]=_val*tree[num]%p; 30 } 31 inline void Plus(int num,int l,int r,int _val,int extra){ 32 plus[num]=(plus[num]*extra+_val)%p; 33 tree[num]=(tree[num]+(r-l+1)*_val)%p; 34 } 35 inline void Push_Down(int num,int l,int r){ 36 Mult(num<<1,l,(l+r)>>1,mult[num]); 37 Mult((num<<1)+1,((l+r)>>1)+1,r,mult[num]); 38 Plus(num<<1,l,(l+r)>>1,plus[num],mult[num]); 39 Plus((num<<1)+1,((l+r)>>1)+1,r,plus[num],mult[num]); 40 plus[num]=0; 41 mult[num]=1; 42 } 43 void Update(int num,int l,int r,int targetl,int targetr,int _val,bool isPlus){ 44 if(l>targetr||r<targetl)return; 45 if(l!=r)Push_Down(num,l,r); 46 if(l>=targetl&&r<=targetr){ 47 if(isPlus)Plus(num,l,r,_val,1); 48 else if(_val!=1)Mult(num,l,r,_val); 49 return; 50 } 51 Update(num<<1,l,(l+r)>>1,targetl,targetr,_val,isPlus); 52 Update((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr,_val,isPlus); 53 tree[num]=(tree[num<<1]+tree[(num<<1)+1])%p; 54 } 55 int Query(int num,int l,int r,int targetl,int targetr){ 56 if(l>targetr||r<targetl)return 0; 57 Push_Down(num,l,r); 58 if(l>=targetl&&r<=targetr)return tree[num]; 59 return (Query(num<<1,l,(l+r)>>1,targetl,targetr)+Query((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr))%p; 60 } 61 signed main(){ 62 n=read(); 63 for(register int i=1;i<=(n<<2);i++)mult[i]=1; 64 p=read(); 65 for(register int i=1;i<=n;i++)a[i]=read(); 66 m=read(); 67 Build(1,1,n); 68 for(register int i=1;i<=m;i++){ 69 switch(read()){ 70 case 1: 71 x=read(); 72 y=read(); 73 Update(1,1,n,x,y,read(),false); 74 break; 75 case 2: 76 x=read(); 77 y=read(); 78 Update(1,1,n,x,y,read(),true); 79 break; 80 default: 81 x=read(); 82 write(Query(1,1,n,x,read())); 83 putchar('\n'); 84 } 85 } 86 }
- [Luogu P4145 上帝造题的七分钟2 / 花神游历各国]
1 #pragma GCC optimize(3) 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #define int long long 6 using namespace std; 7 inline int read(){ 8 char ch=getchar(); 9 int o=0; 10 while(ch<'0'||ch>'9')ch=getchar(); 11 while(ch>='0'&&ch<='9'){ 12 o=(o<<3)+(o<<1)+(ch^48); 13 ch=getchar(); 14 } 15 return o; 16 } 17 inline void write(int xx){ 18 if(xx>9)write(xx/10); 19 putchar(xx%10^48); 20 } 21 int n,m,x,y,a[100001],tree[400001],maxnum[400001]; 22 inline void Build(int num,int l,int r){ 23 if(l==r){ 24 tree[num]=maxnum[num]=a[l]; 25 return; 26 } 27 Build(num<<1,l,(l+r)>>1); 28 Build((num<<1)+1,((l+r)>>1)+1,r); 29 tree[num]=tree[num<<1]+tree[(num<<1)+1]; 30 maxnum[num]=max(maxnum[num<<1],maxnum[(num<<1)+1]); 31 } 32 inline void Update(int num,int l,int r,int targetl,int targetr){ 33 if(maxnum[num]<2||r<targetl||l>targetr)return; 34 if(l==r){ 35 tree[num]=sqrt(tree[num]); 36 maxnum[num]=tree[num]; 37 return; 38 } 39 Update(num<<1,l,(l+r)>>1,targetl,targetr); 40 Update((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr); 41 tree[num]=tree[num<<1]+tree[(num<<1)+1]; 42 maxnum[num]=max(maxnum[num<<1],maxnum[(num<<1)+1]); 43 } 44 inline int Query(int num,int l,int r,int targetl,int targetr){ 45 if(r<targetl||l>targetr)return 0; 46 if(l>=targetl&&r<=targetr)return tree[num]; 47 return Query(num<<1,l,(l+r)>>1,targetl,targetr)+Query((num<<1)+1,((l+r)>>1)+1,r,targetl,targetr); 48 } 49 signed main(){ 50 n=read(); 51 for(register int i=1;i<=n;i++)a[i]=read(); 52 Build(1,1,n); 53 m=read(); 54 for(register int i=1;i<=m;i++){ 55 if(read()){ 56 x=read(); 57 y=read(); 58 if(x>y)swap(x,y); 59 write(Query(1,1,n,x,y)); 60 putchar('\n'); 61 }else{ 62 x=read(); 63 y=read(); 64 if(x>y)swap(x,y); 65 Update(1,1,n,x,y); 66 } 67 } 68 }