线段树10pts求助
查看原帖
线段树10pts求助
115359
phigy楼主2023/6/30 10:06
#include <iostream>

using namespace std;

int n,m;
struct tree
{
	int lazy,lson,rson,ll,rr;
}t[5000005];
int cnt=1; 

void check(int x)
{
	int mid=(t[x].ll+t[x].rr)/2;
	if(t[x].lson==0)
	{
		
		t[x].lson=++cnt;
		t[cnt].ll=t[x].ll;
		t[cnt].rr=mid;
	}
	if(t[x].rson==0)
	{
		t[x].rson=++cnt;
		t[cnt].ll=mid+1;
		t[cnt].rr=t[x].rr;
	}
}

void add(int x,int l,int r)
{
	if(l<=t[x].ll&&t[x].rr<=r)
	{
		t[x].lazy^=1;
	}
	else
	{
		if(t[x].ll<=r&&r<=t[x].rr)
		{
			check(x);
			add(t[x].lson,l,r);
		}
		if(t[x].ll<=l&&l<=t[x].rr)
		{
			check(x);
			add(t[x].rson,l,r);
		}
	}
}

void pushdown(int x)
{
	t[t[x].lson].lazy^=t[x].lazy;
	t[t[x].rson].lazy^=t[x].lazy;
	t[x].lazy=0;
}

void query(int x,int q)
{
	if(t[x].ll==q&&q==t[x].rr)
	{
		cout<<t[x].lazy<<endl;
	}
	else
	{
		check(x);
		pushdown(x);
		int mid=(t[x].ll+t[x].rr)/2;
		if(t[x].ll<=q&&q<=mid)
		{
			query(t[x].lson,q); 
		}
		if(mid+1<=q&&q<=t[x].rr)
		{
			query(t[x].rson,q);
		}
	}
}


int main()
{
	int i,j,k;
	cin>>n>>m;
	t[1].ll=1;
	t[1].rr=n;
	while(m--)
	{
		int op;
		cin>>op;
		if(op==1)
		{
			int l,r;
			cin>>l>>r;
			add(1,l,r);
		}
		if(op==2)
		{
			int q;
			cin>>q;
			query(1,q);
		}
	}
	return 0;
} 
2023/6/30 10:06
加载中...