void CFin(D TID,D Fc){
 D FX=LenX,FY=LenY;memset(PreSet,-1,sizeof(PreSet));
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j){
   I(C[i][j])FX=min(FX,i),FY=min(FY,j);
   F(D d=0;d<4;++d)Fin[i][j][d]=100000000,PreRep[i][j][d]=0,Pd[i][j][d]=-1;
  }
 }
 Fin[FX][FY][Fc]=0;W(Qu.size())Qu.pop();
 Qu.push(make_pair(make_pair(FX,FY),Fc));
 #define C(x,y)(!QPlc(TID,x-Ts[TID][d].FX,y-Ts[TID][d].FY,d))
 W (!Qu.empty()){
  D x=Qu.front().first.first,y=Qu.front().first.second,d=Qu.front().second,w=Fin[x][y][d];
  D ny=y;Qu.pop();
  D nx=x-1;
  W(C(nx,y)){
   I(((!C(nx-1,y))||((x-nx)%max(1,Gravity)==0))&&(Fin[nx][y][d]>w+1)){
    Fin[nx][y][d]=w+1,Qu.push(make_pair(make_pair(nx,y),d));
    Pi[nx][y][d]=x;Pj[nx][y][d]=y;Pd[nx][y][d]=d;PreCh[nx][y][d]=K_SOFTDROP;PreRep[nx][y][d]=(x-nx+max(1,Gravity)-1)/max(1,Gravity);
   }
   --nx;
  }
  D cn=0;
  W(C(x,ny-1))--ny,++cn;
  I(Fin[x][ny][d]>w+1){
  	Fin[x][ny][d]=w+1,Qu.push(make_pair(make_pair(x,ny),d));
  	Pi[x][ny][d]=x;Pj[x][ny][d]=y;Pd[x][ny][d]=d;PreCh[x][ny][d]=K_LEFT;PreRep[x][ny][d]=cn;
  }
  ny=y;cn=0;W(C(x,ny+1))++ny,++cn;
  I(Fin[x][ny][d]>w+1){
  	Fin[x][ny][d]=w+1,Qu.push(make_pair(make_pair(x,ny),d));
  	Pi[x][ny][d]=x;Pj[x][ny][d]=y;Pd[x][ny][d]=d;PreCh[x][ny][d]=K_RIGHT;PreRep[x][ny][d]=cn;
  }
  I(C(x,y-1)){
   I(Fin[x][y-1][d]>w+1){
    Fin[x][y-1][d]=w+1,Qu.push(make_pair(make_pair(x,y-1),d));
    Pi[x][y-1][d]=x;Pj[x][y-1][d]=y;Pd[x][y-1][d]=d;PreCh[x][y-1][d]=K_LEFT;PreRep[x][y-1][d]=1;
   }
  }
  I(C(x,y+1)){
   I(Fin[x][y+1][d]>w+1){
    Fin[x][y+1][d]=w+1,Qu.push(make_pair(make_pair(x,y+1),d));
    Pi[x][y+1][d]=x;Pj[x][y+1][d]=y;Pd[x][y+1][d]=d;PreCh[x][y+1][d]=K_RIGHT;PreRep[x][y+1][d]=1;
   }
  }
  F(D del=1;del<4;++del){
   I(FixedFac)CN;
   I((del==2)&&(!Sp180))CN;
   I((del==1)&&(!Sp2S)&&(DefaultR))CN;
   I((del==3)&&(!Sp2S)&&(!DefaultR))CN;
   D nd=(d+del)&3,RX=0,RY=0,_f=0;
   d=(d+InitRot)&3;nd=(nd+InitRot)&3;
   vector<pair<D,D> >KT=(TID)?(((TID==3)||(TID>=11))?OK[d][nd]:K[d][nd]):IK[d][nd];
   d=(d+4-InitRot)&3;nd=(nd+4-InitRot)&3;
   F(pair<D,D>p:KT){
   	I((!KWall)&&((p.first)||(p.second)))CN;
   	D nx=x-Ts[TID][d].FX+p.second,ny=y-Ts[TID][d].FY+p.first;
   	I(!QPlc(TID,nx,ny,nd)){
     _f=1;RX=nx;RY=ny;BK;
    }
    I((RSys==3)&&((TID==1)||(TID==2)||(TID==5))&&(Stuck))BK;
   }
   I(_f){
   	RX+=Ts[TID][nd].FX;RY+=Ts[TID][nd].FY;
    I(Fin[RX][RY][nd]>w+1){
     Fin[RX][RY][nd]=w+1,Qu.push(make_pair(make_pair(RX,RY),nd));
     Pi[RX][RY][nd]=x;Pj[RX][RY][nd]=y;Pd[RX][RY][nd]=d;PreCh[RX][RY][nd]=(del==1)?K_CCW:((del==2)?K_180:K_CW);PreRep[RX][RY][nd]=1;
	}
   }
  }
 }
 F(D j=0;j<LenY;++j){
  F(D d=0;d<4;++d){
   F(D i=LenX-2;i>=0;--i){
   	I((Fin[i][j][d]<100000000)&&(Fin[i][j][d]>Fin[i+1][j][d]))Fin[i][j][d]=Fin[i+1][j][d];
   }
  }
 }
 D ReCMod=1;
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j){
   I(B[i][j]&&!C[i][j]&&(i+ARe)*3>=DisX)CMod=0;
   I(B[i][j]&&!C[i][j]&&(i+ARe+1)*3>=DisX)ReCMod=0;
  }
 }
 I(ReCMod)CMod=1;
 Rd=-1;pair<LD,LD> MnF=make_pair(0LL,0LL);D MnFi=0; 
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j){
   F(D d=0;d<4;++d){
    I((Fin[i][j][d]>=100000000)||((i>0)&&(Fin[i-1][j][d]<100000000)))CN;
    F(D ii=0;ii<LenX;++ii){
   	 F(D jj=0;jj<LenY;++jj)TB[ii][jj]=((B[ii][jj])&&(!C[ii][jj]));
	}
    F(D ii=0;ii<Ts[TID][d].LenX;++ii){
     F(D jj=0;jj<Ts[TID][d].LenY;++jj){
      I(Ts[TID][d].Bl[ii][jj])TB[i+ii-Ts[TID][d].FX][j+jj-Ts[TID][d].FY]=2;
	 }}
	pair<LD,LD> Fr=TmpCalc(i,TID);
	I((Rd==-1)||(make_pair(Fr,Fin[i][j][d])<make_pair(MnF,MnFi))){
     MnF=Fr;MnFi=Fin[i][j][d];Rd=d;Ri=i;Rj=j;
	}
   }
  }
 }
 LstMnF=MnF;
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j)HM[i][j]=0;
 }
 F(D i=0;i<Ts[TID][Rd].LenX;++i){
  F(D j=0;j<Ts[TID][Rd].LenY;++j){
   I(Ts[TID][Rd].Bl[i][j])HM[Ri+i-Ts[TID][Rd].FX][Rj+j-Ts[TID][Rd].FY]=14;
  }
 }
 W(Oper.size())Oper.pop();
 D Prev=0;
 W(((Ri!=FX)||(Rj!=FY)||(Rd!=Fc))&&(Fin[Ri+1][Rj][Rd]<100000000)&&(Ri+1<LenX))++Ri;
 W((Rd>=0)&&((Ri!=FX)||(Rj!=FY)||(Rd!=Fc))){
  I(PreCh[Ri][Rj][Rd]!=K_SOFTDROP||Prev){
   I((ARR<0)&&((PreCh[Ri][Rj][Rd]==K_LEFT)||(PreCh[Ri][Rj][Rd]==K_RIGHT)))PreRep[Ri][Rj][Rd]=min(PreRep[Ri][Rj][Rd],2);
   F(D i=1;i<PreRep[Ri][Rj][Rd];++i)Oper.push(make_pair(PreCh[Ri][Rj][Rd],1));
   I(PreRep[Ri][Rj][Rd])Oper.push(make_pair(PreCh[Ri][Rj][Rd],0));
   Prev=1;
  }
  D Ni=Pi[Ri][Rj][Rd],Nj=Pj[Ri][Rj][Rd],Nd=Pd[Ri][Rj][Rd];
  Ri=Ni;Rj=Nj;Rd=Nd;
 }
 #undef C
}
D Spw(CT T,D TID,D Fc,D PreTest=0){
 I(PartTopOut){
  D Li=(LenY>=4)?DisX-1:DisX-3;
  F(D i=Li;i<LenX;++i){
   F(D j=0;j<LenY;++j)I(CheckT(i,j))RE 0;
  }
 }
 D PosY=(LenY>>1)+(T.LenY>>1)-1,PosX=DisX;
 PosY=max(PosY,T.LenY-1);
 PosY=min(PosY,LenY-1);
 PosX-=T.LenX-1;PosY-=T.LenY-1;bool flag=1;
 F(D i=T.LenX-1;i>=0;--i){
  F(D j=0;j<T.LenY;++j){
   I(T.Bl[i][j]){
    flag=0;BK;
   }}
  I(flag)++PosX;
 }
 F(D i=0;i<T.LenX;++i){
  F(D j=0;j<T.LenY;++j){
   I((T.Bl[i][j])&&(CheckT(PosX+i,PosY+j)))RE 0;
  }}
 F(D i=0;i<T.LenX;++i){
  F(D j=0;j<T.LenY;++j){
   I(T.Bl[i][j]){
    B[PosX+i][PosY+j]=T.Color;
    C[PosX+i][PosY+j]=1;
 }}}
 I(!PreTest)CFin(TID,Fc);RE 1;
}
D CheckAC(){
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j){
   I(B[i][j])RE 0;
  }}
 RE 1;
}
LD KeyStTm,HDTm;D LstHD=0;
char RInp='!';D ArrK,_sdd=SDD;
char GetKeyInLi(LD TmLi){
 _ntlp=_ntl-(TmLi-(clock()-KeyStTm));
 ArrK=0;
 I(InitH!='!'){
  char res=InitH;InitH='!';RE res;
 }
 I(InitR!='!'){
  char res=RInp=InitR;InitR='!';RE res;
 }
 I(gks(K_HOLD))RE K_HOLD;
 I (TmLi<-15LL&&TID!=-TC) RE '!';
 char LRInp=RInp;
 I(gks(K_CW)){
  RInp=K_CW;
  I(LRInp!=K_CW)RE K_CW;
 }
 EI(gks(K_CCW)){
  RInp=K_CCW;
  I(LRInp!=K_CCW)RE K_CCW;
 }
 EI(gks(K_180)){
  RInp=K_180;
  I(LRInp!=K_180)RE K_180;
 }
 EI(gks(K_ROT)){
  RInp=K_ROT;
  I(LRInp!=K_ROT)RE K_ROT;
 }
 EI(gks(K_HARDDROP)){
  RInp=K_HARDDROP; LD cur=clock();
  I(!LstHD) {
  	HDTm=cur;LstHD=1;RE K_HARDDROP;
  }
 }
 E RInp='!';
 I(!gks(K_HARDDROP))LstHD=0;
 I((!(gks(K_LEFT)))&&(!(gks(K_RIGHT)))&&(!(gks(K_SOFTDROP))))LKey='!';
 I((LKey!=K_LEFT)&&(LKey!=K_RIGHT)&&(LKey!=K_SOFTDROP))ARep=0;
 I((LKey==K_LEFT)&&(gks(K_LEFT))){
  LD NewTm=clock();
  I((NewTm-LGTm>=((ARep)?ARR:DAS))){
   LGTm=NewTm;ARep=1;LKey=K_LEFT;ArrK=1;RE K_LEFT;
  }}
 EI((LKey==K_RIGHT)&&(gks(K_RIGHT))){
  LD NewTm=clock();
  I((NewTm-LGTm>=((ARep)?ARR:DAS))){
 LGTm=NewTm;ARep=1;LKey=K_RIGHT;ArrK=1;RE K_RIGHT;
  }}
 EI((LKey==K_SOFTDROP)&&(gks(K_SOFTDROP))){
  LD NewTm=clock();
  I(NewTm-LGTm>=((ARep)?_sdd:DAS)){
   LGTm=NewTm;ARep=1;LKey=K_SOFTDROP;ArrK=1;RE K_SOFTDROP;
  }}
 E {
  LKey=0;
  I(kbhit()){
   char res='!';D DirK=0;W(kbhit())res=getch(),DirK|=(res==-32);
   I((res!=K_HARDDROP)&&(TmLi<=-15))res='!';
   I(isupper(res))res=tolower(res);
   I(DirK){
    I(res==107)res=VK_LEFT;I(res==109)res=VK_RIGHT;I(res==104)res=VK_UP;I(res==112)res=VK_DOWN;
   }
   I((res!=K_CW)&&(res!=K_CCW)&&(res!=K_180)&&(res!=K_ROT)&&(res!=K_HARDDROP)&&(res!=K_CON)){
    I(LKey!=res)ARep=0;
    LGTm=clock();RE LKey=res;
   }}
 }
 W(((LD)clock())-KeyStTm<TmLi||TID==TC){
  _ntlp=_ntl-(TmLi-(clock()-KeyStTm));
  I(gks(K_HOLD))RE K_HOLD;
  LRInp=RInp;
  I(gks(K_CW)){
   RInp=K_CW;
   I(LRInp!=K_CW)RE K_CW;
  }
  EI(gks(K_CCW)){
   RInp=K_CCW;
   I(LRInp!=K_CCW)RE K_CCW;
  }
  EI(gks(K_180)){
   RInp=K_180;
   I(LRInp!=K_180)RE K_180;
  }
  EI(gks(K_ROT)){
   RInp=K_ROT;
   I(LRInp!=K_ROT)RE K_ROT;
  }
  EI(gks(K_HARDDROP)){
   RInp=K_HARDDROP; LD cur=clock();
   I(!LstHD) {
    HDTm=cur;LstHD=1;RE K_HARDDROP;
   }
  }
  E RInp='!';
  I(!gks(K_HARDDROP))LstHD=0;
  I((LKey==K_LEFT)&&(gks(K_LEFT))){
   LD NewTm=clock();
   I(NewTm-LGTm>=((ARep)?ARR:DAS)){
    LGTm=NewTm;ARep=1;LKey=K_LEFT;ArrK=1;RE K_LEFT;
   }}
  EI((LKey==K_RIGHT)&&(gks(K_RIGHT))){
   LD NewTm=clock();
   I(NewTm-LGTm>=((ARep)?ARR:DAS)){
    LGTm=NewTm;ARep=1;LKey=K_RIGHT;ArrK=1;RE K_RIGHT;
   }}
  EI((LKey==K_SOFTDROP)&&(gks(K_SOFTDROP))){
   LD NewTm=clock();
   I(NewTm-LGTm>=((ARep)?_sdd:DAS)){
    LGTm=NewTm;ARep=1;LKey=K_SOFTDROP;ArrK=1;RE K_SOFTDROP;
   }}
  E {
   ARep=0;
   I(kbhit()){
    char res='!';D DirK=0;W(kbhit())res=getch(),DirK|=(res==-32);
    I((res!=K_HARDDROP)&&(TmLi<=-15))res='!';
    I(isupper(res))res=tolower(res);
    I(DirK){
     I(res==107)res=VK_LEFT;I(res==109)res=VK_RIGHT;I(res==104)res=VK_UP;I(res==112)res=VK_DOWN;
    }
    LD cur = clock();
    I((res!=K_CW)&&(res!=K_CCW)&&(res!=K_180)&&(res!=K_ROT)&&(res!=K_HARDDROP)&&(res!=K_CON)){
     I(LKey!=res)ARep=0;
     LGTm=clock();RE LKey=res;
    }}}
  I(CDrop)MsRe=max(0,MsRe-Rnd.Rand(2)),Ds();
  E DsTm();
 }
 RE '!';
}
D TCnt[910];
D F2Poi,RndReset=1;
D TPool[5000],PSz=0,PoolC[910],Hei[100];
D RndT(){
 I(RndFunc<2){
  D MinC=TCnt[0];F(D i=1;i<WTC;++i)MinC=min(TCnt[i],MinC);
  D PosC=0;F(D i=0;i<WTC;++i)I(MinC==TCnt[i])++PosC;
  D NextT=0,NextP=Rnd.Rand(PosC);
  F(D i=0;i<WTC;++i){
   I(MinC==TCnt[i])I(!(NextP--))NextT=i;
  }
  I(RndFunc)++TCnt[NextT];
  RE TDy[NextT];
 }
 I(RndFunc==2){
  I(RndReset){
   F2Poi=RndReset=0;F(D i=0;i<WTC;++i)TCnt[i]=i;
   shuffle(TCnt,TCnt+WTC,mt19937(time(0)));
  }
  D Re = TDy[TCnt[F2Poi]];
  ++F2Poi;I(F2Poi>=WTC)F2Poi=0;
  RE Re;
 }
 I(RndFunc==3){
  I(RndReset){
   TCnt[0]=TCnt[1]=TCnt[2]=-1;RndReset=0;
  }
  D x=Rnd.Rand(7);vector<int> cast={2,1,0,3,6,5,4};
  I(min(TCnt[0],min(TCnt[1],TCnt[2]))<0){
   TCnt[0]=TCnt[1];TCnt[1]=TCnt[2];TCnt[2]=x;
   RE cast[x];
  }
  I(x!=(TCnt[0]|TCnt[1]|TCnt[2])){
   TCnt[0]=TCnt[1];TCnt[1]=TCnt[2];TCnt[2]=x;
   RE cast[x];
  }
  x=Rnd.Rand(7);
  I(x!=(TCnt[0]|TCnt[1]|TCnt[2])){
   TCnt[0]=TCnt[1];TCnt[1]=TCnt[2];TCnt[2]=x;
   RE cast[x];
  }
  x=Rnd.Rand(7);
  TCnt[0]=TCnt[1];TCnt[1]=TCnt[2];TCnt[2]=x;
  RE cast[x];
 }
 I(RndFunc==4){
  I(RndReset){
   TCnt[0]=-1;RndReset=0;
  }
  D x = Rnd.Rand(WTC+1);
  I((x==TCnt[0])||(x==WTC))RE TDy[TCnt[0]=Rnd.Rand(WTC)];
  E RE TDy[TCnt[0]=x];
 }
 I(RndFunc==5||RndFunc==6){
  I(RndReset){
   F(D i=0;i<4;++i)TCnt[i]=WTC-1;
   RndReset=0;
  }
  D x=0;
  F(D i=0;i<((RndFunc==5)?4:6);++i){
   x=Rnd.Rand(WTC);
   I((x!=TCnt[0])&&(x!=TCnt[1])&&(x!=TCnt[2])&&(x!=TCnt[3]))BK;
  }
  TCnt[F2Poi]=x;F2Poi=(F2Poi==3)?0:F2Poi+1;
  RE TDy[x];
 }
 I(RndFunc==7){
  I(RndReset||!PSz){
   RndReset=0;
   F(D i=0;i<4;++i)TCnt[i]=-1;
   F(D i=0;i<WTC;++i){
    F(D j=0;j<5;++j)TPool[PSz++]=i;
   }
  }
  D x=0;
  F(D i=0;i<6;++i){
   x=Rnd.Rand(PSz);
   I((TPool[x]!=TCnt[0])&&(TPool[x]!=TCnt[1])&&(TPool[x]!=TCnt[2])&&(TPool[x]!=TCnt[3]))BK;
  }
  D Re=TPool[x];F(D i=0;i<WTC;++i)PoolC[i]=0;
  TCnt[F2Poi]=Re;F2Poi=(F2Poi==3)?0:F2Poi+1;
  F(D i=0;i<PSz;++i){
   I(i!=x)++PoolC[TPool[i]];
  }
  D MnC=PoolC[0];F(D i=1;i<WTC;++i)MnC=min(MnC,PoolC[i]);
  D MnS=0;F(D i=0;i<WTC;++i)MnS+=(PoolC[i]==MnC);
  D Ins=0;I(MnS)MnS=Rnd.Rand(MnS);
  F(D i=0;i<WTC;++i){
   I(PoolC[i]==MnC){
    I(!MnS){
     Ins=i;BK;
	}
	--MnS;
   }
  }
  TPool[x]=Ins;
  RE TDy[Re];
 }
 I(RndFunc==8){
  I(RndReset){
   RndReset=0;F(D i=0;i<WTC;++i)TCnt[i]=0;
  }
  F(D i=0;i<WTC;++i){
   TCnt[i]>>=1;TCnt[i]=min(1000000000,TCnt[i]+=Rnd.Rand(100000000));
  }
  F(D i=0;i<WTC;++i)PoolC[i]=i;
  shuffle(PoolC,PoolC+WTC,mt19937(D(clock())));
  D x=PoolC[0];
  F(D i=1;i<WTC;++i){
   I(TCnt[PoolC[i]]>TCnt[x])x=PoolC[i];
  }
  TCnt[x]=TCnt[x]*2/7;RE TDy[x];
 }
 I(RndFunc==9){
  I(RndReset){
   RndReset=PSz=0;
  }
  I(!PSz){
   D x=Rnd.Rand(WTC),y=Rnd.Rand(WTC);
   I(y==x)y=Rnd.Rand(WTC);
   F(D i=0;i<3;++i)TPool[PSz++]=x;
   F(D i=0;i<3;++i)TPool[PSz++]=y;
  }
  D x=Rnd.Rand(PSz);swap(TPool[x],TPool[PSz-1]);
  x=TPool[PSz-1];--PSz;
  RE TDy[x];
 }
 I(RndFunc==10){
  I(RndReset){
   RndReset=0;TCnt[0]=3;
  }
  I(TCnt[0]){
   --TCnt[0];RE 0;
  }
  vector<D> Lst={1,2,4,4,6,6};
  Hei[0]=Hei[LenY+1]=1000;
  F(D i=0;i<LenY;++i){
   Hei[i+1]=0;
   F(D j=LenX;j>=0;--j){
    I(B[j][i]&&!C[j][i]){
     Hei[i+1]=j+1;BK;
	}
   }
  }
  D Pit=0,Stair=0,Plain=0;
  F(D i=1;i<=LenY;++i){
   I(Hei[i]+3<=Hei[i-1]&&Hei[i]+3<=Hei[i+1])Pit=1;
  }
  F(D i=1;i<LenY;++i){
   I(Hei[i]==Hei[i+1])++Plain;
   I(abs(Hei[i]-Hei[i+1])==1)++Stair;
  }
  I(!Pit){
   F(D i=0;i<3;++i)Lst.push_back(0);
  }
  I(Stair<=2){
   F(D i=0;i<4;++i)Lst.push_back(5);
  }
  I(Plain<=2){
   F(D i=0;i<3;++i)Lst.push_back(3);
  }
  RE Lst[Rnd.Rand(Lst.size())];
 }
 RE 0;
}
D SpwTSt;
D NewT(D Cert=-1){
 D NextT=Cert;
 I(Cert==-1){
  NextT=TQu[0];
  F(D i=0;i+1<max(1,NxtT);++i)TQu[i]=TQu[i+1];
  TQu[max(1,NxtT)-1]=RndT();
 }
 F(D i=0;i<(LenY<4?4:1);++i){
  I(Spw(Ts[NextT][i],NextT,i)){
   SpwTSt=i;RE NextT;
  }
 }
 RE -1;
}
D RmL(){
 D LCT=LenX;
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j){
   I(!B[i][j]){
    --LCT;BK;
   }
  }
 }
 I(LCT<LCSt)RE 0;
 D LCID=0;
 D NewL=0,Ls=0,FirstClear=LenX-1;
 F(D i=0;i<LenX;++i){
  D LClear=1,IsG=0,Neg=0;
  F(D j=0;j<LenY;++j){
   I(!B[i][j]){
    LClear=0;BK;
   }
   I(B[i][j]<0)Neg=1;
   I((B[i][j]&63)==8)IsG=1;
  }
  I(Neg)LClear=0;
  I(LClear){
   ++LCID;I(LCT-LCID>=LCLi)LClear=0;
  }
  D sz=BlockSz;
  I(LClear){
   ++Ls;FirstClear=min(FirstClear,i);
   F(D j=0;j<LenY;++j){
    D lx=(DisX-i+5)*sz-80,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
    BMark[i][j]=1;PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,3.0,0.05,-2.0,(Rnd.Rand(11)-5)*0.1,0.2,0,0x99FFFF});
    F(D d=0;d<5;++d){
     FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
     PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.2,0.05,0.5*sin(theta),0.5*cos(theta),0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.3)});
	}
	B[i][j]=0;
   }
   I(!LCG)++NewL;
   I(IsG)++SD;
  }
  E {
   I(NewL<i){
    F(D j=0;j<LenY;++j)B[NewL][j]=B[i][j];
   }
   ++NewL;
  }}
 F(D i=NewL;i<LenX;++i){
  F(D j=0;j<LenY;++j)B[i][j]=0;
 }
 I((LCG==2)&&(Ls)){
  F(D j=0;j<LenY;++j){
   D FillPos=0;
   F(D i=0;i<FirstClear;++i)I(B[i][j])FillPos=i+1;
   F(D i=FirstClear;i<LenX;++i){
    I(B[i][j]){
     B[FillPos][j]=B[i][j];++FillPos;
    }}
   F(D i=FillPos;i<LenX;++i)B[i][j]=0;
  }
 }
 RE Ls;
}
D GridC=0,GPos,AGC;
void AddG(D y,D Tp=2){
 I(Tp!=2)y=Rnd.Rand(LenY);
 I(GTp==7)y=(GPos>=LenY)?LenY+LenY-GPos-1:GPos;
 ++GPos;GPos%=(LenY<<1);
 F(D i=LenX-2;i>=0;--i){
  F(D j=0;j<LenY;++j){
   B[i+1][j]=B[i][j];C[i+1][j]=C[i][j];
  }}
 GridC ^= 1;
 bool AllFull=1, AllEmpty=1;
 F(D i=0;i<LenY;++i){
  I(!GTp)B[0][i]=0;
  EI((GTp==1)||(GTp==6)||(GTp==7))B[0][i]=((i==y)xor(GTp==6))?0:(8|(1024*Tp));
  EI(GTp==2)B[0][i]=(Rnd.Rand(3))?0:(8|(1024*Tp));
  EI(GTp==3)B[0][i]=((GridC+i)&1)?0:(8|(1024*Tp));
  EI(GTp==4)B[0][i]=((B[1][i])&&(!C[1][i]))?(8|(1024*Tp)):0;
  EI(GTp==5)B[0][i]=((B[1][i])&&(!C[1][i]))?0:(8|(1024*Tp));
  C[0][i]=0;
  I (B[0][i])AllEmpty=0;
  E AllFull=0;
 }
 I((AllEmpty)&&(GTp)&&(GTp!=4)&&(GTp!=5))B[0][y]=(8|(1024*Tp));
 I(AllFull){
  B[0][y]=0;
  I(GTp==5){
   F(D i=0;i<LenY;++i)B[0][i]=0;
  }
 }
 I(ExG){
  F(D i=0;i<LenY;++i){
   I(!B[0][i])B[0][i]=-1;
  }
 }
 MvDown();++AGC;
}
D RemoveG(){
 I(!ARe)RE 0;
 F(D i=0;i+1<ARe;++i)AQu[i]=AQu[i+1];
 --ARe;
 RE 1;
}
D EnableG(){
 I(!ARe)RE 0;
 AddG(AQu[0]);
 F(D i=0;i+1<ARe;++i)AQu[i]=AQu[i+1];
 --ARe;
 RE 1;
}
D QSecGr(){
 D p=0, d=1;
 F(D i=0;i<LenX;++i){
  I(B[i][p])RE i;
  F(D j=0;j<LenY;++j)I((j!=p)&&(!B[i][j]))RE i;
  I((!CheckT(i-1,p))||(!CheckT(i+1,p))||(!CheckT(i,p-1))||(!CheckT(i,p+1)))RE i;
  p+=d;
  I(p>=LenY)p-=2,d=-d;
  I(p<0)p+=2,d=-d;
 }
 RE LenX;
}
D GetTHeight(){
 D res=(LenX<<2);
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j){
   I(C[i][j]){
    res-=LenX;res+=i;
   }}}
 RE res;
}
FL VMul(D V){
 I(!VTp)RE max(0.01,1.0-VMp*FL(V));
 I(VTp==1)RE 1.0/max(0.01,1.0+VMp*FL(V));
 RE pow(1.0-VMp,FL(V));
}
D BPC=0,*BPoi[25000],Bac=0,BPos=0;
struct BacRec {
 LD _sr,_asr,_tim,_gt;
 vector<D> val;
 void Get(){
  _sr=Sr;_asr=ASr;_gt=GoalTm;val.clear();_tim=clock()-BegTm;
  F(D i=0;i<BPC;++i)val.push_back(*BPoi[i]);
 }
 void Set(){
  Sr=_sr;ASr=_asr;GoalTm=_gt;BegTm=clock()-_tim;
  F(D i=0;i<val.size();++i)*BPoi[i]=val[i];
 }
}BR[35],TBR;
void AddBac(D &x){
 BPoi[BPC++]=&x;
}
void Storage(){
 I(TBR.val.size()){
  BPos=(BPos+1)%30;BR[BPos]=TBR;Bac=min(Bac+1,30);
 }
 TBR.Get();
}
void Backup(){
 --Bac;TBR=BR[BPos];BR[BPos].Set();
 BPos=(BPos+29)%30;
}
queue<pair<D,D> > STask,SDel;
HMIDIOUT SHnd;
void Pb(){
 I(!SndEff)F(;;);
 D id=0,ii;
 F(;;){
  I(STask.empty()){
   Sleep(20);CN;
  }
  pair<D,D>o=STask.front();STask.pop();
  id=ii=(ii+1)%3;id+=3;
  midiOutShortMsg(SHnd,o.first<<8|0xC0|id);
  midiOutShortMsg(SHnd,0x7F<<16|o.second<<8|0x90|id);
 }
}
void Pbl(){
 I(!SndEff)for(;;);
 F(;;){
  I(SDel.empty()){
   Sleep(20);CN;
  }
  pair<D,D>o=SDel.front();SDel.pop(); 
  midiOutShortMsg(SHnd,o.first<<8|0xC0);
  midiOutShortMsg(SHnd,0x7F<<16|o.second<<8|0x90);
  LD _=clock();while(clock()-_<200);
 }
}
string Cast="zxcvbnmasdfghjqwertyu1234567";
/*
<<<<<<<<<dsdgs dgs sdsan dsdgs dgs sds a d g s  asasdsan d g s  qjgdsdgs d g s  asasdsan d g s  qjgdsdgs \
sas sas sasdsan sas sas dqjgdsd sas sas sasdsan sas sad g    g\
tew w-q.wehqwqw-e.egtew w-q.wqwew-q.q  gtew w-q.wehqwqw-e.e  gh egh wwh    g\
tew w-q.wehqwqw-e.egtew w-q.wqwewqqhqgtew w-q.wehqwqw-e.e  gh egh wwh       \
dsdgs dgs sdsan dsdgs dgs s-d.sad dsdgs dgs sdsan dsdgs dgs sds a \
d g s  asasdsan d g s  qjgdsdgs d g s  asasdsan d g s q jgdsdgs \
sas sas sasdsan sas sas dqjgdsd sas sas sasdsan sas sad g    g\
tew w-q.wehqwqw-e.egtew w-q.wqwew-q.q  gtew w-q.wehqwqw-e.e  gh egh wwh    g\
tew w-q.wehqwqw-e.egtew w-q.wqwewqqhqgtew w-q.wehqwqw-e.e  gh egh wwh       \
nbnew q d g     nbnew q w e     nbnew q h g     a  qw ewq            b\
gds sasdnasasddbgds sasasdsaa  b\
gds sasdnasasdd  bn dbn ssn    \
gtew w-q.wehqwqw-e.egtew w-q.wqwew-q.q  gtew w-q.wehqwqw-e.e  gh egh wwe    >>\
gtew w-q.wehqwqw-e.egtew w-q.wqwew-q.q  gtew w-q.wehqwqw-e.e  ew qew qwq      ew qew qwq     \
g t w-q.wehqwqw-e.e y t w-q.wqwre    gtew w-q.wehqwqw-e.e  ew qew qwq   ghqe\
w w-q.wehqwqw-e.egtew w-q.wqwewqqhqgtew w-q.wehqwqw-e.e           >>>>>>>\
*/
string Loop="\
ndnbnbcbnbndddgdsdsan-b-nasd g s   \
ndnbnbcbndgds-s-dmbndsdgdmnb   \
a s a   s d g d s d s   a d g d s d s   a n .b-n.a-na d s  ds a s   \
a s a   s d g d s d s   a d g d s d s   a n .b-n.a-na d s  ds  aa g a \
n sdsan   .s-dsan bnb n a s dg  d   sdsan   .s-dsan s d       a   s   \
dndd dddsdgds masbss ssssdgsa asd   g d na  n asd sd  g dhgd\
saasdndd dddsdgds masbss ssssdgsa asd   g d na  n asd   g d s   d s a\
dnbnbcbnbndd   sdsan-b-nasd g s   \
ndnbnbcbndgds-s-dmbndsdgdmnb   \
a s a   s d g d s d s   a d g d s d s   a n .b-n.a-na d s  ds a s   \
a s a   s d g d s d s   a d g d s d s   a n .b-n.a-na d s  ds  aa g a \
n sdsan   .s-dsan bnb n a s dg  d   sdsan   .s-dsan s d       a   s   \
dndd dddsdgds masbss ssssdgsa asd   g d na  n asd sd  g dhgd\
saasdndd dddsdgds masbss ssssdgsa asd   g d na  n asd   g d s   d s a\
dnsdnsdnsdnsdgdsdnsdnsdsdgq-j-q-j-hgd\
sdnsdnsdsdnsdngdsdgds-s-dsad sam \
nas d n nasdgds bas d b basdgsa \
nas d n nasdgds asd sd  g d   a s \
dndd dddsdgds masbss ssssdgsa asd   g d na  n asd sd  g dhgd\
saasdndd dddsdgds masbss sssh g d asd   g d na  n asd   g d s   d s a\
dnbnbcbnbndddgdsdsan-b-nasd h g s \
ddnbnbcbndgds-s-dmbn   g  dg d g qjh           ";
int FQ[7]={60,62,64,65,67,69,71};
int Gf(int x) {
 if((x>=0)&&(x<7))return FQ[x];
 if(x<0)return Gf(x+7)-12;
 return Gf(x-7)+12;
}
void Pm() {
 I(!SndEff)F(;;);
 D m=0,del=0,deftm=50,tm=deftm;
 midiOutShortMsg(SHnd,10<<8|0xC1);
 midiOutShortMsg(SHnd,0<<8|0xC2);
 F(;;) {
  F(D i=0;i<Loop.size();++i) {
   D r=-1;char ch=Loop[i];
   I(ch=='_')CN;
   I(ch=='>'){++m;CN;}
   I(ch=='<'){--m;CN;}
   I(ch=='+'){tm<<=1;CN;}
   I(ch=='-'){tm>>=1;CN;}
   I(ch=='#'){++del;CN;}
   I(ch=='.'){tm=tm*3/2;CN;}
   F(D j=0;j<Cast.size();++j)I(ch==Cast[j])r=j;
   LD o=clock();
   I(r!=-1)midiOutShortMsg(SHnd,0x5f<<16|(Gf(r)+m+del)<<8|0x91),midiOutShortMsg(SHnd,0x6f<<16|(Gf(r)+m+del-12)<<8|0x92);
   W(clock()-o<tm);
   tm=deftm;del=0;
  }
 }
}
string LCW[50];
#define snd(a,b)STask.push(make_pair(a,b))
#define sndl(a,b)SDel.push(make_pair(a,b))
D WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdL,D nCmdShow){
 system("color 0F");system("cls");
 ifstream fin;fin.open("oajdncnhdi.txt");
 AFile[1]=RFile[2]="1__reiohsoirghsdh.txt";
 AFile[2]=RFile[1]="2__reiohsoirghsdh.txt";
 F(D i=1;i<=2;++i){
  fclose(fopen(RFile[i].c_str(),"w"));
 }
 LD tmp;I(fin>>tmp)ASr=tmp;
 fin.clear();fin.close();midiOutOpen(&SHnd,0,0,0,CALLBACK_NULL);
 SettingsBoard();SetBkMode(Sc.Hdc,TRANSPARENT);thread TPb(Pb),TPbl(Pbl),TPm(Pm);
 I(!GLi)GLi=10000;
 I(!SALi)SALi=1000;
 I(!NTmLi)NTmLi=1000000000;
 I(!LTmLi)LTmLi=1000000000;
 I(MvsLi<0)MvsLi=1000000000;
 I(LCLi<0)LCLi=1000;
 MvsLeft=MvsLi;
 LCW[1]="    SINGLE";LCW[2]="    DOUBLE";LCW[3]="    TRIPLE";LCW[4]="   Q U A D";
 LCW[5]=" P E N T A";LCW[6]="   H E X A";LCW[7]=" H E P T A";LCW[8]="   O C T A";
 LCW[9]=" E N N E A";LCW[10]="   D E C A";LCW[11]="   HENDECA";LCW[12]="    DODECA";
 LCW[13]="  TRIADECA";LCW[14]="TESARADECA";LCW[15]=" PENTEDECA";LCW[16]="  HEXADECA";
 LCW[17]=" HEPTADECA";LCW[18]="  OCTADECA";LCW[19]=" ENNEADECA";LCW[20]="    EICOSA";
 LCW[21]="ULTIMATRIS";LCW[22]="  KIRBTRIS";LCW[23]="INFINITRIS";LCW[24]="   KAGARIS";LCW[25]="  ELECTRIS";
 F(D i=26;i<50;++i)LCW[i]="PERFECTRIS";
 TC=30;BsT[TC].Init("*|",3,'*');
 BsT[0].Init("....|....|****|....|",11,'I');
 BsT[1].Init("...|***|*..|",1,'J');
 BsT[2].Init("...|***|..*|",6,'L');
 I(OSp)BsT[3].Init("...|.**|.**|",14,'O');
 E BsT[3].Init("**|**|",14,'O');
 BsT[4].Init("...|**.|.**|",10,'S');
 BsT[5].Init("...|***|.*.|",13,'T');
 BsT[6].Init("...|.**|**.|",12,'Z');
 F(D i=0;i<=6;++i)F(D j=0;j<U4;++j)TDy[WTC++]=i;
 BsT[7].Init("**|*.|",16,'C');
 BsT[8].Init("...|***|...|",17,'i');
 F(D j=0;j<U3;++j)TDy[WTC++]=7;
 F(D j=0;j<U3;++j)TDy[WTC++]=8;
 BsT[9].Init("..|**|",18,'_');
 F(D j=0;j<U2;++j)TDy[WTC++]=9;
 I(OSp)BsT[10].Init("..|.*|",2,'.');
 E BsT[10].Init("*|",2,'.');
 F(D j=0;j<U1;++j)TDy[WTC++]=10;
 BsT[11].Init(".....|.....|*****|.....|.....|",19,'|');
 BsT[12].Init("***|.*.|.*.|",20,'t');
 BsT[13].Init("...|***|*.*|",21,'U');
 BsT[14].Init("***|..*|..*|",22,'V');
 I(OSp)BsT[15].Init("....|..*.|.***|..*.|",24,'X');
 E BsT[15].Init(".*.|***|.*.|",24,'X');
 BsT[16].Init(".*.|***|*..|",25,'F');
 BsT[17].Init(".*.|***|..*|",26,'E');
 BsT[18].Init("**.|.*.|.**|",27,'s');
 BsT[19].Init(".**|.*.|**.|",28,'z');
 BsT[20].Init(".....|.....|.****|.*...|.....|",30,'j');
 BsT[21].Init(".....|.....|****.|...*.|.....|",29,'l');
 BsT[22].Init(".....|.....|****.|..*..|.....|",31,'Y');
 BsT[23].Init(".....|.....|.****|..*..|.....|",32,'R');
 BsT[24].Init(".....|.....|***..|..**.|.....|",33,'H');
 BsT[25].Init(".....|.....|..***|.**..|.....|",34,'N');
 BsT[26].Init("**.|***|...|",35,'Q');
 BsT[27].Init(".**|***|...|",36,'P');
 BsT[28].Init(".**|**.|*..|",23,'W');
 F(D i=11;i<=28;++i)F(D j=0;j<U5;++j)TDy[WTC++]=i;
 BsT[29].Init("*********|",8,'G');
 CT _t=CT();_t.Init("*.*|*.*|*.*|*.*|",0,'I');//SpTp[0].push_back(_t);
 //_t=CT();_t.Init("***|...|.**|",0,'J');SpTp[1].push_back(_t);
 //_t=CT();_t.Init("*****|*..**|**.**|?*..?|",0,'J');SpTp[1].push_back(_t);
 //_t=CT();_t.Init("***|...|**.|",0,'L');SpTp[2].push_back(_t);
 //_t=CT();_t.Init("*****|**..*|**.**|?..*?|",0,'L');SpTp[2].push_back(_t);
 _t=CT();_t.Init("*.*|...|*..|",0,'T');SpTp[5].push_back(_t);
 _t=CT();_t.Init("*.*|...|..*|",0,'T');SpTp[5].push_back(_t);
 //_t=CT();_t.Init("***|...|..*|..?|",0,'T');SpTp[5].push_back(_t);
 //_t=CT();_t.Init("***|...|*..|?..|",0,'T');SpTp[5].push_back(_t);
 //_t=CT();_t.Init(".**|..*|.**|...|*..|",0,'T');SpTp[5].push_back(_t);
 //_t=CT();_t.Init("**.|*..|**.|...|..*|",0,'T');SpTp[5].push_back(_t);
 _t=CT();_t.Init("..*|*..|",0,'S');SpTp[4].push_back(_t);
 _t=CT();_t.Init("*..|..*|",0,'Z');SpTp[6].push_back(_t);
 D NxtRdRes=1;
 F(D i=0;i<=TC;++i){
  BsT[i]=RT(BsT[i],InitRot);
  Ts[i][0]=BsT[i];
  F(D j=1;j<4;++j)Ts[i][j]=RT(Ts[i][j-1],1);
 }
 I(RSys==1){
  K[0][3]={{0,0},{-1,0},{-1,+1},{0,-2},{-1,-2}};
  K[3][0]={{0,0},{+1,0},{+1,-1},{0,+2},{+1,+2}};
  K[3][2]={{0,0},{+1,0},{+1,-1},{0,+2},{+1,+2}};
  K[2][3]={{0,0},{-1,0},{-1,+1},{0,-2},{-1,-2}};
  K[2][1]={{0,0},{+1,0},{+1,+1},{0,-2},{+1,-2}};
  K[1][2]={{0,0},{-1,0},{-1,-1},{0,+2},{-1,+2}};
  K[1][0]={{0,0},{-1,0},{-1,-1},{0,+2},{-1,+2}};
  K[0][1]={{0,0},{+1,0},{+1,+1},{0,-2},{+1,-2}};
  K[0][2]={{0,0},{0,+1},{+1,+1},{-1,+1},{+1,0},{-1,0}};
  K[2][0]={{0,0},{0,-1},{-1,-1},{+1,-1},{-1,0},{+1,0}};
  K[1][3]={{0,0},{-1,0},{-1,+2},{-1,+1},{0,+2},{0,+1}};
  K[3][1]={{0,0},{+1,0},{+1,+2},{+1,+1},{0,+2},{0,+1}};
  IK[0][3]={{0,0},{-2,0},{+1,0},{-2,-1},{+1,+2}};
  IK[3][0]={{0,0},{+2,0},{-1,0},{+2,+1},{-1,-2}};
  IK[3][2]={{0,0},{-1,0},{+2,0},{-1,+2},{+2,-1}};
  IK[2][3]={{0,0},{+1,0},{-2,0},{+1,-2},{-2,+1}};
  IK[2][1]={{0,0},{+2,0},{-1,0},{+2,+1},{-1,-2}};
  IK[1][2]={{0,0},{-2,0},{+1,0},{-2,-1},{+1,+2}};
  IK[1][0]={{0,0},{+1,0},{-2,0},{+1,-2},{-2,+1}};
  IK[0][1]={{0,0},{-1,0},{+2,0},{-1,+2},{+2,-1}};
  IK[0][2]={{0,0},{0,+1},{+1,+1},{-1,+1},{+1,0},{-1,0}};
  IK[2][0]={{0,0},{0,-1},{-1,-1},{+1,-1},{-1,0},{+1,0}};
  IK[1][3]={{0,0},{-1,0},{-1,+2},{-1,+1},{0,+2},{0,+1}};
  IK[3][1]={{0,0},{+1,0},{+1,+2},{+1,+1},{0,+2},{0,+1}};
 }
 vector<pair<D,D> >CCW={{0,0},{0,-1},{1,0},{1,-1},{0,-2},{1,-2},{2,0},{2,-1},{2,-2},{-1,0},{-1,-1},{0,1},{1,1},{2,1},{-1,-2},{-2,0},
 {0,2},{1,2},{2,2},{-2,-1},{-2,-2},{-1,1}},CW,FLIP;
 F(pair<D,D>p:CCW)CW.push_back(make_pair(-p.first,p.second));
 F(D i=0;i<CW.size();++i){
  FLIP.push_back(CCW[i]);FLIP.push_back(CW[i]);
 }
 I(RSys==2){
  F(D i=0;i<4;++i){
   F(D j=0;j<4;++j){
   	I(((i+1)&3)==j)K[i][j]=IK[i][j]=CCW;
   	I(((i+3)&3)==j)K[i][j]=IK[i][j]=CW;
   	I(((i+2)&3)==j)K[i][j]=IK[i][j]=FLIP;
   }}}
 I(RSys==3){
  Ts[0][2]=Ts[0][0];Ts[0][1]=Ts[0][3];
  F(D i=1;i<TC;++i){
   F(D j=0;j<4;++j)Ts[i][j]=FrameDrop(Ts[i][j]);
  }
  F(D i=0;i<4;++i){
   F(D j=0;j<4;++j){
    I(i!=j)K[i][j]=OK[i][j]={{0,0},{1,0},{-1,0}},IK[i][j]={{0,0}};
   }}}
 I(RSys==4){
  Ts[0][0]=Ts[0][2];Ts[0][3]=Ts[0][1];
  Ts[6][3]=Ts[6][1];Ts[6][0]=Ts[6][2];
  Ts[4][3]=Ts[4][1];Ts[4][0]=Ts[4][2];
  F(D i=0;i<4;++i){
   F(D j=0;j<4;++j){
    I(i!=j)K[i][j]=IK[i][j]=OK[i][j]={{0,0}};
   }}}
 I((RSys==5)||(!RSys)){
  I(RSys)Ts[0][0]=Ts[0][2];
  E Ts[0][2]=Ts[0][0];
  Ts[0][1]=Ts[0][3];
  Ts[6][1]=Ts[6][3];Ts[6][0]=Ts[6][2];
  Ts[4][1]=Ts[4][3];Ts[4][0]=Ts[4][2];
  F(D i=0;i<4;++i){
   F(D j=0;j<4;++j){
    I(i!=j)K[i][j]=IK[i][j]=OK[i][j]={{0,0}};
   }}}
 I(RSys==6){
  F(D i=0;i<4;++i){
   F(D j=0;j<4;++j){
    I(i==j)CN;
    I((i&1)==(j&1)){
     K[i][j]={{0,0},{0,+1},{0,-1},{-1,0},{+1,0}};
     IK[i][j]={{0,0},{0,-1},{0,+1}};
    }
    I(j==((i+1)&3)){
     K[i][j]={{0,0},{0,+1},{+1,0},{-1,0},{+1,+1},{-1,+1},{0,-1},{+1,-1},{-1,-1}};
    }
    I(j==((i+3)&3)){
     K[i][j]={{0,0},{0,+1},{-1,0},{+1,0},{-1,+1},{+1,+1},{0,-1},{-1,-1},{+1,-1}};
    }}}
  IK[0][3]={{0,0},{0,-1},{0,-2},{0,+1},{+1,-1},{-1,-1},{+1,-2},{-1,-2}};
  IK[3][0]={{0,0},{0,-1},{0,-2},{0,+1},{-1,0},{+1,0},{+2,0}};
  IK[3][2]={{0,0},{0,-1},{0,-2},{0,+1},{-1,0},{+1,0},{+2,0}};
  IK[2][3]={{0,0},{0,+1},{0,+2},{0,-1},{-1,+1},{+1,+1},{-1,+2},{+1,+2}};
  IK[2][1]={{0,0},{0,+1},{0,+2},{0,-1},{+1,+1},{-1,+1},{+1,+2},{-1,+2}};
  IK[1][2]={{0,0},{0,-1},{0,-2},{0,+1},{+1,0},{-1,0},{-2,0}};
  IK[1][0]={{0,0},{0,-1},{0,-2},{0,+1},{+1,0},{-1,0},{-2,0}};
  IK[0][1]={{0,0},{0,-1},{0,-2},{0,+1},{-1,-1},{+1,-1},{-1,-2},{+1,-2}};
 }
 F(D i=0;i<4;++i){
 F(D j=0;j<4;++j){
  I((!RSys)||(RSys==3)||(RSys==5)||(RSys==6))CN;
  I(((i+1)&3)==j)OK[i][j]=CCW;
  I(((i+3)&3)==j)OK[i][j]=CW;
  I(((i+2)&3)==j)OK[i][j]=FLIP;
 }}
 F(D i=0;i<=TC;++i){
  F(D j=0;j<4;++j){
   D FX=20, FY=20;
   F(D ii=0;ii<Ts[i][j].LenX;++ii){
    F(D jj=0;jj<Ts[i][j].LenY;++jj){
     I(Ts[i][j].Bl[ii][jj])FX=min(FX,ii),FY=min(FY,jj);
	}
   }
   Ts[i][j].FX=FX;Ts[i][j].FY=FY;
  }
 }
 F(D d=0;d<TC;++d){
  I(LenY<4)CN;
  D _=0;F(D i=0;i<WTC;++i)I(TDy[i]==d)_=1; 
  I(!_)CN;
  Spw(Ts[d][0],d,0,1);
  F(D i=0;i<LenX;++i){
   F(D j=0;j<LenY;++j){
   	I(B[i][j])SpPoi[i][j]=TSpPoi[d][i][j]=1,B[i][j]=C[i][j]=0;
   }
  }
 }
 I(PartTopOut){
  D Li=(LenY>=4)?DisX-1:DisX-3;
  F(D i=Li;i<LenX;++i){
   F(D j=0;j<LenY;++j){
    SpPoi[i][j]=1;F(D d=0;d<TC;++d)TSpPoi[d][i][j]=1;
   }
  }
 }
 QuLen=1;H=-1;
 F(D i=0;i<max(1,NxtT);++i)TQu[i]=RndT();
 F(D i=0;i<BsGH;++i)AddG(Rnd.Rand(LenY),0);
 TID=NewT();D TSt=SpwTSt;
 HAv=1;
 STs=SLs=Ren=0;
 MsRe=0;V=StartV;
 LD LastA=BegTm=KeyStTm=clock();
 LastA=BegTm=KeyStTm=clock();
 MsA=MsB="          ";
 ARe=0;
 D ResCd=0;
 LD LockTmEnd=Sr=0LL;
 LD DropTmEnd=0LL;
 BU=1;
 D RHg=(LenX<<10);
 D HRen=0,CInp=0;char LAct='!';bool fst=1,LqSp=0;
 D SecG=0;
 F(D i=0;i<4;++i)tO[i]=Ts[3][i];
 bool LEff=1;D _ontl=NTmLi,VD=0;
 #define _ AddBac
 _(H);_(HAv);_(STs);_(SLs);_(SD);_(SInp);_(SA);_(Ren);_(SF);_(SFF);_(Chain);_(SecG);
 _(FSTs);_(FSLs);_(FSD);_(FSInp);_(FSFInp);_(FSA);_(FSF);_(FSFF);_(F2Poi);
 _(PSz);F(D i=0;i<max(100,WTC*5);++i)_(TPool[i]);
 _(ARe);F(D i=0;i<1000;++i)_(AQu[i]);
 F(D i=0;i<LenX;++i){
  F(D j=0;j<LenY;++j)_(B[i][j]),_(C[i][j]);
 }
 F(D i=0;i<=TC;++i)_(B2B[i]);
 F(D i=0;i<max(1,NxtT);++i)_(TQu[i]);
 _(ResB);_(V);_(VD);_(TID);_(TSt);_(Win);_(Debug);_(TLE);
 F(D i=0;i<max(5,WTC);++i)_(TCnt[i]);
 #undef _
 Storage();D sz=BlockSz;
 D UseH=0;LD LastSoftD=0LL;
 D ResetReq=0;
 D PoiUse=0;
 LD LAutoTm=0LL,AutoSpeed=30LL;
 F(;;){
  V=StartV;AGC=0;Patk();
  I(VUpLs)V+=SLs/VUpLs;
  I(VUpTs)V+=STs/VUpTs;
  I(VUpTm)V+=D((clock()-BegTm)/1000LL/VUpTm);
  I(VUpA)V+=SA/VUpA;
  I(MaxV)V=min(V,MaxV);
  V+=VD;
  bool f=CkD();FL Mp=VMul(V);
  BonusMp=1.0/max(0.01,Mp);
  _ntl=(NTmLi<0)?NTmLi:D(ceil(FL(NTmLi)*Mp));
  _gr=Gravity;
  I((DropDelayToG)&&(_ntl<DropDelayToG)){
   _gr=min(LenX,Gravity*((_ntl>=1)?DropDelayToG/_ntl:LenX));_ntl=DropDelayToG;
  }
  I(TID==TC)_gr=0;
  D _ltl=(LTmLi<0)?LTmLi:D(ceil(FL(LTmLi)*pow(Mp,0.2)));
  D _are=(ARE<0)?ARE:D(ceil(FL(ARE)*Mp));
  D _lcd=(LCD<0)?LCD:D(ceil(FL(LCD)*Mp));
  _sdd=(SDD<0)?SDD:D(ceil(FL(SDD)*Mp));
  I(f){
   I(!DropTmEnd){
    DropTmEnd=(LD)(clock())+(LD)_ntl;I(LockTmEnd)DropTmEnd=min(LockTmEnd,DropTmEnd);
   }
   LockTmEnd=0LL;
  }
  E {
   I(!LockTmEnd){
    LockTmEnd=(LD)(clock())+(LD)_ltl;I(DropTmEnd)LockTmEnd=min(LockTmEnd,DropTmEnd);
   }
   DropTmEnd=0LL;
  }
  LD cur=clock(),Duration=cur;
  char ch=K_REFRESH;
  I(((LEff)&&(MvsLeft>0)&&(ResetReq))||(NxtRdRes)){
  	DropTmEnd=cur+_ntl;LockTmEnd=cur+_ltl;VerDel+=LDRate;NxtRdRes=PoiUse=0;
  }
  EI(PoiUse&&MvsLeft>0){
   ++MvsLeft;PoiUse=0;
  }
  E PoiUse=0;
  char PKey=LKey;D PreSp=0;
  I(fst)fst=0;
  E{
   I(Auto){
   	LD Rem=(TID==TC)?1000000000LL:((LD)(f?(DropTmEnd-clock()):(LockTmEnd-clock())));
   	I(clock()-LAutoTm<=AutoSpeed&&Rem>0LL&&((Oper.empty())||(Oper.top().first!=K_SOFTDROP)))ch='?';
	E{
     LAutoTm=clock();
     I(UseH)ch=K_HOLD,UseH=0;
     EI(Oper.size()){
     ch=Oper.top().first;ARep=Oper.top().second;Oper.pop();}
     E ch=K_HARDDROP,ARep=0;
	}
   }
   E ch=GetKeyInLi((TID==TC)?1000000000LL:((LD)(f?(DropTmEnd-clock()):(LockTmEnd-clock()))));
  }
  Duration=clock()-Duration;I(!ArrK)PKey='!';
  I(ch==K_SOFTDROP){
   I(LastSoftD+200LL>=clock())PKey=K_SOFTDROP;
   LastSoftD=clock();
  }
  D NxtRd=Sight=0;ResetReq=!CkD();
  I((ch!='!')&&(ch!=K_HARDDROP)&&(ch!=K_HOLD)&&(TID!=TC)&&(ch!=K_SOFTDROP))--MvsLeft,PoiUse=1;
  I(isupper(ch))ch=tolower(ch);
  D HCg=0,Con=((Zen)&&(!Auto)&&(gks(K_CON))),Undo=0;
  OnGround=!CkD();D HTo=-1;LEff=1;
  I((!Con)&&(FixedFac)&&((ch==K_ROT)||(ch==K_CCW)||(ch==K_CW)||(ch==K_180)))ch='~';
  I(ch!=K_RETRY){
   I(ResCd)--ResCd;
   E ResB=max(0,ResB-10);
  }
  I(((ch!=K_CCW)&&(ch!=K_CW))||(!OnGround))HRen=0;
  I(ch==K_LEFT&&!Con){LastSoftD=0LL;
   I((ARR<0)&&(ARep)){
   	LEff=0;
    W(CkL())MvLeft((TID==TC)&&(gks(K_HOLDPLACE))),LEff=1,GR=0,HoriDel+=1.0;
   }
   EI(!MvLeft((TID==TC)&&(gks(K_HOLDPLACE))))LEff=0;
   E GR=0,HoriDel+=1.0;
   I((LEff)&&((PKey!=K_LEFT)||(!CInp))&&(!Auto||!ARep))++CInp,++SInp,++SFInp;
  }
  EI(ch==K_RIGHT&&!Con){LastSoftD=0LL;
   I((ARR<0)&&(ARep)){
   	LEff=0;
    W(CkR())MvRight((TID==TC)&&(gks(K_HOLDPLACE))),LEff=1,GR=0,HoriDel-=1.0;
   }
   EI(!MvRight((TID==TC)&&(gks(K_HOLDPLACE))))LEff=0;
   E GR=0,HoriDel-=1.0;
   I((LEff)&&((PKey!=K_RIGHT)||(!CInp))&&(!Auto||!ARep))++CInp,++SInp,++SFInp;
  }
  EI((ch==K_EDIT)&&(Con)&&(TID!=TC)){
   I(R(Ts[TID][TSt],Ts[TC][TSt],TSt,TSt)){
    TID=TC;MvsLeft=10000;Debug=1;
   }
  }
  EI((ch>='1')&&(ch<='9')&&(TID!=ch-'0'-1)&&(Con)){
   I(R(Ts[TID][TSt],Ts[ch-'0'-1][TSt],TSt,TSt)){
    TID=ch-'0'-1;MvsLeft=10000;Debug=1;
   }
   E I(R(Ts[TID][TSt],Ts[ch-'0'-1][(TSt+1)&3],TSt,(TSt+1)&3)){
    TSt=(TSt+1)&3;TID=ch-'0'-1;MvsLeft=10000;Debug=1;
   }
   E I(R(Ts[TID][TSt],Ts[ch-'0'-1][(TSt+2)&3],TSt,(TSt+2)&3)){
    TSt=(TSt+2)&3;TID=ch-'0'-1;MvsLeft=10000;Debug=1;
   }
   E I(R(Ts[TID][TSt],Ts[ch-'0'-1][(TSt+3)&3],TSt,(TSt+3)&3)){
    TSt=(TSt+3)&3;TID=ch-'0'-1;MvsLeft=10000;Debug=1;
   }
  }
  EI((Con)&&(ch==K_UNDO)&&(Bac))Undo=NxtRd=1;
  EI((Con)&&(ch==K_ADDLAYER))AddG(Rnd.Rand(LenY)),snd(116,60),LEff=0,Debug=1;
  EI((Con)&&(ch==K_SIGHT)&&(BlindLv))Sight=Debug=1,LEff=0;
  EI((Con)&&(ch==K_LOCK))Debug=NxtRd=1;
  EI(((Con)||(TID==TC))&&(ch==K_SKIP)){
  	F(D i=0;i<=DisX;++i){
     F(D j=0;j<LenY;++j){
      I(!C[i][j])CN;
      D lx=(DisX-i+5)*sz-80+RealDRate,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
      F(D d=0;d<5;++d){
       FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
       PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.1,0.05,0.5*sin(theta),0.5*cos(theta),0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.3)});
      }
	 }
   }
   F(D i=0;i<LenX;++i){
    F(D j=0;j<LenY;++j)I(C[i][j])B[i][j]=C[i][j]=0; 
   }
   NxtRd=Debug=1;
  }
  EI((Con)&&(ch==K_DECLV))--VD,LEff=0;
  EI((Con)&&(ch==K_INCLV))++VD,LEff=0;
  EI((Con)&&(ch==K_HOLD)&&(EnH)){
   LEff=0;HAv=Debug=1;
  }
  EI((ch==K_HINT)&&(Zen)&&((Con)||(!BlindLv))){
  	Hint=BU=1,LEff=0;I(BlindLv)Debug=1;
  	F(D i=0;i<=DisX;++i){
     F(D j=0;j<LenY;++j){
      I(!HM[i][j])CN;
      D lx=(DisX-i+5)*sz-80+RealDRate,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
      F(D d=0;d<5;++d){
       FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
       PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.2,0.05,0.5*sin(theta),0.5*cos(theta),0,0,Sc.Mix(0xFFFFFF,ShiftColor(BsT[TID].Color),0.3)});
      }
	 }
   }
  }
  EI((ch==K_FILLCOL)&&(TID==TC)){
   F(D j=0;j<LenY;++j){
    D _=0;
    F(D i=0;i<LenX;++i)_|=C[i][j];
    I(_)F(D i=0;i<LenX;++i)I(!B[i][j])B[i][j]=Ts[TC][0].Color;
   }
   LEff=0;
  }
  EI((ch==K_FILLROW)&&(TID==TC)){
   F(D j=0;j<LenX;++j){
    D _=0;
    F(D i=0;i<LenY;++i)_|=C[j][i];
    I(_)F(D i=0;i<LenX;++i)I(!B[j][i])B[j][i]=Ts[TC][0].Color;
   }
   LEff=0;
  }
  EI((ch==K_DELCOL)&&(TID==TC)){
   F(D j=0;j<LenY;++j){
    D _=0;
    F(D i=0;i<LenX;++i)_|=C[i][j];
    I(_)F(D i=0;i<LenX;++i)I(!C[i][j])B[i][j]=0;
   }
   LEff=0;
  }
  EI((Con)&&(ch==K_DELLAYER)){
   LEff=0;D _=(OnGround)?0:LenX;Debug=1;
   F(D j=0;j<LenY;++j){
    I(!B[0][j])CN;
    D lx=(DisX+5)*sz-80,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
    PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,3.0,0.05,-2.0,(Rnd.Rand(11)-5)*0.1,0.2,0,0xFF6666});
   }
   F(D i=0;i<_;++i){
   	F(D j=0;j<LenY;++j)B[i][j]=B[i+1][j],C[i][j]=C[i+1][j]; 
   }
  }
  EI(ch==K_RETRY&&!Con){
   ResB+=2;ResCd=5;
  }
  EI((ch==K_UP)&&((TID==TC)||(Con))){
   I (MvUp((TID==TC)&&(gks(K_HOLDPLACE))))VerDel+=1.0;MvsLeft=10000;LEff=1;Debug=1;RHg=(LenX<<10);
  }
  EI((((ch==K_ROT)&&(!DefaultR)||((ch==K_CCW)&&((!DefaultR)||(Sp2S)))))&&!Con){LastSoftD=0LL;
   I(LAct!=K_CW)HRen=0;I(FixedFac)Debug=1;
   E ++HRen;D nw=GetTHeight();
   I(nw<RHg||1){
    RHg=nw;//LockTmEnd=0LL;
   }
   I(LEff=R(Ts[TID][TSt],Ts[TID][(TSt+1)&3],TSt,(TSt+1)&3))TSt=(TSt+1)&3,++CInp,++SInp,++SFInp;
   I(CkU()+CkD()+CkL()+CkR()==0)PreSp=1;
  }
  EI((((ch==K_ROT)&&(DefaultR))||((ch==K_CW)&&((DefaultR)||(Sp2S))))&&!Con){LastSoftD=0LL;
   I(LAct!=K_CCW)HRen=0;I(FixedFac)Debug=1;
   E ++HRen;D nw=GetTHeight();
   I(nw<RHg||1){
    RHg=nw;//LockTmEnd=0LL;
   }
   I(LEff=R(Ts[TID][TSt],Ts[TID][(TSt+3)&3],TSt,(TSt+3)&3))TSt=(TSt+3)&3,++CInp,++SInp,++SFInp;
   I(CkU()+CkD()+CkL()+CkR()==0)PreSp=1;
  }
  EI((ch==K_180)&&(Sp180)&&!Con){LastSoftD=0LL;
   I(FixedFac)Debug=1;
   I((Sp180)&&(LEff=R(Ts[TID][TSt],Ts[TID][(TSt+2)&3],TSt,(TSt+2)&3)))TSt=(TSt+2)&3,++CInp,++SInp,++SFInp;
   D nw=GetTHeight();
   I(nw<RHg||1){
    RHg=nw;//LockTmEnd=0LL;
   }
   I(CkU()+CkD()+CkL()+CkR()==0)PreSp=1;
  }
  EI(ch==K_HARDDROP&&!Con){LastSoftD=0LL;
   D _f=(TID==TC);
   W((TID!=TC)&&(CkD())){
    MvDown();_f=1;I(!Debug)Sr+=2LL;
    F(D i=0;i<=DisX;++i){
     F(D j=0;j<LenY;++j){
      I(!C[i][j])CN;
      D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
      PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),Rnd.Rand(15)*0.1+0.1,0.05,-1.5,0,0,0,ShiftColor(B[i][j])});
	 }
	}
   }
   F(D i=0;i<LenX;++i){
    F(D j=0;j<LenY;++j){
     I((_f)&&(C[i][j]))B[i][j]|=1024;
    }}
   NxtRd=1;
  }
  EI(ch==K_HOLD&&!Con){
   I((EnH)&&(HAv)){
    I(H>=0){
     swap(H,TID);HTo=TID;
    }
    E H=TID;
    NxtRd=HCg=1;HAv=0;
    F(D i=0;i<=DisX;++i){
     F(D j=0;j<LenY;++j){
      I(!C[i][j])CN;
      D lx=(DisX-i+5)*sz-80+RealDRate,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
      F(D d=0;d<5;++d){
       FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
       PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.1,0.05,0.5*sin(theta),0.5*cos(theta),0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.3)});
      }
	 }
	}
    RemoveC();
   }E LEff=0;}
  EI(((ch=='3')||(ch=='4')||(ch=='0'))&&(TID==TC)){
   D _=(ch=='3')?BsT[TC].Color:8;I(ch=='0')_=0;
   F(D i=0;i<LenX;++i){
   	F(D j=0;j<LenY;++j)I(C[i][j])M[i][j]=_;
   }
  }
  EI(((ch=='1')||(ch=='2'))&&(TID==TC)){
   D _=0;F(D i=0;i<=TC;++i)I(Ts[TC][0].Color==BsT[i].Color)_=i;
   I(ch=='2')_=(_==TC)?0:_+1;
   E _=(_)?_-1:TC;
   F(D i=0;i<4;++i)Ts[TC][i].Color=BsT[_].Color;
   F(D i=0;i<LenX;++i){
   	F(D j=0;j<LenY;++j)I(C[i][j])B[i][j]=BsT[_].Color;
   }
  }
  EI((ch==K_SOFTDROP&&!Con)||(ch=='!')){
   _ntlp=0;
   I(ch==K_SOFTDROP)LEff=0;
   F(D i=0;i<((ch=='!')?((TID==TC)?0:_gr):max(((SDD<0)&&(!Auto))?LenX:1,_gr));++i){
    I(CkD()){
     GR=0;I(ch==K_SOFTDROP)Sr+=(!Debug),LEff=1,VerDel-=1.0-LDRate/double(max(1,_gr)),_ntlp=0;
     MvDown((TID==TC)&&(gks(K_HOLDPLACE)));D nw=GetTHeight();
     I(nw<RHg||1){
      RHg=nw;DropTmEnd=0LL;NxtRdRes=1;
     }}
    EI((ch=='!')&&(!i)){
     NxtRd=1;MvsLeft=MvsLi;BK;
    }}
   I((ch==K_SOFTDROP)&&(LEff)&&(PKey!=K_SOFTDROP))++SInp,++SFInp;}
  EI(ch==K_PAUSE&&!Con){
   LD _s=clock();LEff=1;
   W(kbhit())getch();
   Sleep(50);getch();BegTm+=clock()-_s;
  }
  EI(ch==K_REFRESH&&!Con){
   system("cls");BU=1;memset(aa,0,sizeof(aa));
  }
  E LEff=0;ResetReq|=!CkD();
  I((PreSp)||(LqSp)){
   F(D i=0;i<=DisX;++i){
    F(D j=0;j<LenY;++j){
     I(!C[i][j])CN;
     D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
     F(D d=0;d<10;++d)PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),0.1*(15+Rnd.Rand(11)),0.01,0.3,0,0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.2)});
	}
   }
  }
  I(((ch==K_LEFT)||(ch==K_RIGHT)||(ch==K_SOFTDROP))&&((LEff)||(!ARep)))snd(118,((LEff)&&(!ARep))?48:35);
  I((ch==K_ROT)||(ch==K_CCW)||(ch==K_CW)||(ch==K_180))snd(115,(LEff)?48:43);
  I((LEff)&&(ARep)){
   F(D i=0;i<=DisX;++i){
     F(D j=0;j<LenY;++j){
      I(!C[i][j])CN;
      D lx=(DisX-i+5)*sz-80+RealDRate,ly=j*sz+200;
      PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),(Rnd.Rand(5)+10)*0.1,0.03,0,0,0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.3)});
	 }
   }
  }
  D Over=0;LAct=ch;
  I((TID==3)&&(OSp)&&(HRen>=12)&&(!LqSp)){
   bool flag=1;
   D ey=-1,ex=-1,my=-1,m=-1;
   F(D j=0;j<LenY;++j){
    F(D i=1;i<LenX;++i){
     I((!B[i-1][j])&&(C[i][j])){
      ey=j;ex=i;BK;
     }
     I((B[i-1][j])&&(C[i][j])){
      my=j;m=(B[i-1][j]&63);BK;
     }}}
   I((ey==-1)||(my==-1)||((m!=BsT[0].Color)&&(m!=BsT[1].Color)&&(m!=BsT[2].Color)
   &&(m!=BsT[4].Color)&&(m!=BsT[5].Color)&&(m!=BsT[6].Color)))flag=0;
   vector<pair<D,D> >FST={{1,0},{1,0},{0,1},{0,0},{0,-1},{1,0},{0,1}},SEC={{2,0},{1,1},{0,2},{0,0},{1,-1},{0,1},{1,1}};
   I(ey==my+1)FST={{1,0},{0,-1},{1,0},{0,0},{0,-1},{1,0},{0,1}},SEC={{2,0},{0,-2},{1,-1},{0,0},{1,-1},{0,-1},{1,1}};
   I((ey!=my+1)&&(ey!=my-1))flag=0;
   I(flag){
    F(D i=0;i<TC;++i)I((i!=3)&&(m==BsT[i].Color)&&((CheckT(ex-1,ey))
    ||(CheckT(ex+FST[i].first,ey+FST[i].second))||(CheckT(ex+SEC[i].first,ey+SEC[i].second))))flag=0;
   }
   I(flag){
    F(D i=0;i<LenX;++i){
     F(D j=0;j<LenY;++j){
      I(C[i][j]){
       C[i][j]=0;B[i][j]=0;
      }}}
    C[ex][ey]=1;B[ex][ey]=BsT[3].Color;
    LqSp=1;B[ex-1][ey]=B[ex][ey];C[ex-1][ey]=1;
    vector<D>FAC={1,3,2,0,1,3,1};I(ey==my+1)FAC={3,2,1,0,3,1,3};
    F(D i=0;i<TC;++i){
     I(m==BsT[i].Color){
      TSt=FAC[i];
      B[ex+FST[i].first][ey+FST[i].second]=B[ex][ey];C[ex+FST[i].first][ey+FST[i].second]=1;
      B[ex+SEC[i].first][ey+SEC[i].second]=B[ex][ey];C[ex+SEC[i].first][ey+SEC[i].second]=1;
	 }
	}
    F(D i=0;i<TC;++i){
     I(m==BsT[i].Color){
      F(D j=0;j<4;++j){
       Ts[3][j]=Ts[i][j];Ts[3][j].Color=tO[j].Color;
      }
      BK;
     }}
    NTmLi=min(NTmLi,50);
   }}
  bool _adl=0;
  I(NxtRd){LastSoftD=0LL;MvsLeft=MvsLi;NxtRdRes=1;
   F(D i=0;i<4;++i)Ts[TC][i].Color=BsT[TC].Color;
   F(D i=0;i<LenX;++i){
    F(D j=0;j<LenY;++j){
     I((!HM[i][j])||(!Hint))CN;
     D lx=(DisX-i+5)*sz-80,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
      F(D d=0;d<5;++d){
       FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
       PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.1,0.05,0.5*sin(theta),0.5*cos(theta),0,0,ShiftColor(BsT[TID].Color)});
      }
	}
   }
  }
  I(Undo){
   MsA=MsB="          ";CInp=LqSp=GR=HRen=ThisA=Clu=cl=LqSp=Hint=0;sndl(120,60);
   RHg=(LenX<<10);MvsLeft=MvsLi;
   I(LqSp){
    NTmLi=_ontl;
    F(D i=0;i<4;++i)Ts[3][i]=tO[i];
   }
   Backup();CFin(TID,TSt);
   W(gks(K_UNDO)&&gks(K_CON));
  }
  EI(NxtRd){
   D _nl=LenX,CluP=Hint=0;HoriDel=VerDel=0.0;
   F(D i=0;i<LenX;++i){
   	F(D j=0;j<LenY;++j){
     I(!C[i][j])CN;
     D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
     F(D d=0;d<5;++d)PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),0.1*(Rnd.Rand(5)+10),0.01,0.2,0,0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.2)});
	}
   	F(D j=0;j<LenY;++j)I(!B[i][j]){
     --_nl;BK;
    }
   }
   F(D i=0;i<LenX;++i){
    F(D j=0;j<LenY;++j){
     I((C[i][j])&&(TSpPoi[TQu[0]][i][j])){
      ++CluP;
      D lx=(DisX-i+5)*sz-80,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
      F(D d=0;d<15;++d){
        FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
        PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.3,0.05,sin(theta),cos(theta),0,0,0xFDD000});
      }
	 }
	}
   }
   D _dl=(HCg)?-1:(((_nl>=LCSt)&&(LCLi))?_lcd:_are);
   I(_dl>=0){
	LD cur=clock();InitH=InitR='!';AreStart=clock();Ds(2);_adl=1;
	D sz=DelPx.size();sort(DelPx.begin(),DelPx.end(),[](pair<D,D>x,pair<D,D>y){RE x.first*2+x.second>y.first*2+y.second;});
	W (clock()-cur<_dl){
     DsTm();
     CheckKey(1);
	 I(sz){
	  W((DelPx.size())&&(FL(clock()-cur)/FL(_dl)>=FL(sz-DelPx.size())/FL(sz))){
	   Sc.Plot(DelPx.back().first,DelPx.back().second,0);DelPx.pop_back();
	  }
	 }
	}
	W(DelPx.size())Sc.Plot(DelPx.back().first,DelPx.back().second,0),DelPx.pop_back();
   }
   I(LqSp){
    NTmLi=_ontl;
    F(D i=0;i<4;++i)Ts[3][i]=tO[i];
   }
   LAct='!';D NL=HRen=0;
   I(!HCg){
    I(ch==K_HARDDROP)snd(13,48);
    E snd(13,45);
    MsA=MsB="          ";MsCB=15;::LqSp=LqSp;
    D FX=LenX,FY=LenY;
	F(D i=0;i<LenX;++i){
     F(D j=0;j<LenY;++j){
      I(C[i][j])FX=min(FX,i),FY=min(FY,j);
	 }
	}
    D JAns=Fin[FX][FY][TSt];
    I(TID==3)JAns=min(min(Fin[FX][FY][0],Fin[FX][FY][1]),min(Fin[FX][FY][2],Fin[FX][FY][3]));
    EI((TID==4)||(TID==6)||(!TID))JAns=min(JAns,Fin[FX][FY][(TSt+2)&3]);
    D NewFF=max(0,SFInp-JAns);I(LqSp)NewFF=0;
    SFF+=NewFF;I(!NewFF)++SF;
    D PreCk=(!CkL())&&(!CkR()),Sp=FinalDrop(),BaseDig=SD;
    I((!Sp)&&(GR)&&(PreCk))Sp=1,FullSp=0;
	NL=Chain=0;
	F(;;){
     D nw=RmL();I(!nw)BK;
     NL+=nw;++Chain;I(LCG<2)BK;
     _nl=LenX;
	 F(D i=0;i<LenX;++i){
      F(D j=0;j<LenY;++j)I(!B[i][j]){
      --_nl;BK;
      }
     }
     I((!_nl)||(!LCLi))BK;
     D _dl=_lcd;
     MsB=LCW[NL];MsRe=250;
     I(_dl>=0){
      LD cur=clock();InitH=InitR='!';AreStart=clock();Ds(2);_adl=1;
      D sz=DelPx.size();sort(DelPx.begin(),DelPx.end(),[](pair<D,D>x,pair<D,D>y){RE x.first*2+x.second>y.first*2+y.second;});
      W (clock()-cur<_dl){
       DsTm();
       CheckKey(1);
       I(sz){
        W((DelPx.size())&&(FL(clock()-cur)/FL(_dl)>=FL(sz-DelPx.size())/FL(sz))){
         Sc.Plot(DelPx.back().first,DelPx.back().second,0);DelPx.pop_back();
        }
       }
      }
      W(DelPx.size())Sc.Plot(DelPx.back().first,DelPx.back().second,0),DelPx.pop_back();
     }
	}
	I(QSecGr()>SecG){
     D _=SecG;SecG=QSecGr();
     F(D i=_;i<=min(DisX,SecG-1);++i){
      F(D j=0;j<LenY;++j){
       D lx=(DisX-i+5)*sz-80,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
       F(D d=0;d<SecG+2;++d){
         FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
         PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*(0.1+SecG*0.01),0.05,sin(theta),cos(theta),0,0,0x39C5BB});
       }
      }
     }
	}
    I(NL)++Ren;
    E {
     Ren=0;W((PQu.size())&&(PQu.back().clr==0xFDD000))PQu.pop_back();
     F(D i=0;i<LenX;++i){
      F(D j=0;j<LenY;++j)BMark[i][j]=0;
     }}
    SLs+=NL;LD NSr=0LL;cl=min(NL,4);
    I(NL)MsB=LCW[NL];
    D NewA=0;Clu=(NL)?CluP:0;
    I(NL){
     I(NL>1)NewA=min(1<<(NL-2),NL);
     I(((Sp)&&(FullSp))&&((TID==5)||(B2BTp))){
      D _=NL<<1;//I((TID)&&(TID!=5)&&(TID!=3))--_;
      NewA=max(_+(NL>=4),NewA);
	 }
     I((Sp)&&(!FullSp)&&(NL>=2)&&((TID==4)||(TID==5)||(TID==6)))NewA=max(NewA,5);
    }
    I(LqSp)Sp=1;
    I((AllowSp!=2)&&(TID!=5))Sp=0;
    I(!AllowSp)Sp=0;
    I(NL==1)NSr+=100LL;
    I(NL==2)NSr+=300LL;
    I(NL==3)NSr+=500LL;
    I(NL>=4)NSr+=800LL+300LL*(NL-4);
    I(Sp){
     MsCA=BsT[TID].Color;SPTID=TID;sndl(114,52);sndl(114,50);sndl(114,54);I(FullSp)sndl(114,52),sndl(114,47);
     MsA="    ?-SPIN";MsA[4]=BsT[TID].Name;
     I((TID==5)||(B2BTp)){
      I(!FullSp)NSr+=(NL<=1)?100LL:((TID>=4)?((TID==5)?1200LL:1000LL):500LL);
      E {
       I(NL)NSr+=500LL+200LL*NL;
       E NSr+=400LL;
       I((TID)&&(TID!=5)&&(TID!=3))NSr-=150LL;
	  }
     }}
    I((Ren>1)&&(LenY>4)&&(LCSt==1))NSr=NSr*(3LL+Ren)/4LL;
    I(Ren>1)NSr+=50LL*((LenY>4)?Ren:min(Ren,LenY*LenY+LenY-2));
    I(NL){
     I((NL>=4)||((!LqSp)&&(Sp))){
      I(!B2BTp){
       ++B2B[TC];I(B2B[TC]>1)NSr=(LD)ceil(FL(NSr)*(3.0-1.5*pow(0.8,B2B[TC]-2)));
       I(B2B[TC]>1)NewA+=1+(B2B[TC]>5)+(B2B[TC]>20)+(B2B[TC]>40);
	  }
      E{
       I(NL>=4){
        B2B[TC]+=10;F(D i=0;i<TC;++i)B2B[i]+=5;
	   }
	   I((!LqSp)&&(Sp)){
        B2B[TID]=max(B2B[TID],B2B[TC]);B2B[TID]+=(FullSp)?10:(((NL>1)&&((TID==4)||(TID==5)||(TID==6)))?18:6);
        F(D i=0;i<TC;++i){
         I(i!=TID)B2B[i]=max(0,B2B[i]-20);
	    }
	   }
       D MB2B=0; F(D i=0;i<=TC;++i)MB2B=max(MB2B,B2B[i]);
       I(MB2B>=19){
        NewA+=1+(MB2B>=50)+(MB2B>100);NSr=(LD)ceil(FL(NSr)*(3.0-1.5*pow(0.8,MB2B-2)));
       }
       F(D i=0;i<=TC;++i)B2B[i]=min(110,B2B[i]);
	  }
	 }
     E {
      I(!B2BTp)B2B[TC]=0;
	  E{
	   F(D i=0;i<=TC;++i)B2B[i]=max(0,B2B[i]-50);
	  }
	 }
	 I(Ren>=2)NewA+=NewA*(Ren-1)/4;
     I((!NewA)&&(Ren>=3))++NewA;
     I((NewA<2)&&(Ren>=7))++NewA;
     I((NewA<3)&&(Ren>=17))++NewA;
     I(CheckAC()){
      I(B2BTp){
       F(D i=0;i<=TC;++i)B2B[i]=105;
	  }
      MsCB=14;MsB="ALL CLEAR ";NewA+=(U4+U5)?LenY:min(LenY,6);
     }
     SA+=NewA;ThisA=NewA;
     W((NewA)&&(ARe)){
      RemoveG();--NewA;
     }
     NewA=min(NewA,SALi);
     D y=Rnd.Rand(LenY);
     NewA=NewA*BFRate/100;
     I(BEL)F(D i=0;i<NewA;++i)if(CRe<1000)CQu[CRe++]=y;
     I(!AEnabled)NewA=0;
     F(D i=0;i<NewA;++i)if(ARe<1000)AQu[ARe++]=y;
    }
    E {
     I(Sp){
      I(B2BTp){
       F(D i=0;i<=TC;++i)B2B[i]+=1;
       I(FullSp)B2B[TID]+=4;
	    E B2B[TID]+=2;
        F(D i=0;i<=TC;++i)B2B[i]=min(110,B2B[i]);
	  }
	 }
	 EI(B2BTp){
	  F(D i=0;i<=TC;++i){
       I(B2B[i]>100)--B2B[i];
	  }
	 }
     ThisA=0;
     D AReceive=min(GLi,ARe);
     W(AReceive--)snd(116,60),EnableG();
    }
    D _=0;
	F(D i=0;i<=TC;++i)_=max(_,48+max(0,max(Ren,B2B[i]/((B2BTp)?19:1))-1));
    F(D i=0;i<NL;++i)sndl(10,min(72,_++));
    NSr+=100LL*Clu;
    I(MsB=="ALL CLEAR "){
     NSr=NSr*min(LenY-1,4)+2500LL+500LL*max(0,LenY-6),sndl(1,67),sndl(1,64),sndl(1,67),sndl(1,69),sndl(1,72);
     F(D i=0;i<=DisX;++i){
      F(D j=0;j<LenY;++j){
       D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
       F(D d=0;d<5;++d)PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),Rnd.Rand(15)*0.1+0.2,0.01,0.5,0,0,0,(Rnd.Rand(2))?0xFFA500:0xFFE211});
	  }
	 }
	}
    NSr=LD(round(double(NSr)*ScrMul));I(NL)NSr=max(LD(NL),NSr);
    MsRe=250;I(SD>BaseDig)LastA=clock();
    HAv=1;++STs;SFInp=0;I(!Debug)Sr+=(LD)ceil(BonusMp*FL(NSr));
   }
   E sndl(113,50);
   D UH=0;pair<LD,LD>HF=make_pair(0LL,0LL);
   Mode=0;
   F(D i=LenX-1;i>=0;--i){
   	I(i*3/2<DisX)BK;
    //F(D j=0;j<LenY;++j)I(B[i][j])Mode=1;
   }
   I((HAv)&&(EnH)&&(!HCg)){
    D tmp=NewT((H==-1)?TQu[1]:H);
    I(tmp!=-1){
     HF=LstMnF;UH=1;
     F(D i=0;i<LenX;++i)F(D j=0;j<LenY;++j)I(C[i][j])B[i][j]=C[i][j]=0;
	}
   }
   I(TID==TC)TID=0;
   TID=NewT(HTo);CInp=LqSp=GR=_ntlp=0;
   I((UH)&&(HF<LstMnF))UseH=1;
   RHg=(LenX<<10);MvsLeft=MvsLi;
   I(TID==-1)Over=1;
   E{
    TSt=SpwTSt;I(!HCg)Storage();
   }
  }
  bool GNotEnough=1;
  I(BsGH>0){
   F(D i=0;i<LenY;++i){
    I((B[BsGH-1][i]&63)==8){
     GNotEnough=0;BK;
    }}
  I(!GTp){
  	GNotEnough=1;
	F(D i=BsGH;i<LenX;++i){
	 F(D j=0;j<LenY;++j){
	  I(CheckT(i,j))GNotEnough=0;
	 }}
  	F(D i=0;i<LenY;++i){
  	 I (CheckT(BsGH-1,i))GNotEnough=1;
	}}}
  I((!Over)&&(NxtRd)&&(AByT)&&(STs%AByT==0)&&(!HCg))AddG(Rnd.Rand(LenY),0),snd(116,60);
  I((!Over)&&(GNotEnough)&&(AByTm)&&(clock()-LastA>(LD)(AByTm))){
   LastA=clock();snd(116,60);AddG(Rnd.Rand(LenY),1);
  }
  KeyStTm=clock();LD CurTm=clock()-BegTm;
   I((GoalLs)&&(((GoalTp)?SD:SLs)>=GoalLs)&&(!Win)){
   F(D i=0;i<=DisX;++i){
    F(D j=0;j<LenY;++j){
     D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
     F(D d=0;d<5;++d)PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),Rnd.Rand(15)*0.1+0.2,0.01,0.5,0,0,0,(Rnd.Rand(2))?0x66CCFF:0x00FFCC});
    }
   }
   Win=1;GoalTm=CurTm;FinalSr=Sr;
   FSTs=STs;FSLs=SLs;FSD=SD;FSInp=SInp;FSFInp=SFInp;FSA=SA;FSF=SF;FSFF=SFF;
  }
  I((TmLi)&&(CurTm>TmLi)&&(!Win)){
   F(D i=0;i<=DisX;++i){
    F(D j=0;j<LenY;++j){
     D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
     F(D d=0;d<5;++d)PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),Rnd.Rand(15)*0.1+0.2,0.01,0.5,0,0,0,(Rnd.Rand(2))?0xEE0000:0x9999FF});
    }
   }
   Win=TLE=1;GoalTm=CurTm;FinalSr=Sr;
   FSTs=STs;FSLs=SLs;FSD=SD;FSInp=SInp;FSFInp=SFInp;FSA=SA;FSF=SF;FSFF=SFF;
  }
  F(D i=0;i<min(LenX,AGC);++i){
   F(D j=0;j<LenY;++j){
   	D lx=(DisX-i+5)*sz-80,ly=j*sz+200;
    F(D d=0;d<((B[i][j])?3:10);++d)PQu.push_back((Particle){lx+Rnd.Rand(sz),ly+Rnd.Rand(sz),Rnd.Rand(15)*0.1+0.1,0.03,0.5,0,0,0,(B[i][j])?0xCCCCCC:0xFFFF00});
   }
  }
  I((NxtRd)&&(!_adl))AreStart=clock();
  Ds((NxtRd)&&(!_adl));
  I((NxtRd)&&(!_adl))LGTm=clock();
  I(ResB>=DisX)Over=1;
  I(Over){sndl(123,60);HoriDel=VerDel=0.0;MsRe=250;PoiUse=0;
   MsA="          ";MsB="GAME OVER ";memset(M,0,sizeof(M));
   MsCB=12;ThisA=::LqSp=SFInp=Clu=Debug=GPos=0;GoalTm=0LL;
   Ds();InitH=InitR='!';
   Sleep(200);W(kbhit())getch();
   getch();
   I(LqSp){
    NTmLi=_ntl;LqSp=0;
    F(D i=0;i<4;++i)Ts[3][i]=tO[i];
   }
   RHg=(LenX<<10);MvsLeft=MvsLi;
   F(D i=0;i<LenX;++i){
    F(D j=0;j<LenY;++j)C[i][j]=0;
   }
   TID=-1;LEff=1;
   D BlC=Ren=0;F(D i=0;i<=TC;++i)B2B[i]=0;
   D _ar=ARe,_rb=ResB,_tl=SLs,_tt=STs,_ta=SA,_inp=SInp,_f=SF;
   LD _sc=Sr,_asr=ASr+Sr;
   ofstream fout;fout.open("oajdncnhdi.txt");
   fout<<_asr;
   fout.clear();fout.close();
   F(D i=0;i<LenX;++i){
    F(D j=0;j<LenY;++j){I(B[i][j])++BlC;
    }}
   D BlRe=BlC;
   SFF=SFInp=0;HAv=1;
   D lp=-1;SD=0;V=StartV;Mp=VMul(V);BonusMp=1.0/max(0.0001,Mp);
   F(D i=LenX-1;i>=0;--i){
    F(D j=0;j<LenY;++j){
     I(!B[i][j])CN;
     --BlRe;
     SLs=D(floor(FL(_tl)/FL(BlC)*FL(BlRe)));
     STs=D(floor(FL(_tt)/FL(BlC)*FL(BlRe)));
     SA=D(floor(FL(_ta)/FL(BlC)*FL(BlRe)));
     ARe=D(floor(FL(_ar)/FL(BlC)*FL(BlRe)));
     ResB=D(floor(FL(_rb)/FL(BlC)*FL(BlRe)));
     SInp=D(floor(FL(_inp)/FL(BlC)*FL(BlRe)));
     SF=D(floor(FL(_f)/FL(BlC)*FL(BlRe)));
     I(!Zen)Sr=(LD)(floor(FL(_sc)/FL(BlC)*FL(BlRe)));
     D lx=(DisX-i+5)*sz-80,rx=lx+sz-1,ly=j*sz+200,ry=ly+sz-1;
     PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,3.0,0.05,-2.0,(Rnd.Rand(11)-5)*0.1,0.2,0,ShiftColor(B[i][j])});
     F(D d=0;d<5;++d){
       FL theta=Rnd.Rand(1000)*0.001*2.0*acos(-1.0);
       PQu.push_back((Particle){FL(lx+rx)*0.5,FL(ly+ry)*0.5,(Rnd.Rand(5)+10)*0.1,0.05,0.5*sin(theta),0.5*cos(theta),0,0,Sc.Mix(0xFFFFFF,ShiftColor(B[i][j]),0.3)});
     }
     B[i][j]=BMark[i][j]=0;
     I(lp!=BlRe*30/BlC)Ds(),lp=BlRe*30/BlC;
    }}
   MvsLeft=MvsLi;QuLen=1;Sr=0LL;ASr=_asr;
   RndReset=1;F(D i=0;i<WTC;++i)TCnt[i]=0;H=-1;
   F(D i=0;i<max(1,NxtT);++i)TQu[i]=RndT();
   TID=NewT();TSt=SpwTSt;W(kbhit())getch();
   MsRe=VD=Mode=MsRe=0;MsA=MsB="          ";
   F(D i=0;i<BsGH;++i)AddG(Rnd.Rand(LenY),0);
   TBR.val.clear();Storage();
   LastA=KeyStTm=BegTm=clock();Win=TLE=SecG=0;Ds();
   LastA=KeyStTm=BegTm=clock();LAct='!';HRen=0;
  }}
 RE 0;
}