Description
Mirko and Slavko started taking tap dance lessons. This dance consists mostly of tapping the floor with
a special kind of shoe. Since Mirko and Slavko are fast learners, they decided to come up with their
own choreography.
Tap dance choreography can be described as a sequence consisting of two letters, ‘L’ and ‘R’. ‘L’ means
that you should tap the floor with your left foot, and ‘R’ with your right foot. Mirko realised that the
most exciting parts of tap dancing are the ones in which you don’t use the same leg twice in a row. He
defined the value of a choreography as the longest subsequence of consecutive elements that doesn’t
contain two consecutive ‘L’s or ‘R’s.
As we all know, designing a choreography can be very challenging, with lots of small changes until it’s
done. For every alteration that Slavko does, he would like to know the current choreography value.
One alteration is changing one ‘L’ to ‘R’, and vice versa.
Before any alterations are made, the choreography consists only of letters ‘L’.
大致意思就是初始给一个长度为N的全是L的串,每次将一个位置修改(L变成R,R变成L),每次修改后求出L和R交替出现的最长的子串长度。
题解
额,跟模板题一样。如果两个位置一样就是0,不一样就是1,答案就是最长的连续的1的长度+1,修改的时候把前后两个位置都改掉就好了。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200006
using namespace std;
inline char nc(){
static char buf[100000],*i=buf,*j=buf;
return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;
}
inline int _read(){
char ch=nc();int sum=0;
while(!(ch>='0'&&ch<='9'))ch=nc();
while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
return sum;
}
struct data{
int l,r,p,ml,mr,m;
}tree[maxn*4];
int n,tet;
void build(int p,int l,int r){
tree[p].l=l;tree[p].r=r;tree[p].p=0;
if(l==r)return;
int mid=(l+r)>>1;
build(p<<1,l,mid);build(p<<1|1,mid+1,r);
}
void update(int p,int k){
if(tree[p].l>k||tree[p].r<k)return;
if(tree[p].l==tree[p].r){
tree[p].p=1-tree[p].p;
if(tree[p].p)tree[p].m=tree[p].ml=tree[p].mr=1;
else tree[p].m=tree[p].ml=tree[p].mr=0;
return;
}
update(p<<1,k);update(p<<1|1,k);
if(tree[p<<1].ml==tree[p<<1].r-tree[p<<1].l+1)tree[p].ml=tree[p<<1].ml+tree[p<<1|1].ml;
else tree[p].ml=tree[p<<1].ml;
if(tree[p<<1|1].mr==tree[p<<1|1].r-tree[p<<1|1].l+1)tree[p].mr=tree[p<<1|1].mr+tree[p<<1].mr;
else tree[p].mr=tree[p<<1|1].mr;
tree[p].m=max(max(tree[p].ml,tree[p].mr),tree[p<<1].mr+tree[p<<1|1].ml);
tree[p].m=max(tree[p].m,max(tree[p<<1].m,tree[p<<1|1].m));
}
int main(){
freopen("step.in","r",stdin);
freopen("step.out","w",stdout);
n=_read();tet=_read();
build(1,1,n-1);
while(tet--){
int x=_read();
if(x!=1)update(1,x-1);
if(x!=n)update(1,x);
printf("%d\n",tree[1].m+1);
}
return 0;
}