求助线段树水题
查看原帖
求助线段树水题
743127
Wu1hong2shen4楼主2023/8/4 23:34

rt,到操作二退房步骤时就挂了,但太蒻了调不出来,只能过 hack

#include <bits/stdc++.h>
using namespace std;

const int T = 1e5+10;
int n,m;

struct dian {int l,r,ans,len,lmax,rmax,lazy;};
struct ttt {
	dian s[T<<2];
	void pushup(int p) {
		if(s[p<<1].ans == s[p<<1].len)
			s[p].lmax = s[p<<1].len + s[p<<1|1].lmax;
		else s[p].lmax = s[p<<1].lmax;
		if(s[p<<1|1].ans == s[p<<1|1].len)
			s[p].rmax = s[p<<1|1].len + s[p<<1].rmax; 
		else s[p].rmax = s[p<<1|1].rmax;
		s[p].ans = max(max(s[p<<1].ans,s[p<<1|1].ans),s[p<<1].rmax+s[p<<1|1].lmax);
	}
	void pushdown(int p) {
		if(s[p].lazy == 0)
			return ;
		if(s[p].lazy == 1) {//1 kai
			s[p<<1].lazy = s[p<<1|1].lazy = 1;
			s[p<<1].len = s[p<<1].lmax = s[p<<1].rmax = 0;
			s[p<<1|1].len = s[p<<1|1].lmax = s[p<<1|1].rmax = 0;
			s[p].lazy = 0;
		}
		if(s[p].lazy == 2) {//2 tui
			s[p<<1].lazy = s[p<<1|1].lazy = 2;
			s[p<<1].len = s[p<<1].lmax = s[p<<1].rmax = s[p<<1].len;
			s[p<<1|1].len = s[p<<1|1].lmax = s[p<<1|1].rmax = s[p<<1|1].len;
			s[p].lazy = 0;
		}
	}
	void build(int p,int l,int r) {
		s[p].l = l;
		s[p].r = r;
		s[p].len = s[p].ans = s[p].lmax = s[p].rmax = r-l+1;
		if(l == r)
			return ;
		int mid = l+r>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r);
	}
	void add(int p,int L,int R,int tai) {
		pushdown(p);
		if(L <= s[p].l && s[p].r <= R) {
			if(tai == 1) s[p].ans = s[p].lmax = s[p].rmax = 0;
			else s[p].ans = s[p].lmax = s[p].rmax = s[p].len;
			s[p].lazy = tai;
			return ;
		}
		int mid = s[p].l+s[p].r>>1;
		if(L <= mid) add(p<<1,L,R,tai);
		if(mid < R) add(p<<1|1,L,R,tai);
		pushup(p);
	}
	int query(int p,int z_len) {
		pushdown(p);
		if(s[p].l == s[p].r) return s[p].l;
		int mid = s[p].l+s[p].r>>1;
		if(s[p<<1].ans >= z_len) return query(p<<1,z_len);
		if(s[p<<1].rmax + s[p<<1|1].lmax >= z_len) return mid-s[p<<1].rmax+1;
		else return query(p<<1|1,z_len);
	}
}tree;

signed main() {
	scanf("%d%d",&n,&m);
	tree.build(1,1,n);
	int tai,x,y;
	while(m--) {
		scanf("%d%d",&tai,&x);
		if(tai == 1) {
			if(tree.s[1].ans >= x) {
				int ll = tree.query(1,x);
				printf("%d\n",ll);
				tree.add(1,ll,ll+x-1,1);
			}
			else printf("%d\n",0);
		}
		else {
			scanf("%d",&y);
			tree.add(1,x,x+y-1,2);
		}
	}
	return 0;
}
2023/8/4 23:34
加载中...