首页 > 其他分享 >POJ 3667 Hotel(线段树:区间覆盖+维护最大连续子区间长度)

POJ 3667 Hotel(线段树:区间覆盖+维护最大连续子区间长度)

时间:2023-02-07 12:00:52浏览次数:59  
标签:3667 return int sum Hotel tree ans 区间 lx


Hotel

Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

Output

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

Source

 

 

​USACO 2008 February Gold​

Time Limit: 3000MS

 

Memory Limit: 65536K

Total Submissions: 21673

 

Accepted: 9442

 

题意:

长度为n的区间,m个操作,一开始都是0

1 x表示求出长度为x的0的连续区间的最左端,并把这个区间变成1

2 x y表示将区间[x,y]变成0

分析:

线段树区间合并的入门题,注意pushdow和pushup操作。

区间赋值:需要懒惰标记,题意需要覆盖标记,直接flag充当

整体查询(全部的)

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500006, INF = 0x3f3f3f3f;
int n, m, a[N];
typedef long long ll;
struct T {
//int l, r, sum, lx, rx, ans;
//1.两个操作:(l l r)求l和r的最大连续字段和,(0,x,y):a[x]修改成y
//区间和sum,区间最大连续字段和ans,
//紧靠左端的最大连续字段和lx,紧靠右端的最大连续字段和rx
int l,r,ls,rs,sum,flag;
//2.两个操作:(l len)求整个序列满足长度为len连续子区间度,返回左端点;(2,x,len):清除a[x]~a[x+len-1]
//懒惰笔记+覆盖标记flag:为0时表示没有被覆盖,为1时表示被覆盖了,为-1时表示子节点中既有被覆盖的也有没被覆盖的.
} tree[N*4];
void pushup(int p){
//tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
//tree[p].lx = max(tree[p<<1].lx, tree[p<<1].sum + tree[p<<1|1].lx);
//tree[p].rx = max(tree[p<<1|1].rx, tree[p<<1].rx + tree[p<<1|1].sum);
//tree[p].ans = max(max(tree[p<<1].ans, tree[p<<1|1].ans), tree[p<<1].rx + tree[p<<1|1].lx);
tree[p].ls = tree[p<<1].ls;
tree[p].rs = tree[p<<1|1].rs;
if (tree[p<<1].ls == tree[p<<1].r - tree[p<<1].l + 1)
tree[p].ls += tree[p<<1|1].ls;
if (tree[p<<1|1].rs == tree[p<<1|1].r - tree[p<<1|1].l + 1)
tree[p].rs += tree[p<<1].rs;

tree[p].sum = max(max(tree[p<<1].sum, tree[p<<1|1].sum), tree[p<<1].rs + tree[p<<1|1].ls);
}
//1问题未涉及到区间更新不需要pushdown
void pushdown(int p){

if (tree[p].flag != -1)
{
tree[p<<1].flag = tree[p<<1|1].flag = tree[p].flag;
if (tree[p<<1].flag) {
tree[p<<1].ls = tree[p<<1].rs = tree[p<<1].sum = tree[p<<1].r - tree[p<<1].l + 1;
tree[p<<1|1].ls = tree[p<<1|1].rs = tree[p<<1|1].sum = tree[p<<1|1].r - tree[p<<1|1].l + 1;
} else
tree[p<<1].ls = tree[p<<1].rs = tree[p<<1].sum = tree[p<<1|1].ls = tree[p<<1|1].rs = tree[p<<1|1].sum = 0;
tree[p].flag = -1;
}
}
void build(int p, int l, int r) {

tree[p].l = l;
tree[p].r = r;
tree[p].sum = tree[p].ls = tree[p].rs = r - l + 1;
tree[p].flag = -1;
if (l == r) {
//tree[p].sum = tree[p].lx = tree[p].rx = tree[p].ans = a[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}

//1问题单点赋值,a[x]=y
/*void change1(int p, int x, int y) {
if (tree[p].l == tree[p].r) {
tree[p].sum = tree[p].lx = tree[p].rx = tree[p].ans = y;
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
if (x <= mid) change1(p << 1, x, y);
else change1(p << 1 | 1, x, y);
pushup(p);
} */
//2问题的区间赋值
void change2(int p, int l, int r,ll val) {
if (tree[p].l >= l && tree[p].r <= r) {
if(val==0)
tree[p].ls = tree[p].rs = tree[p].sum = tree[p].flag = 0;

if(val==1)
{
tree[p].ls = tree[p].rs = tree[p].sum = tree[p].r - tree[p].l + 1;
tree[p].flag=1;
}
return;
}
pushdown(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if (l <= mid) change2(p << 1, l, r,val);
if (r > mid) change2(p << 1 | 1, l, r,val);
pushup(p);
}
//2问题的整个序列的查询最大子序列长度
int ask1(int p, int d) {
if (tree[p].l == tree[p].r) return tree[p].l;
pushdown(p);
if (tree[p<<1].sum >= d) return ask1(p << 1, d);
int mid = (tree[p].l + tree[p].r) >> 1;
if (tree[p<<1].rs + tree[p<<1|1].ls >= d) return mid - tree[p<<1].rs + 1;
if (tree[p<<1|1].sum >= d) return ask1(p << 1 | 1, d);
return -1;
}
/*
//问题1的查询l和r之间最大子序列和
T ask(int p, int l, int r) {
if (l <= tree[p].l && r >= tree[p].r) return tree[p];
T a, b, ans;
a.sum = a.lx = a.rx = a.ans = b.sum = b.lx = b.rx = b.ans = -INF;
ans.sum = 0;
int mid = (tree[p].l + tree[p].r) >> 1;
if (l <= mid) {
a = ask(p << 1, l, r);
ans.sum += a.sum;
}
if (r > mid) {
b = ask(p << 1 | 1, l, r);
ans.sum += b.sum;
}
ans.ans = max(max(a.ans, b.ans), a.rx + b.lx);
ans.lx = max(a.lx, a.sum + b.lx);
ans.rx = max(b.rx, b.sum + a.rx);
if (l > mid) ans.lx = max(ans.lx, b.lx);
if (r <= mid) ans.rx = max(ans.rx, a.rx);
return ans;
}
*/
int main() {
cin >> n >> m;
build(1, 1, n);
while (m--) {
int op, x, d;
scanf("%d", &op);
if (op == 1) {
scanf("%d", &d);
if (tree[1].sum < d) puts("0");
else {
int ans = ask1(1, d);
printf("%d\n", ans);
change2(1, ans, ans + d - 1,0);
}
} else {
scanf("%d %d", &x, &d);
change2(1, x, x + d - 1,1);
}
}
return 0;
}

 

标签:3667,return,int,sum,Hotel,tree,ans,区间,lx
From: https://blog.51cto.com/u_14932227/6041864

相关文章

  • 连号区间数
    问题描述小明这些天一直在思考这样一个奇怪而有趣的问题:在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是:如果区间[L,R]里的所有元素(即此排列的第L个......
  • 求区间内质数(素数)
    题目:判断101~200之间有多少个质数(素数),并输出全部质数(素数)。质数(又称素数),是指在大于1的自然数中,除了1和它本身外,不能被其他自然数整除(除0以外)的数称之为素数(质数)。比1大但......
  • 闭区间可导函数在在两个端点处连续的证明
    如果一个函数在闭区间\([a,b]\)内可导,那么首先\(f^{'}\)在区间\((a,b)\)任意一点都存在,且如下两个极限存在\[\lim_{h\rightarrow0^+}\frac{f(a+h)-f(a)}{h}\\\lim_{h......
  • POJ 1436 Horizontally VisibleSegments(线段树成段更新+区间覆盖染色)
    DescriptionThereisanumberofdisjointverticallinesegmentsintheplane.Wesaythattwosegmentsarehorizontallyvisibleiftheycanbeconnectedbyaho......
  • POJ 3468(树状数组+区间修改)
    题目描述给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一:1、“Clrd”,表示把A[l],A[l+1],…,A[r]都加上d。2、“Qlr”,表示询问数列中第l~r个数的和......
  • ST算法(区间最值)
    ST算法是解决RMQ(区间最值)问题,它能在O(nlogn)的时间预处理,然后酶促查询的复杂度是O(1)。其原理是倍增,f[i][j]表示从i位起的2^j个数中的最大数,即[i,i+2^j-1]中的最大值。首先,我们......
  • Java如何将若干时间区间进行合并的方法步骤
    java如何将若干时间区间进行合并的方法步骤问题原因工作中突然有个场景,需要合并时间区间。将若干闭合时间区间合并,实现思路如下:1、先对日期区间进行按时间顺序排序,这样......
  • 多区间合并
    给出多个区间,每给出一个区间就查询:使\([1,x]\)都被覆盖到的最大的\(x\)为多少可以用set<pair<int,int>>存入区间(set默认按照pair的first升序排序),每次insert一个区间后......
  • 【算法训练营day36】LeetCode435. 无重叠区间 LeetCode763. 划分字母区间 LeetCode56.
    LeetCode435.无重叠区间题目链接:435.无重叠区间独上高楼,望尽天涯路好像有点开窍了!我的思路是,升序排序(左对齐),然后按顺序遍历,遇到重叠时,拿走尾巴更长的区间,从而保证局部......
  • [概率论与数理统计]笔记:5.3 置信区间
    5.3置信区间前言点估计无法提供其估计的误差,而区间估计可以。案例:“某人的月薪比2k多,比20k少”,这就是一个区间估计。区间估计的好坏有两个衡量指标:区间长度真实值......