P15344 有趣的序列 II 题解
Mr_RedStone · · 题解
出题人题解。
思路
无操作 4
观察到操作
考虑转化题目中的操作,不难发现
感觉上操作
令
因此原式可以化为:
但显然这个式子并不对称,因为
由此令
接下来考虑修改操作,即操作
再考虑操作
由此得到一种
AC
考虑沿用上面维护
暴力一点,翻转区间
考虑优化,不难发现由于操作
代码
AC
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int NR=1e5+5;
int n,q;
string t;
int s[NR],g[NR];
struct Node{
int l,r;
int c0,c1;
bool lazy=0;
friend Node operator +(const Node& a,const Node& b){
Node c;
c.l=min(a.l,b.l);
c.r=max(a.r,b.r);
c.c0=a.c0+b.c0;
c.c1=a.c1+b.c1;
return c;
}
};
struct LCZ{
Node tr[4*NR];
void pushdown(int x){
if(tr[x].lazy){
int l=x<<1,r=(x<<1)+1;
tr[l].lazy^=1;
swap(tr[l].c0,tr[l].c1);
tr[r].lazy^=1;
swap(tr[r].c0,tr[r].c1);
tr[x].lazy=0;
}
}
void build(int x,int l,int r,bool flag){
if(l==r){
if((l&1)==flag) tr[x]={l,r,(g[l]==0),(g[l]==1),0};
else tr[x]={l,r,0,0,0};
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid,flag);
build((x<<1)+1,mid+1,r,flag);
tr[x]=tr[x<<1]+tr[(x<<1)+1];
}
void update(int x,int l,int r){
if(r<tr[x].l||l>tr[x].r) return;
if(l<=tr[x].l&&tr[x].r<=r){
swap(tr[x].c0,tr[x].c1);
tr[x].lazy^=1;
return;
}
pushdown(x);
update(x<<1,l,r);
update((x<<1)+1,l,r);
tr[x]=tr[x<<1]+tr[(x<<1)+1];
}
Node query(int x,int l,int r){
if(r<tr[x].l||l>tr[x].r) return {r,l,0,0,0};
if(l<=tr[x].l&&tr[x].r<=r) return tr[x];
pushdown(x);
return query(x<<1,l,r)+query((x<<1)+1,l,r);
}
int queryp(int x,int p){
if(tr[x].l==tr[x].r) return tr[x].c1?1:0;
pushdown(x);
int mid=(tr[x].l+tr[x].r)>>1;
if(p<=mid) return queryp(x<<1,p);
else return queryp((x<<1)+1,p);
}
}f[2];
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>q;
cin>>t;
t=" "+t;
s[0]=g[0]=0;
for(int i=1;i<=n;i++){
s[i]=s[i-1]^(t[i]-'0');
g[i]=s[i]^(i&1);
}
f[0].build(1,0,n,0);
f[1].build(1,0,n,1);
while(q--){
int op;
cin>>op;
if(op==1){
int l,r;
cin>>l>>r;
int gl=f[(l-1)&1].queryp(1,l-1);
int gr=f[r&1].queryp(1,r);
if(gl==gr) cout<<"Yes\n";
else cout<<"No\n";
}
if(op==2){
int l,r;
cin>>l>>r;
Node t0=f[0].query(1,l-1,r);
Node t1=f[1].query(1,l-1,r);
int c0=t0.c0+t1.c0;
int c1=t0.c1+t1.c1;
cout<<1ll*c0*(c0-1)/2+1ll*c1*(c1-1)/2<<"\n";
}
if(op==3){
int p,x;
cin>>p>>x;
int p1=f[p&1].queryp(1,p)^(p&1);
int p2=f[(p-1)&1].queryp(1,p-1)^((p-1)&1);
if(p1^p2!=x){
f[0].update(1,p,n);
f[1].update(1,p,n);
}
}
if(op==4){
int l,r;
cin>>l>>r;
f[l&1].update(1,l,r);
}
}
return 0;
}
:::
无操作 4
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int NR=1e5+5;
int n,q;
string t;
int s[NR],g[NR];
struct Node{
int l,r;
int c0,c1;//统计区间l,r中gi为0或1的个数
bool lazy;//标记翻转区间
friend Node operator +(const Node& a,const Node& b){
Node c;
c.l=min(a.l,b.l);
c.r=max(a.r,b.r);
c.c0=a.c0+b.c0;
c.c1=a.c1+b.c1;
return c;
}
}tr[4*NR];
void pushdown(int x){
if(tr[x].lazy){
int l=x<<1,r=(x<<1)+1;
tr[l].lazy^=1;
swap(tr[l].c0,tr[l].c1);
tr[r].lazy^=1;
swap(tr[r].c0,tr[r].c1);
tr[x].lazy=0;
}
}
void build(int x,int l,int r){
if(l==r){
tr[x]={l,r,(g[l]==0),(g[l]==1),0};
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build((x<<1)+1,mid+1,r);
tr[x]=tr[x<<1]+tr[(x<<1)+1];
}
void update(int x,int l,int r){
if(r<tr[x].l||l>tr[x].r) return;
if(l<=tr[x].l&&tr[x].r<=r){
swap(tr[x].c0,tr[x].c1);
tr[x].lazy^=1;
return;
}
pushdown(x);
update(x<<1,l,r);
update((x<<1)+1,l,r);
tr[x]=tr[x<<1]+tr[(x<<1)+1];
}
Node query(int x,int l,int r){
if(r<tr[x].l||l>tr[x].r) return {r,l,0,0,0};
if(l<=tr[x].l&&tr[x].r<=r) return tr[x];
pushdown(x);
return query(x<<1,l,r)+query((x<<1)+1,l,r);
}
int queryp(int x,int p){//查询单点g,用于处理操作1
if(tr[x].l==tr[x].r) return tr[x].c1?1:0;
pushdown(x);
int mid=(tr[x].l+tr[x].r)>>1;
if(p<=mid) return queryp(x<<1,p);
else return queryp((x<<1)+1,p);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>q;
cin>>t;
t=" "+t;
s[0]=g[0]=0;
for(int i=1;i<=n;i++){
s[i]=s[i-1]^(t[i]-'0');//预处理s
g[i]=s[i]^(i&1);//预处理g
}
build(1,0,n);//注意由于要使用[l-1,r],因此维护区间[0,n]
while(q--){
int op;
cin>>op;
if(op==1){
int l,r;
cin>>l>>r;
int gl=queryp(1,l-1);
int gr=queryp(1,r);
if(gl==gr) cout<<"Yes\n";
else cout<<"No\n";
}
if(op==2){
int l,r;
cin>>l>>r;
Node lcz=query(1,l-1,r);
cout<<1ll*lcz.c0*(lcz.c0-1)/2+1ll*lcz.c1*(lcz.c1-1)/2<<"\n";//组合数
}
if(op==3){
int p,x;
cin>>p>>x;
if(t[p]-'0'!=x) update(1,p,n);
t[p]=x+'0';
}
}
return 0;
}
:::