线段树(区间合并) 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     详细解释:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html
 8 */
 9 #include <cstdio>
10 #include <algorithm>
11 #include <cstring>
12 using namespace std;
13
14 #define lson l, mid, rt << 1
15 #define rson mid + 1, r, rt << 1 | 1
16 const int MAXN = 5e4 + 10;
17 const int INF = 0x3f3f3f3f;
18 struct Segment_Tree {
19     int sum[MAXN<<2], lsum[MAXN<<2], rsum[MAXN<<2], cover[MAXN<<2];
20     void push_up(int rt, int len)    {
21         lsum[rt] = lsum[rt<<1];
22         rsum[rt] = rsum[rt<<1|1];
23         if (lsum[rt] == len - (len>>1)) lsum[rt] += lsum[rt<<1|1];
24         if (rsum[rt] == len>>1)   rsum[rt] += rsum[rt<<1];
25         sum[rt] = max (rsum[rt<<1] + lsum[rt<<1|1], max (sum[rt<<1], sum[rt<<1|1]));
26     }
27     void push_down(int rt, int len) {
28         if (cover[rt] != -1)    {
29             cover[rt<<1] = cover[rt<<1|1] = cover[rt];
30             sum[rt<<1] = lsum[rt<<1] = rsum[rt<<1] = cover[rt] ? 0 : len - (len>>1);
31             sum[rt<<1|1] = lsum[rt<<1|1] = rsum[rt<<1|1] = cover[rt] ? 0 : len >> 1;
32             cover[rt] = -1;
33         }
34     }
35     void build(int l, int r, int rt)    {
36         sum[rt] = lsum[rt] = rsum[rt] = r - l + 1;
37         cover[rt] = -1;
38         if (l == r) return ;
39         int mid = (l + r) >> 1;
40         build (lson);   build (rson);
41     }
42     void updata(int ql, int qr, int c, int l, int r, int rt)   {
43         if (ql <= l && r <= qr) {
44             sum[rt] = lsum[rt] = rsum[rt] = c ? 0 : (r - l + 1);
45             cover[rt] = c;  return ;
46         }
47         push_down (rt, r - l + 1);
48         int mid = (l + r) >> 1;
49         if (ql <= mid)  updata (ql, qr, c, lson);
50         if (qr > mid)   updata (ql, qr, c, rson);
51         push_up (rt, r - l + 1);
52     }
53     int query(int w, int l, int r, int rt) {
54         if (l == r) return l;
55         push_down (rt, r - l + 1);
56         int mid = (l + r) >> 1;
57         if (sum[rt<<1] >= w)    return query (w, lson);
58         else if (rsum[rt<<1] + lsum[rt<<1|1] >= w)  return mid - rsum[rt<<1] + 1;
59         else    return query (w, rson);
60     }
61 }st;
62
63 int main(void)  {       //POJ 3667 Hotel
64     int n, m;
65     while (scanf ("%d%d", &n, &m) == 2) {
66         st.build (1, n, 1);
67         for (int i=1; i<=m; ++i)    {
68             int op, a, b; scanf ("%d%d", &op, &a);
69             if (op == 1)    {
70                 if (st.sum[1] < a)  puts ("0");
71                 else    {
72                     int p = st.query (a, 1, n, 1);
73                     printf ("%d\n", p);
74                     st.updata (p, p + a - 1, 1, 1, n, 1);
75                 }
76             }
77             else    {
78                 scanf ("%d", &b);
79                 st.updata (a, a + b - 1, 0, 1, n, 1);
80             }
81         }
82     }
83
84     return 0;
85 }
时间: 2024-10-20 11:15:26

线段树(区间合并) POJ 3667 Hotel的相关文章

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

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——————【线段树区间合并】

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 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

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

Language: Default Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12417   Accepted: 5346 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lak

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

题意: 有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作 1 w 表示在总区间内查询一个长度为w的可用区间并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0 2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出. 思路: 这是第一次遇到线段树区间合并的题目,写下感悟,还是对线段的更新和查询工作,但是查询的对象的性质已经不像单点那样,查询的是某个线段的最大可用区间是多少,还要一并

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

线段树 区间合并

poj3667 Hotel 区间合并入门题,照着代码打的, 题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边       2 a b:将[a,a+b-1]的房间清空思路:记录区间中最长的空房间,开三个数组,msum[rt]表示节点rt内连续的1的个数的最大值,lsum[rt]表示从节点rt左端点开始连续1的个数,rsum[rt]表示从节点rt右端点开始连续1的个数..线段树操作:update:区间替换 query:询问满足条件的最左端点 1 #include<iostream>

HDU 3911 Black And White(线段树区间合并)

Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she wan