蒟蒻第三次求调!
查看原帖
蒟蒻第三次求调!
641839
Fracture_Hikari楼主2023/1/13 15:37
#include<bits/stdc++.h>
using namespace std;
namespace Tree{
	const int maxn=100005;
	int n,m;
	int ans=0;
	struct tree{
		int l,r;
		int lson,rson;
		int tag;
		int num;
	}a[maxn*4];
	void pushup(int x){
		if(a[a[x].lson].num==1&&a[a[x].rson].num==1)
			a[x].num=1;
		else if(a[a[x].lson].num==0&&a[a[x].rson].num==0)
			a[x].num=0;
		else a[x].num=-1;
	}
	int cnt=0;
	int Root;
	void Build(int &x,int l,int r){
		if(x==0){
			x=++cnt;
			a[x].l=l;
			a[x].r=r;
			a[x].tag=-1;
			a[x].num=0;
		}
		if(l==r){
			a[x].num=0;
			return ;
		}
		int mid=(l+r)/2;
		Build(a[x].lson,l,mid);
		Build(a[x].rson,mid+1,r);
		pushup(x);
	}
	void pushdown(int x){
		if(a[x].tag==-1)
			return ;
		a[a[x].lson].num=a[x].tag;
		a[a[x].rson].num=a[x].tag;
		a[a[x].lson].tag=a[x].tag;
		a[a[x].rson].tag=a[x].tag;
		a[x].tag=-1;
	}
	void change(int x,int l,int r){
		if(a[x].r<l||a[x].l>r)
			return ;
		if(a[x].l==a[x].r){
			int op;
			if(a[x].num==1)
				op=0;
			else if(a[x].num==0)
				op=1;
			else
				op=-1;
			a[x].num=op;
			a[x].tag=op;
			return ;
		}
		pushdown(x);
		change(a[x].lson,l,r);
		change(a[x].rson,l,r);
		pushup(x);
	}
	void ask(int x,int f){
		if(f<a[x].l||f>a[x].r)
			return ;
		//cout<<f<<" "<<a[x].l<<" "<<a[x].r<<" "<<a[x].num<<endl;
		if(a[x].l==a[x].r){
			ans=a[x].num;
			return ;
		}
		pushdown(x);
		ask(a[x].lson,f);
		ask(a[x].rson,f);
		pushup(x);
	}
	int main(){
		scanf("%d%d",&n,&m);
		Build(Root,1,n);
		for(int i=1;i<=m;i++){
			int op;
			int x,y;
			scanf("%d",&op);
			if(op==1){
				scanf("%d%d",&x,&y);
				change(Root,x,y); 
			}
			if(op==2){
				scanf("%d",&x);
				ans=0;
				ask(Root,x);
				printf("%d\n",ans);
			}
		}
		return 0;
	}
}
int main(){return Tree::main();}
2023/1/13 15:37
加载中...