E. Nezzar and Binary String

E. Nezzar and Binary String
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Nezzar has a binary string s of length n that he wants to share with his best friend, Nanako. Nanako will spend q days inspecting the binary string. At the same time, Nezzar wants to change the string s into string f during these q days, because it looks better.

It is known that Nanako loves consistency so much. On the i-th day, Nanako will inspect a segment of string s from position li to position ri inclusive. If the segment contains both characters ‘0’ and ‘1’, Nanako becomes unhappy and throws away the string.

After this inspection, at the i-th night, Nezzar can secretly change strictly less than half of the characters in the segment from li to ri inclusive, otherwise the change will be too obvious.

Now Nezzar wonders, if it is possible to avoid Nanako being unhappy and at the same time have the string become equal to the string f at the end of these q days and nights.

Input
The first line contains a single integer t (1≤t≤2⋅105) — the number of test cases.

The first line of each test case contains two integers n,q (1≤n≤2⋅105, 0≤q≤2⋅105).

The second line of each test case contains a binary string s of length n.

The third line of each test case contains a binary string f of length n.

Then q lines follow, i-th of them contains two integers li,ri (1≤li≤ri≤n) — bounds of the segment, that Nanako will inspect on the i-th day.

It is guaranteed that the sum of n for all test cases doesn’t exceed 2⋅105, and the sum of q for all test cases doesn’t exceed 2⋅105.

Output
For each test case, print “YES” on the single line if it is possible to avoid Nanako being unhappy and have the string f at the end of q days and nights. Otherwise, print “NO”.

You can print each letter in any case (upper or lower).

Example
inputCopy
4
5 2
00000
00111
1 5
1 3
2 1
00
01
1 2
10 6
1111111111
0110001110
1 10
5 9
7 10
1 7
3 5
6 10
5 2
10000
11000
2 5
1 3
outputCopy
YES
NO
YES
NO
Note
In the first test case, 00000–→00011→00111 is one of the possible sequences of string changes.

In the second test case, it can be shown that it is impossible to have the string f at the end.

逆向思维,从后往前推。看能否从 f 推向 s
用线段树维护1的个数
add表示将要转变的数

#include<bits/stdc++.h>
using namespace std;
#define N 200005
int n,q,T;
char s[N],f[N];
pair<int,int> a[N];
struct tree
{
	int l,r,sum,add;
}t[4*N];

void build(int p,int l,int r)//建树维护1的个数
{
	t[p]={l,r,0,-1};
	if (l==r) 
	{
		t[p].sum=f[l]-'0';
		return;
	}
	int mid=(l+r)>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
	 
}

void spread(int p)//下放要成为的数
{
	if (t[p].add!=-1)
	{
		t[p<<1].sum=t[p].add*(t[p<<1].r-t[p<<1].l+1);
		t[p<<1|1].sum=t[p].add*(t[p<<1|1].r-t[p<<1|1].l+1);
		t[p<<1].add=t[p].add;
		t[p<<1|1].add=t[p].add;
		t[p].add=-1;
	}
}

void change(int p,int l,int r,int c)//区间[l,r]变成c
{
	if (l<=t[p].l&&t[p].r<=r) 
	{
		t[p].add=c;
		t[p].sum=c*(t[p].r-t[p].l+1);
		return;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if (l<=mid) change(p<<1,l,r,c);
	if (r>mid) change(p<<1|1,l,r,c);
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}

int ask(int p,int l,int r)//询问1的个数
{
	if(l<=t[p].l&&t[p].r<=r) return t[p].sum;
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	int val=0;
	if (l<=mid) val+=ask(p<<1,l,r);
	if (r>mid) val+=ask(p<<1|1,l,r);
	return val;
}



bool judge(int p)//判断是否相同
{
	if (t[p].l==t[p].r) return t[p].sum==s[t[p].l]-'0';
	spread(p);
	if (!judge(p<<1)) return 0;
	if (!judge(p<<1|1)) return 0;
	return 1;
}

bool check()
{
	for (int i=q;i>=1;i--)
	{
		int l=a[i].first,r=a[i].second;
		int cnt1=ask(1,l,r);
		int cnt0=r-l+1-cnt1;
		if (cnt1==cnt0) return 0;
		if (!cnt1||!cnt0) continue;
		change(1,l,r,cnt1>cnt0?1:0);
	}
	return judge(1);
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&q);
		scanf("%s%s",s+1,f+1);
		for (int i=1;i<=q;i++) scanf("%d%d",&a[i].first,&a[i].second);
		build(1,1,n);
		puts(check()?"YES":"NO");
	}

} 

版权声明:本文为qq_52062635原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。