In this problem, a good string will remain to be a good string no matter how many times it has been flipped for the relationships between adjacent characters remain the same (if two characters are different, they are still different after flipping, vice versa). It is evident that the relativity between adjacent characters (whether they are equal of different) uniquely determines whether a string is good.
We denote a i a_i ai as the relativity between characters s i s_i si and s i + 1 s_{i+1} si+1, with a i = [ s i = = s i + 1 ] a_i = [s_i==s_{i+1}] ai=[si==si+1]. Considering the problem’s sequence modification operation in terms of relativity, we can see that a i a_i ai does not change for elements inside the modification interval for the above-stated reason. The only change of relativity comes at the interval endpoints, since one of their neighboring characters has not been modified. Therefore, the sequence modification is reduced to single point inverse operations.
For the interval query, we can simply calculate if there exist an i i i where a i = 1 a_i=1 ai=1 to see if the string is good. Since all operations are dynamic, we can use the segment tree data structure to maintain them.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
const int Maxn=5e5+10;
const int Maxm=Maxn<<2;
int sum[Maxm];
char a[Maxn];
int n,m;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
inline void push_up(int k)
{
sum[k]=sum[k<<1]+sum[k<<1|1];
}
void modify(int k,int l,int r,int pos,int v)
{
if(l==r){sum[k]+=v;return;}
int mid=(l+r)>>1;
if(pos<=mid)modify(k<<1,l,mid,pos,v);
else modify(k<<1|1,mid+1,r,pos,v);
push_up(k);
}
int query(int k,int l,int r,int x,int y)
{
if(x>y)return 0;
if(x<=l && r<=y)return sum[k];
int mid=(l+r)>>1,ret=0;
if(x<=mid)ret=query(k<<1,l,mid,x,y);
if(mid<y)ret+=query(k<<1|1,mid+1,r,x,y);
return ret;
}
int main()
{
// freopen("in.txt","r",stdin);
n=read(),m=read();
scanf("%s",a+1);
for(int i=1;i<n;++i)
if(a[i]==a[i+1])modify(1,1,n,i,1);
while(m--)
{
int opt=read(),l=read(),r=read();
if(opt==2)
{
if(l==r || !query(1,1,n,l,r-1))puts("Yes");
else puts("No");
continue;
}
if(l>1)
{
if(query(1,1,n,l-1,l-1))modify(1,1,n,l-1,-1);
else modify(1,1,n,l-1,1);
}
if(r<n)
{
if(query(1,1,n,r,r))modify(1,1,n,r,-1);
else modify(1,1,n,r,1);
}
}
return 0;
}

文章讨论了一个关于字符串的问题,通过分析相邻字符的关系(相同或不同),利用段树数据结构高效地处理字符串的好字符串性质,包括单点逆序操作和区间查询。

被折叠的 条评论
为什么被折叠?



