线段树求条
查看原帖
线段树求条
530468
_determination_楼主2023/10/31 15:46

RT WA0

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100010];
struct abc{
	int l,r;
	int l0,r0,l1,r1,m0,m1,s0,s1;
	int len;
	int op;
}t[400010];
abc pu(abc ls,abc rs)
{
	abc x;
	if(ls.m0==ls.len)
	{
		x.l0=ls.len+rs.l0;
	}else{
		x.l0=ls.l0;
	}
	if(rs.m0==rs.len)
	{
		x.r0=rs.len+ls.r0;
	}else{
		x.r0=rs.r0;
	}
	x.m0=max(ls.l0,max(rs.r0,ls.r0+rs.l0));
	x.s0=ls.s0+rs.s0;
	
	if(ls.m1==ls.len)
	{
		x.l1=ls.len+rs.l1;
	}else{
		x.l1=ls.l1;
	}
	if(rs.m1==rs.len)
	{
		x.r1=rs.len+ls.r1;
	}else{
		x.r1=rs.r1;
	}
	x.m1=max(ls.l1,max(rs.r1,ls.r1+rs.l1));
	x.s1=ls.s1+rs.s1;
	x.len=ls.len+rs.len;
	return x;
}
void build(int x,int l,int r)
{
	t[x].l=l;
	t[x].r=r;
	t[x].op=-1;
	t[x].len=r-l+1;
	if(l==r)
	{
		if(a[l]==0)
		{
			t[x].l0=t[x].r0=t[x].m0=t[x].s0=1;
			t[x].l1=t[x].r1=t[x].m1=t[x].s1=0;
		}else{
			t[x].l1=t[x].r1=t[x].m1=t[x].s1=1;
			t[x].l0=t[x].r0=t[x].m0=t[x].s0=0;
		}
		return;
	}
	int mid=(l+r)/2;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	t[x]=pu(t[x<<1],t[x<<1|1]);
	t[x].l=l;
	t[x].r=r;
	t[x].op=-1;
}
void pd(int x)
{
	if(t[x].op==-1)
	{
		return;
	}
	if(t[x].l==t[x].r)
	{
		return;
	}
//	cout << "pd " << t[x].l << " " << t[x].r << endl;
	pd(x<<1);
	pd(x<<1|1);
//	cout << "turn" <<(x<<1) << " " <<(x<<1|1) << endl; 
	if(t[x].op==0)
	{
		t[x<<1].l0=t[x<<1].r0=t[x<<1].m0=t[x<<1].s0=t[x<<1].len;
		t[x<<1].l1=t[x<<1].r1=t[x<<1].m1=t[x<<1].s1=0;
		t[x<<1].op=0;
		t[x<<1|1].l0=t[x<<1|1].r0=t[x<<1|1].m0=t[x<<1|1].s0=t[x<<1|1].len;
		t[x<<1|1].l1=t[x<<1|1].r1=t[x<<1|1].m1=t[x<<1|1].s1=0;
		t[x<<1|1].op=0;
		t[x].op=-1;
		return;
	}
	if(t[x].op==1)
	{
		t[x<<1].l0=t[x<<1].r0=t[x<<1].m0=t[x<<1].s0=0;
		t[x<<1].l1=t[x<<1].r1=t[x<<1].m1=t[x<<1].s1=t[x<<1].len;
		t[x<<1].op=1;
		t[x<<1|1].l0=t[x<<1|1].r0=t[x<<1|1].m0=t[x<<1|1].s0=0;
		t[x<<1|1].l1=t[x<<1|1].r1=t[x<<1|1].m1=t[x<<1|1].s1=t[x<<1|1].len;
		t[x<<1|1].op=1;
		t[x].op=-1;
		return;
	}
	if(t[x].op==2)
	{
		swap(t[x<<1].l0,t[x<<1].l1);
		swap(t[x<<1].r0,t[x<<1].r1);
		swap(t[x<<1].m0,t[x<<1].m1);
		swap(t[x<<1].s0,t[x<<1].s1);
		t[x<<1].op=2;
		swap(t[x<<1|1].l0,t[x<<1|1].l1);
		swap(t[x<<1|1].r0,t[x<<1|1].r1);
		swap(t[x<<1|1].m0,t[x<<1|1].m1);
		swap(t[x<<1|1].s0,t[x<<1|1].s1);
		t[x<<1|1].op=2;
		t[x].op=-1;
		return;
	}
}
void op0(int x,int l,int r)
{
	int lf=t[x].l,rt=t[x].r;
	if(l<=lf&&rt<=r)
	{
		t[x].l0=t[x].r0=t[x].m0=t[x].s0=rt-lf+1;
		t[x].l1=t[x].r1=t[x].m1=t[x].s1=0;
		t[x].op=0;
		return ;
	}
	if(rt<l||r<lf)
	{
		return;
	}
	pd(x);
	op0(x<<1,l,r);
	op0(x<<1|1,l,r);
	t[x]=pu(t[x<<1],t[x<<1|1]);
	t[x].l=lf;
	t[x].r=rt;
	t[x].op=-1;
}
void op1(int x,int l,int r)
{
	int lf=t[x].l,rt=t[x].r;
//	cout << "op1" << " " << lf << " " << rt << " " << l << " " << r<< endl;
	if(l<=lf&&rt<=r)
	{
//		cout << "T" << endl;
		t[x].l0=t[x].r0=t[x].m0=t[x].s0=0;
		t[x].l1=t[x].r1=t[x].m1=t[x].s1=rt-lf+1;
		t[x].op=1;
		return ;
	}
	if(rt<l||r<lf)
	{
//		cout << "R" << endl;
		return;
	}
	pd(x);
//	cout << "in\n";
	op1(x<<1,l,r);
//	cout << "out\n in\n";
	op1(x<<1|1,l,r);
//	cout << "out\n"; 
	t[x]=pu(t[x<<1],t[x<<1|1]);
	t[x].l=lf;
	t[x].r=rt;
	t[x].op=-1;
}
void op2(int x,int l,int r)
{
	int lf=t[x].l,rt=t[x].r;
	if(l<=lf&&rt<=r)
	{
		swap(t[x].l0,t[x].l1);
		swap(t[x].r0,t[x].r1);
		swap(t[x].m0,t[x].m1);
		swap(t[x].s0,t[x].s1);
		t[x].op=2;
		return ;
	}
	if(rt<l||r<lf)
	{
		return;
	}
	pd(x);
	op2(x<<1,l,r);
	op2(x<<1|1,l,r);
	t[x]=pu(t[x<<1],t[x<<1|1]);
	t[x].l=lf;
	t[x].r=rt;
	t[x].op=-1;
}
int op3(int x,int l,int r)
{
	int lf=t[x].l,rt=t[x].r;
	if(l<=lf&&rt<=r)
	{
//		cout << lf << " " << rt << " " << t[x].s1 << endl;
		return t[x].s1;
	}
	if(rt<l||r<lf)
	{
//		cout << lf << " " << rt << " 0.0\n";
		return 0;
	}
	pd(x);
	return op3(x<<1,l,r)+op3(x<<1|1,l,r);
}
abc op4(int x,int l,int r)
{
	int lf=t[x].l;
	int rt=t[x].r;
	if(l<=lf&&rt<=r)
	{
		return t[x];
	}
	pd(x);
	if(t[x<<1].r<l)
	{
		return op4(x<<1|1,l,r);
	}
	if(t[x<<1|1].l>r)
	{
		return op4(x<<1,l,r);
	}
	return pu(op4(x<<1,l,r),op4(x<<1|1,l,r));
}
signed main()
{
//	ios::sync_with_stdio(false);
	int n,q;
	cin >> n >> q;
	for ( int i =1 ; i <= n;i++ )
	{
		cin >>a[i];
	}
	build(1,1,n);
	
	while(q--)
	{
		int op,l,r;
		cin >> op >> l >> r;
		l++;
		r++;
		if(op==0)
		{
			op0(1,l,r);
		}else if(op==1)
		{
			op1(1,l,r);
		}else if(op==2)
		{
			op2(1,l,r);
		}else if(op==3)
		{
			cout << op3(1,l,r) << endl;
		}else{
			cout << op4(1,l,r).m1 << endl;
		}
	}
	return 0;
}
/*
10 10
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
*/
2023/10/31 15:46
加载中...