[csharp]
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FFF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 100011;
struct zz
{
int l;
int r;
int x;
}zx[maxn<<2];
int n,m;
inline void pup(int root)
{
zx[root].x = zx[LL(root)].x + zx[RR(root)].x;
return ;
}
void build(int l=0,int r=n,int root=1)
{
zx[root].l=l;
zx[root].r=r;
if(l==r)
{
zx[root].x=0;
return;
}
else
{
int mid = (l+r)/2;
build(l,mid,LL(root));
build(mid+1,r,RR(root));
pup(root);
}
return ;
}
void reset(int x,int y,int root=1)
{
if(zx[root].l == x && zx[root].r == x)
{
zx[root].x+=y;
return ;
}
else
{
if(root*2 < 4*maxn && x<=zx[root*2].r)
{
reset(x,y,root*2);
}
else if(root*2+1 < 4*maxn && x>=zx[root*2+1].l)
{
reset(x,y,root*2+1);
}
pup(root);
}
return ;
}
int query(int l,int r,int root=1)
{
if(l<=zx[root].l && r>=zx[root].r)
{
return zx[root].x;
}
else
{
int re=0;
if(l<=zx[LL(root)].r)
{
re+=query(l,r,LL(root));
}
if(r>=zx[RR(root)].l)
{
re+=query(l,r,RR(root));
}
return re;
}
}
bool you(int x,int root=1)
{
if(zx[root].l==x && zx[root].r==x)
{
if(zx[root].x>0)
{
return true;
}
else
{
return false;
}
}
else
{
if(x<=zx[LL(root)].r)
{
return you(x,LL(root));
}
else if(x>=zx[RR(root)].l)
{
return you(x,RR(root));
}
}
}
int find(int x,int root=1)
{
if(zx[root].l == zx[root].r)
{
return zx[root].l;
}
else
{
if(zx[LL(root)].x>=x)
{
return find(x,LL(root));
}
else
{
return find(x-zx[LL(root)].x,RR(root));
}
}
}
int main()
{
int T;
cin>>T;
for(int tt=1;tt<=T;tt++)
{
cin>>n>>m;
build();
i64 ans=0;
int c;
int temp;
int l,r;
bool f=true;
int now = 0;
for(int i=1;i<=m;i++)
{
SS(c);
if(c==1)
{
if(zx[1].x==0)
{
continue;
}
else
{
if(you(now))
{
reset(now,-1);
continue;
}
else
{
temp = query(0,now);
l = find(temp);
r = find(temp+1);
if(!temp)
{
f=true;
ans+=r-now;
now=r;
reset(r,-1);
continue;
}
else if(temp==zx[1].x)
{
f=false;
ans+=now-l;
now=l;
reset(l,-1);
continue;
}
else if(now-l < r-now)
{
f=false;
ans+=now-l;
now=l;
reset(l,-1);
continue;
}
else if(now-l > r-now)
{
f=true;
ans+=r-now;
now=r;
reset(r,-1);
continue;
}
else if(now-l==r-now)
{
ans+=r-now;
if(f)
{
now=r;
reset(r,-1);
continue;
}
else
{
now=l;
reset(l,-1);
continue;
}
}
}
}
}
else if(!c)
{
SS(temp);
reset(temp,1);
}
}
printf("Case %d: %d\n",tt,ans);
}
return 0;
}
作者:zz_1215