poj-3367(线段树+区间合并)

发布时间 2023-04-08 15:56:41作者: 魏老6

Hotel

POJ - 3667

思路:与hdu-1540(线段树+区间合并) - 魏老6 - 博客园 (cnblogs.com)类似,只不过是区间修改,多维护一个最大连续区间sum。

#define _CRT_SECURE_NO_WARNINGS 1
#include<algorithm>
#include<fstream>
#include<iostream>
#include<cstdio>
#include<deque>
#include<string>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
//#include<unordered_map>
using namespace std;
#define INF 2e9
#define MAXN 310000
#define N 1000100
#define M 10007
#define endl '\n'
#define exp 1e-8
#define lc p << 1
#define rc p << 1|1
#define lowbit(x) ((x)&-(x))
const double pi = acos(-1.0);
typedef long long LL;
typedef unsigned long long ULL;
inline ULL read() {
	ULL x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch>'9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
void print(ULL x) {
	if (x > 9)print(x / 10);
	putchar(x % 10 ^ 48);
}
struct tree
{
	int l, r, sum, pre, suf,tag;  //sum最大连续区间长度,pre最长前缀区间长度,suf最长后缀区间长度,tag标记
}tr[4*N];
int n, m;
void pushup(int p)
{
	int len = tr[p].r - tr[p].l + 1;
	tr[p].pre = tr[lc].pre;
	tr[p].suf = tr[rc].suf;
	tr[p].sum = tr[lc].suf + tr[rc].pre;
	if (tr[lc].pre == len - (len >> 1))
	{
		tr[p].pre = tr[lc].pre + tr[rc].pre;
	}
	if (tr[rc].suf == len >> 1)
	{
		tr[p].suf = tr[lc].suf + tr[rc].suf;
	}
	tr[p].sum = max(tr[p].sum, max(tr[lc].sum, tr[rc].sum));  //在左边,右边,中间取最大值
}
void pushdown(int p)  //下传标记, tag==1 为退房,-1为开房
{
	if (tr[p].tag == 1)
	{
		int len = tr[lc].r - tr[lc].l + 1;
		tr[lc].pre = tr[lc].suf = tr[lc].sum = len;
		len = tr[rc].r - tr[rc].l + 1;
		tr[rc].pre = tr[rc].suf = tr[rc].sum = len;
		tr[lc].tag = 1;
		tr[rc].tag = 1;
		tr[p].tag = 0;
	}
	else if (tr[p].tag == -1)
	{
		tr[lc].pre = tr[lc].suf = tr[lc].sum = 0;
		tr[rc].pre = tr[rc].suf = tr[rc].sum = 0;
		tr[lc].tag = -1;
		tr[rc].tag = -1;
		tr[p].tag = 0;
	}
}
void build(int p, int l, int r)
{
	tr[p].tag = 0;
	tr[p].l = l, tr[p].r = r;
	if (l == r)
	{
		tr[p].pre = tr[p].suf = tr[p].sum = 1;
		return;
	}
	int m = l + r >> 1;
	build(lc, l, m);
	build(rc, m + 1, r);
	pushup(p);
}
void update(int p,int x,int y,int k)
{
	if (x <= tr[p].l && tr[p].r <= y)
	{
		int len = tr[p].r - tr[p].l + 1;
		if (k == 1)
		{
			tr[p].pre = tr[p].suf = tr[p].sum = len;
			tr[p].tag = 1;
		}
		else
		{
			tr[p].pre = tr[p].suf = tr[p].sum = 0;
			tr[p].tag = -1;
		}
		return;
	}
	pushdown(p);
	int m = tr[p].l + tr[p].r >> 1;
	if (x <= m)update(lc, x, y, k);
	if (y > m)update(rc, x, y, k);
	pushup(p);
}
int query(int p,int m)  
{
	if (tr[p].l == tr[p].r)return tr[p].l; 
	pushdown(p);
	int mid = tr[p].r + tr[p].l >> 1;
	if (tr[lc].sum >= m)return query(lc, m);       //如果左边有大于m间房,走左边
	if (tr[lc].suf + tr[rc].pre >= m)return mid - tr[lc].suf + 1;  //如果中间连续取件大于m,返回左端点
	else return query(rc, m);    //走右边
}
int main()
{
	n = read(), m = read();
	build(1, 1, n);
	while (m--)
	{
		int op = 0, x = 0, y = 0;
		op = read(), x = read();
		if (op == 1)
		{
			if (tr[1].sum >= x)
			{
				int k = query(1, x);
				update(1, k, k + x - 1, -1);
				printf("%d\n", k);
			}
			else puts("0");
		}
		else
		{
			y = read();
			update(1, x, x + y - 1, 1);
		}
	}
	return 0;
}