调吐了,8pts
查看原帖
调吐了,8pts
719978
DYYqwq楼主2022/12/7 20:44

rt,讨论区的8pts和我不太一样啊

#include<bits/stdc++.h>
#define lson(root) (root << 1)
#define rson(root) (root << 1 | 1)
using namespace std;
int n , t;
struct node
{
	int lmx , rmx , mx_len , len , lazy;
};
node tree[800010];
void pushup(int root)
{
	tree[root].mx_len = max(tree[lson(root)].mx_len , tree[rson(root)].mx_len);
	tree[root].mx_len = max(tree[lson(root)].rmx + tree[rson(root)].lmx , tree[root].mx_len);
	tree[root].lmx = tree[lson(root)].lmx;
	if(tree[lson(root)].lmx == tree[lson(root)].len)
		tree[root].lmx = tree[lson(root)].len + tree[rson(root)].lmx;
	tree[root].rmx = tree[rson(root)].rmx;
	if(tree[rson(root)].rmx == tree[rson(root)].len)
		tree[root].rmx = tree[rson(root)].len + tree[lson(root)].rmx; 
}
void pushdown(int root , int l , int r)
{
	int mid = (l + r) >> 1;
	if(tree[root].lazy == 1)
	{
		tree[lson(root)].mx_len = tree[lson(root)].lmx = tree[lson(root)].rmx = 0;
		tree[lson(root)].lazy = 1;
		tree[rson(root)].mx_len = tree[rson(root)].lmx = tree[rson(root)].rmx = 0;
		tree[rson(root)].lazy = 1;
	}
	else
	{
		tree[lson(root)].mx_len = tree[lson(root)].lmx = tree[rson(root)].rmx = mid - l + 1;
		tree[lson(root)].lazy = 2;
		tree[rson(root)].mx_len = tree[rson(root)].lmx = tree[rson(root)].rmx = r - mid;
		tree[rson(root)].lazy = 2;
	}
	tree[root].lazy = 0;
}
void update(int root , int l , int r , int L , int R , int x)
{
	if(L <= l && R >= r)
	{
		if(x == 1)
		{
			tree[root].mx_len = tree[root].lmx = tree[root].rmx = 0;
			tree[root].lazy = 1;
		}
		else
		{
			tree[root].mx_len = tree[root].lmx = tree[root].rmx = r - l + 1;
			tree[root].lazy = 2;
		}
		return;
	}
	if(tree[root].lazy)
		pushdown(root , l , r);
	int mid = (l + r) >> 1;
	if(L <= mid)
		update(lson(root) , l , mid , L , R , x);
	if(R > mid)
		update(rson(root) , mid + 1 , r , L , R , x);
	pushup(root);
}
int query(int root , int l , int r , int x)
{
	if(l == r)
		return r;
	int mid = (l + r) >> 1;
	if(tree[root].lazy)
		pushdown(root , l , r);
	if(x <= tree[lson(root)].mx_len)
		return query(lson(root) , l , mid , x);
	if(x <= tree[lson(root)].rmx + tree[rson(root)].lmx)
		return mid - tree[lson(root)].rmx + 1;
	if(x <= tree[rson(root)].mx_len)
		return query(rson(root) , mid + 1 , r , x);
}
void build(int root , int l , int r)
{
	tree[root].mx_len = tree[root].lmx = tree[root].rmx = tree[root].len = r - l + 1;
	if(l == r)
		return;
	int mid = (l + r) >> 1;
	build(lson(root) , l , mid);
	build(rson(root) , mid + 1 , r);
}
int main()
{
	freopen("P2894_2.in" , "r" , stdin);
	freopen("P2894_2.txt" , "w" , stdout); 
	scanf("%d%d" , &n , &t);
	build(1 , 1 , n);
	while(t --)
	{
		int op;
		scanf("%d" , &op);
		if(op == 1)
		{
			int x;
			scanf("%d" , &x);
			if(tree[1].mx_len < x)
				printf("0\n");
			else
			{
				int ans = query(1 , 1 , n , x);
				printf("%d\n" , ans);
				if(ans != 0)
					update(1 , 1 , n , ans , ans + x - 1 , 1);
			}
		}
		else
		{
			int x , y;
			scanf("%d%d" , &x , &y);
			update(1 , 1 , n , x , x + y - 1 , 2);
		}
	}
	return 0;
}
2022/12/7 20:44
加载中...