POJ 3667 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 D(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
  1 #include<cstdio>
  2 #include<cstring>
  3 #define max(a,b) (a>b?a:b)
  4 using namespace std;
  5 struct f
  6 {
  7     int x,y,z,v;
  8 };f tree[200200];
  9 void build(int l,int r,int p)
 10 {
 11     tree[p].x=tree[p].y=tree[p].z=r-l+1;
 12     tree[p].v=-1;
 13     if (l==r) return ;
 14     int m=(l+r)/2;
 15     build(l,m,p*2);
 16     build(m+1,r,p*2+1);
 17 }
 18 void pushdown(int x,int p)
 19 {
 20     if (tree[p].v!=-1)
 21     {
 22         tree[p*2].v=tree[p*2+1].v=tree[p].v;
 23         if (tree[p].v==0)
 24         {
 25             tree[p*2].x=tree[p*2].y=tree[p*2].z=x-(x/2);
 26             tree[p*2+1].x=tree[p*2+1].y=tree[p*2+1].z=x/2;
 27         }
 28         else
 29         {
 30             tree[p*2].x=tree[p*2].y=tree[p*2].z=0;
 31             tree[p*2+1].x=tree[p*2+1].y=tree[p*2+1].z=0;
 32         }
 33         tree[p].v=-1;
 34     }
 35 }
 36 void pushup(int x,int p)
 37 {
 38     tree[p].x=tree[p*2].x;
 39     tree[p].y=tree[p*2+1].y;
 40     if (tree[p].x==x-(x/2)) tree[p].x+=tree[p*2+1].x;
 41     if (tree[p].y==x/2) tree[p].y+=tree[p*2].y;
 42     tree[p].z=max(tree[p*2].y+tree[p*2+1].x,max(tree[p*2].z,tree[p*2+1].z));
 43 }
 44 int find(int l,int r,int w,int p)
 45 {
 46     if (l==r) return l;
 47     pushdown(r-l+1,p);
 48     int m=(l+r)/2;
 49     if (tree[p*2].z>=w) return find(l,m,w,p*2);
 50     if (tree[p*2].y+tree[p*2+1].x>=w) return m-tree[p*2].y+1;
 51     return find(m+1,r,w,p*2+1);
 52 }
 53 void un(int ll,int rr,int l,int r,int p,int w)
 54 {
 55     if (ll<=l&&rr>=r)
 56     {
 57         if (w) tree[p].x=tree[p].y=tree[p].z=0;
 58         else tree[p].x=tree[p].y=tree[p].z=r-l+1;
 59         tree[p].v=w;
 60         return ;
 61     }
 62     pushdown(r-l+1,p);
 63     int m=(l+r)/2;
 64     if (m<ll) un(ll,rr,m+1,r,p*2+1,w);
 65     else if (m>=rr) un(ll,rr,l,m,p*2,w);
 66     else
 67     {
 68         un(ll,m,l,m,p*2,w);
 69         un(m+1,rr,m+1,r,p*2+1,w);
 70     }
 71     pushup(r-l+1,p);
 72 }
 73 int main()
 74 {
 75     int n,m,a,b;
 76     while (~scanf("%d%d",&n,&m))
 77     {
 78         build(1,n,1);
 79         while (m--)
 80         {
 81             scanf("%d",&a);
 82             if (a==1)
 83             {
 84                 scanf("%d",&b);
 85                 if (b>tree[1].z)
 86                 {
 87                     printf("0\n");
 88                     continue;
 89                 }
 90                 a=find(1,n,b,1);
 91                 printf("%d\n",a);
 92                 un(a,b+a-1,1,n,1,1);
 93             }
 94             else
 95             {
 96                 scanf("%d%d",&a,&b);
 97                 un(a,b+a-1,1,n,1,0);
 98             }
 99         }
100     }
101 }
时间: 2024-11-21 01:02:36

POJ 3667 Hotel (线段树)的相关文章

poj 3667 Hotel - 线段树

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 v

POJ 3667 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 Stree

POJ 3667 Hotel 线段树 区间合并

题意: 1 输入a:询问是不是有连续长度为a的空房间,有的话住进最左边 2 输入a b:将[a,a+b-1]的房间清空 思路:记录区间中最长的空房间 线段树操作: update:区间替换 query:询问满足条件的最左端点 #include <cstdio> #include <iostream> #include <algorithm> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1

POJ 3667 Hotel ( 线段树区间合并 )

题目链接~~> 做题感悟:这题是接触线段树区间合并的第一题,做的很纠结. 解题思路: 注意线段树上节点代表的信息 : 每个节点需要维护 lc , rc , mc ,add ,见下图: add 为懒惰标记.假设 i 代表父亲节点编号,左儿子为  i * 2  ,右儿子为 i * 2  + 1 ,那么我们可以得到 : T [ i ] .lc 首先加上左儿子的左边的空格数,然后需要判断一下,如果左儿子的左节点占满了整个左区间时需要再加上右儿子的左边的空格数.同理 T [ i ] .rc 也可以这样得到

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

POJ 1823 Hotel 线段树

题目链接 和上一题差不多....第三种操作只需要输出maxx[1]的值就可以. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string>

线段树(区间合并) POJ 3667 Hotel

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt

POJ 3667 Hotel(线段树)

POJ 3667 Hotel 题目链接 题意:有n个房间,现在有两个操作 1.找到连续长度a的空房间,入住,要尽量靠左边,如果有输出最左边的房间标号,如果没有输出0 2.清空[a, a + b - 1]的房间 思路:线段树的区间合并,记录下左边连续最长和右边连续最长空房间,和每一段的最大值,这样pushup的时候就是进行区间合并,注意查询的时候由于是要尽量左,所以先查左孩子,在查横跨左右的,在查右孩子 代码: #include <cstdio> #include <cstring>

POJ 2481 Cows (线段树)

Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每只牛有一个值[S,E],如果对于牛i和牛j来说,它们的值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj.现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮. 思路:将牛按照S从小到大排序,S相同按照E从大到小排序,这就保证了排在后面的牛一定不比前面的牛强壮.再按照E值(离散化后)建立一颗线段树(这里最值只有1e5,所

POJ 2299 离散化线段树

点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted: 14752 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by