题目:E. Nezzar and Binary String
分析:
这个题按时间从后往前思考,会发现如果不让他生气,每次修改的操作都是唯一的,用一个线段树维护即可
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
struct node
{
int l, r;
int sum;
int flag;
}t[maxn << 2];
int init[maxn];
int a[maxn];
pii b[maxn];
void pushup(int tar)
{
t[tar].sum = t[tar << 1].sum + t[tar << 1 | 1].sum;
}
void pushdown(int tar)
{
if(t[tar].flag != -1)
{
t[tar << 1].flag = t[tar << 1 | 1].flag = t[tar].flag;
t[tar << 1].sum = (t[tar << 1].r - t[tar << 1].l + 1) * t[tar << 1].flag;
t[tar << 1 | 1].sum = (t[tar << 1 | 1].r - t[tar << 1 | 1].l + 1) * t[tar << 1 | 1].flag;
t[tar].flag = -1;
}
}
void build(int tar, int l, int r)
{
t[tar] = {l, r, 0, -1};
if(l == r)
{
t[tar].sum = a[l];
return;
}
int mid = l + r >> 1;
build(tar << 1, l, mid), build(tar << 1 | 1, mid + 1, r);
pushup(tar);
}
void update(int tar, int l, int r, int val)
{
if(l <= t[tar].l && t[tar].r <= r)
{
t[tar].flag = val;
t[tar].sum = (t[tar].r - t[tar].l + 1) * t[tar].flag;
return;
}
pushdown(tar);
int mid = t[tar].r + t[tar].l >> 1;
if(l <= mid) update(tar << 1, l, r, val);
if(r > mid) update(tar << 1 | 1, l, r, val);
pushup(tar);
}
int query(int tar, int l, int r)
{
if(l <= t[tar].l && t[tar].r <= r)
return t[tar].sum;
pushdown(tar);
int mid = t[tar].r + t[tar].l >> 1;
int res = 0;
if(l <= mid) res += query(tar << 1, l, r);
if(r > mid) res += query(tar << 1 | 1, l, r);
return res;
}
int main()
{
int tt; cin >> tt;
while(tt--)
{
int n, q; cin >> n >> q;
for(int i = 1; i <= n; i++)
scanf("%1d", init + i);
for(int i = 1; i <= n; i++)
scanf("%1d", a + i);
for(int i = 1; i <= q; i++)
cin >> b[i].x >> b[i].y;
build(1, 1, n);
int flag = 0;
for(int i = q; i >= 1; i--)
{
int l = b[i].x, r = b[i].y;
int num = r - l >> 1;
int one = query(1, l, r);
int zero = r - l + 1 - one;
if(zero <= num)
update(1, l, r, 1);
else if(one <= num)
update(1, l, r, 0);
else
{
flag = 1;
break;
}
}
for(int i = 1; i <= n; i++)
if(init[i] != query(1, i, i))
{
flag = 1;
break;
}
if(flag) cout << "NO" << endl;
else cout << "YES" << endl;
}
}