Masuko or Haru?题解
先不考虑修改操作。首先区间的变化有点难受,考虑差分一下变成两个点反转。问题转化为了可以同时反转某两个点,问两个串能否一样。
因为异或操作是可逆的,所以将能一次操作完成一样的两个状态两两连边后,是一个无向图,问题等价于判两个是否在一个连通块内。所以可以将问题转化为判两个串是否可以同时转化为一个连通块内有特征的字符串。不妨令这个有特征的字符串为可以到达的最小的字符串。
对于这样的一次变化两个的操作,一个普遍的套路是将变化的两个位置连边建一个图,问题转化为每个点有一个权值,每次可以选择一条边反转连接的两个点的权值。发现对于图上任意一条路径,如果同时对这条路径进上的所有边进行反转操作,只会反转起点和终点。从高位到低位考虑,如果当前位能够通过一些操作使得只修改了低位不修改高位后变成
可以发现,带上修改操作之后,也就是可以合并两个连通块。对于较小的一个连通块最大值可以进行变化。连通块的合并考虑并查集即可。如果只对产生了合并的修改操作对每个串进行修改,则修改数是
#include<bits/stdc++.h>
using namespace std;
namespace fastio
{
const int bufl=1<<20;
const double base1[16]={1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15};
const double base2[16]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15};
struct IN{
FILE *IT;char ibuf[bufl],*is=ibuf,*it=ibuf;
IN(){IT=stdin;}IN(char *a){IT=fopen(a,"r");}
inline char getChar(){if(is==it){it=(is=ibuf)+fread(ibuf,1,bufl,IT);if(is==it)return EOF;}return *is++;}
template<typename Temp>inline void getInt(Temp &a){a=0;int b=0,c=getChar();while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)a=(a<<1)+(a<<3)+c-48,c=getChar();if(b)a=-a;}
template<typename Temp>inline void getDouble(Temp &a){a=0;int b=0,c=getChar(),d=0;__int128 e=0,f=0;while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)e=(e<<1)+(e<<3)+c-48,c=getChar();if(c==46){c=getChar();while(c>=48&&c<=57)d++,f=(f<<1)+(f<<3)+c-48,c=getChar();}a=e+base1[d]*f;if(b)a=-a;}
IN& operator>>(char &a){a=getChar();return *this;}
IN& operator>>(char *a){do{*a=getChar();}while(*a<=32);while(*a>32)*++a=getChar();*a=0;return *this;}
IN& operator>>(string &a){char b=getChar();while(b<=32)b=getChar();while(b>32)a+=b,b=getChar();return *this;}
IN& operator>>(int &a){getInt(a);return *this;}
IN& operator>>(long long &a){getInt(a);return *this;}
IN& operator>>(unsigned long long &a){getInt(a);return *this;}
IN& operator>>(__int128 &a){getInt(a);return *this;}
IN& operator>>(float &a){getDouble(a);return *this;}
IN& operator>>(double &a){getDouble(a);return *this;}
IN& operator>>(long double &a){getDouble(a);return *this;}
};
struct OUT{
FILE *IT;char obuf[bufl],*os=obuf,*ot=obuf+bufl;int Eps;long double Acc;
OUT(){IT=stdout,Eps=6,Acc=1e-6;}OUT(char *a){IT=fopen(a,"w"),Eps=6,Acc=1e-6;}
inline void ChangEps(int x=6){Eps=x;}
inline void flush(){fwrite(obuf,1,os-obuf,IT);os=obuf;}
inline void putChar(int a){*os++=a;if(os==ot)flush();}
template<typename Temp>inline void putInt(Temp a){if(a<0){putChar(45);a=-a;}if(a<10){putChar(a+48);return;}putInt(a/10);putChar(a%10+48);}
template<typename Temp>inline void putLeading(Temp a,int b){if(!b)return;putLeading(a/10,b-1);putChar(a%10+48);}
template<typename Temp>inline void putDouble(Temp a){if(a<0){putChar(45);a=-a;}__int128 b=a;putInt(b);a-=b;a*=base2[Eps];b=a+Acc;putChar(46);putLeading(b,Eps);}
OUT& operator<<(char a){putChar(a);return *this;}
OUT& operator<<(char *a){while(*a>32)putChar(*a++);return *this;}
OUT& operator<<(string a){for(auto c:a)putChar(c);return *this;}
OUT& operator<<(int a){putInt(a);return *this;}
OUT& operator<<(long long a){putInt(a);return *this;}
OUT& operator<<(unsigned long long a){putInt(a);return *this;}
OUT& operator<<(__int128 a){putInt(a);return *this;}
OUT& operator<<(float a){putDouble(a);return *this;}
OUT& operator<<(double a){putDouble(a);return *this;}
OUT& operator<<(long double a){putDouble(a);return *this;}
~OUT(){flush();}
};
}
using fastio::IN;
using fastio::OUT;
IN fin;
OUT fout;
typedef unsigned long long ull;
const int NN=1e7+4,P=131;
char s[NN];
int fa[NN],n,m;
ull hsh[NN],pk[NN];
int get(int x,int y)
{
return (x-1)*m+y;
}
int find(int u)
{
return fa[u]==u?u:fa[u]=find(fa[u]);
}
void rev(int id,int p)
{
int x=get(id,p);
hsh[id]-=pk[p-1]*s[x];
if(s[x]=='1')
s[x]='0';
else
s[x]='1';
hsh[id]+=pk[p-1]*s[x];
}
int main()
{
int q;
fin>>n>>m>>q>>s+1;
for(int i=1;i<=n;i++)
{
for(int j=m;j>1;j--)
{
s[get(i,j)]=s[get(i,j)]==s[get(i,j-1)]?'0':'1';
hsh[i]=hsh[i]*P+s[get(i,j)];
}
hsh[i]=hsh[i]*P+s[get(i,1)];
}
pk[0]=1;
for(int i=1;i<=m+1;i++)
{
fa[i]=i;
pk[i]=pk[i-1]*P;
}
while(q--)
{
int opt;
fin>>opt;
if(opt==1)
{
int l,r;
fin>>l>>r;
r++;
l=find(l),r=find(r);
if(l!=r)
{
if(l>r)
swap(l,r);
fa[l]=r;
for(int i=1;i<=n;i++)
if(s[get(i,l)]=='1')
{
rev(i,l);
if(r<=m)
rev(i,r);
}
}
}
else
{
int x,y;
fin>>x>>y;
fout<<(hsh[x]==hsh[y]?"Masuko":"Haru")<<'\n';
}
}
return 0;
}