There is a 01 string \ (a,|a|=n \). \110} \).
\(n,q\in\left[1,2\times10^5\right]\).
Consider the necessary and sufficient conditions under which the analysis can be made equal by several operations.
It is not difficult to find that it is impossible to change the number of \ (\ textt0 \) and \ (\ textt1 \) in 01 string each time. Therefore, the equal number of \ (\ textt0 \) and \ (\ textt1 \) in \ (2 \) 01 strings is a necessary condition to become equal through several operations, but the sufficiency is far from enough.
It is also found that the operation of \ (texttt{110}leftrightarrowtexttt{011}) is more interesting, which is equivalent to pushing the unique \ (texttt{0}) bit to the left / right (2). Since this is a 01 string, non \ (\ textt0 \) is \ (\ textt1 \), so we will find out all \ (\ textt0 \) positions, and other positions are \ (\ textt1 \).
It is not difficult to find that the relative positions of all \ (\ textt0 \) pairs in the 01 string will not change. Proof: if \ (2 \) \ (\ textt0 \) wants to exchange positions, then the state of the last moment before the exchange must be the distance difference \ (\ leq2 \), at this time \ (\ textt0 \) on the left cannot move \ (2 \) through \ (\ texttt{011}\to\texttt{110} \), because the \ (1\sim2 \) lattice on the right is \ (\ textt0 \), and the substring with the length of \ (3 \) at the beginning must not be \ (\ texttt{011} \). Similarly, the \ (\ textt0 \) on the right cannot move the \ (2 \) cell to the left. Obtain evidence.
Therefore, all \ (\ textt0 \) of \ (2 \) 01 strings are considered to correspond one by one in the order of occurrence. Obviously, a \ (\ textt0 \) in the 01 string of \ (1 \) can go to the same position as a \ (\ textt0 \) in the 01 string of \ (2 \) if and only if the difference between their positions in the string is even, that is, the parity is equal. So we guess that after all \ (\ textt0 \) of \ (2 \) 01 strings correspond one by one according to the order of occurrence, the position parity of each pair of \ (\ textt0 \) in the string is equal, which is the necessary and sufficient condition that the \ (2 \) 01 strings can become equal through several operations. Prove:
- Sufficiency proof: mathematical induction.
- When \ (2 \) strings do not exist \ (\ textt0 \), the sufficiency is obvious;
- It is assumed that when \ (2 \) strings have \ (x-1(x\geq1) \) \ (\ textt0 \), the sufficiency is satisfied. At this time, if you want to align all \ (\ textt0 \) pairs of \ (2 \) 01 strings with \ (x \) \ (\ textt0 \), you can align the right \ (\ textt0 \) of the left \ (1 \) pair \ (\ textt0 \) with the left \ (\ textt0 \) through several operations. The problem is converted into aligning the remaining \ (x-1 \) pair \ (\ textt0 \) one by one. According to the assumption, there is a scheme. So if the sufficiency is satisfied when \ (2 \) strings have \ (x-1 \) \ (\ textt0 \), then the sufficiency is satisfied when \ (2 \) strings have \ (x \) \ (\ textt0 \).
- The need is clear.
To sum up, it is proved.
Next, the question becomes a relatively naked sequence hash: every time a \ (2 \) substring is given, ask whether the sequence composed of the position parity of \ (\ textt0 \) in the \ (2 \) substring is equal. Note: the positional parity here refers to the positional parity relative to \ (l1/l2 \), not to \ (1 \), so it is necessary to record the positional parity sequence of \ (2 \) items corresponding to opposite \ (\ textt0 \). The subsequence of the position parity sequence of \ (\ textt0 \) contained in the substring can be found by binary search.
Here is the AC Code:
#include<bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back const int N=200000; int n/*01 String length*/,qu/*Frequency of enquiry*/; char a[N+5];//01 strings vector<int> pos;//Location sequence for '0' const int hbase1=131,hmod1=998244353,hbase2=13331,hmod2=1000000007;//Hash parameter int Hsh1[N+1],Rhsh1[N+1],hpw1[N+1],Hsh2[N+1],Rhsh2[N+1],hpw2[N+1];//Hsh: prefix hash of position parity sequence relative to'0'of 1, Rhsh: relative to... Of 2 void hsh_init(){//Hash preprocessing hpw1[0]=hpw2[0]=1; for(int i=1;i<=pos.size();i++)//For the convenience of prefix operation, hash arrays are 1-indexed Hsh1[i]=(1ll*Hsh1[i-1]*hbase1+pos[i-1]%2+1)%hmod1, Rhsh1[i]=(1ll*Rhsh1[i-1]*hbase1+!(pos[i-1]%2)+1)%hmod1, hpw1[i]=1ll*hpw1[i-1]*hbase1%hmod1, Hsh2[i]=(1ll*Hsh2[i-1]*hbase2+pos[i-1]%2+1)%hmod2, Rhsh2[i]=(1ll*Rhsh2[i-1]*hbase2+!(pos[i-1]%2)+1)%hmod2, hpw2[i]=1ll*hpw2[i-1]*hbase2%hmod2; } pair<int,int> hsh(int l,int r){//Hash value of the parity sequence of pos[l-1~r-1] relative to 1 return mp(((Hsh1[r]-1ll*Hsh1[l-1]*hpw1[r-l+1]%hmod1)+hmod1)%hmod1, ((Hsh2[r]-1ll*Hsh2[l-1]*hpw2[r-l+1]%hmod2)+hmod2)%hmod2); } pair<int,int> rhsh(int l,int r){//Hash value of pos[l-1~r-1] relative to the parity sequence of 2 return mp(((Rhsh1[r]-1ll*Rhsh1[l-1]*hpw1[r-l+1]%hmod1)+hmod1)%hmod1, ((Rhsh2[r]-1ll*Rhsh2[l-1]*hpw2[r-l+1]%hmod2)+hmod2)%hmod2); } int main(){ cin>>n>>a+1>>qu; for(int i=1;i<=n;i++)if(a[i]=='0')pos.pb(i);//Preprocessing pos hsh_init();//Preprocessing hash while(qu--){ int l1,l2,len; scanf("%d%d%d",&l1,&l2,&len); int l1_0=lower_bound(pos.begin(),pos.end(),l1)-pos.begin()+1,//Left endpoint of position parity sequence of '0' contained in a[l1~l1+len-1] r1_0=lower_bound(pos.begin(),pos.end(),l1+len)-pos.begin(),//Right endpoint of position parity sequence of '0' contained in a[l1~l1+len-1] l2_0=lower_bound(pos.begin(),pos.end(),l2)-pos.begin()+1,//Left endpoint of position parity sequence of '0' contained in a[l2~l2+len-1] r2_0=lower_bound(pos.begin(),pos.end(),l2+len)-pos.begin();//Right endpoint of position parity sequence of '0' contained in a[l2~l2+len-1] pair<int,int> hsh1=l1%2?hsh(l1_0,r1_0):rhsh(l1_0,r1_0),hsh2=l2%2?hsh(l2_0,r2_0):rhsh(l2_0,r2_0);//DP Hash puts(hsh1==hsh2?"Yes":"No");//Equal judgement } return 0; }