【POJ1823】【线段树】Hotel

Description

The "Informatics" hotel is one of the most luxurious hotels from Galaciuc. A lot of tourists arrive or leave this hotel in one year. So it is pretty difficult to keep the evidence of the occupied rooms. But this year the owner of the hotel decided to do some changes. That‘s why he engaged you to write an efficient program that should respond to all his needs.

Write a program that should efficiently respond to these 3 types of instructions: 
type 1: the arrival of a new group of tourists 
A group of M tourists wants to occupy M free consecutive rooms. The program will receive the number i which represents the start room of the sequence of the rooms that the group wants to occupy and the number M representing the number of members of the group. It is guaranteed that all the rooms i,i+1,..,i+M-1 are free at that moment. 
type 2: the departure of a group of tourists 
The tourists leave in groups (not necessarilly those groups in which they came). A group with M members leaves M occupied and consecutive rooms. The program will receive the number i representing the start room of the sequence of the released rooms and the number M representing the number of members of the group. It is guaranteed that all the rooms i,i+1,..,i+M-1 are occupied. 
type 3: the owner‘s question 
The owner of the hotel may ask from time to time which is the maximal length of a sequence of free consecutive rooms. He needs this number to know which is the maximal number of tourists that could arrive to the hotel. You can assume that each room may be occupied by no more than one tourist.

Input

On the first line of input, there will be the numbers N (3 <= N <= 16 000) representing the number of the rooms and P (3 <= P <= 200 000) representing the number of the instructions.

The next P lines will contain the number c representing the type of the instruction:

  • if c is 1 then it will be followed (on the same line) by 2 other numbers, i and M, representing the number of the first room distributed to the group and the number of the members
  • if c is 2 then it will be followed (on the same line) by 2 other numbers, i and M, representing the number of the first room that will be released and the number of the members of the group that is leaving
  • if c is 3 then it will not be followed by any number on that line, but the program should output in the output file the maximal length of a sequence of free and consecutive rooms

Output

In the output you will print for each instruction of type 3, on separated lines, the maximal length of a sequence of free and consecutive rooms. Before the first instruction all the rooms are free.

Sample Input

12 10
3
1 2 3
1 9 4
3
2 2 1
3
2 9 2
3
2 3 2
3 

Sample Output

12
4
4
6
10

Source

Romania OI 2002

【分析】

题目太水都不好意思发了。

贴诗。

  1 /*
  2 唐代许浑
  3 《咸阳城东楼 / 咸阳城西楼晚眺 / 西门》
  4
  5 一上高城万里愁,蒹葭杨柳似汀洲。
  6 溪云初起日沉阁,山雨欲来风满楼。
  7 鸟下绿芜秦苑夕,蝉鸣黄叶汉宫秋。
  8 行人莫问当年事,故国东来渭水流。
  9 */
 10 #include <iostream>
 11 #include <cstdio>
 12 #include <algorithm>
 13 #include <cstring>
 14 #include <vector>
 15 #include <utility>
 16 #include <iomanip>
 17 #include <string>
 18 #include <cmath>
 19 #include <queue>
 20 #include <assert.h>
 21 #include <map>
 22 #include <ctime>
 23 #include <cstdlib>
 24 #include <stack>
 25 #define LOCAL
 26 const int MAXN = 1600000 + 10;
 27 const int MAXM = 75 + 10;
 28 const int INF = 100000000;
 29 const int SIZE = 450;
 30 const int maxnode =  0x7fffffff + 10;
 31 using namespace std;
 32 int i;
 33 struct SEGTREE{
 34        struct Node{
 35               int l, r;
 36               int rmax, mmax, lmax;//分别代表从左边开始的最长,从右边开始的最长和中间的最长
 37               int delta;
 38
 39               /*void Count(){
 40                    rmax = lmax = mmax = 0;
 41                    for (int i = l; i <= r; i++) if (data[i] == 0){lmax = i - l;break;}
 42                    for (int i = r; i >= l; i--) if (data[i] == 0){rmax = r - i;break;}
 43                    int t = 0;
 44                    for (int i = l; i <= r; i++){
 45
 46                    }
 47               }*/
 48        }tree[MAXN * 4];
 49
 50        void pushdown(int t){
 51             if (tree[t].delta  != -1){
 52                if (tree[t].delta == 1) {//全1
 53                   tree[(t<<1)].lmax = tree[(t<<1)].rmax = tree[(t<<1)].mmax = tree[(t<<1)].r - tree[(t<<1)].l + 1;tree[(t<<1)].delta = 1;
 54                   tree[(t<<1) | 1].lmax = tree[(t<<1) | 1].rmax = tree[(t<<1) | 1].mmax = tree[(t<<1) | 1].r - tree[(t<<1) | 1].l + 1;tree[(t<<1) | 1].delta = 1;
 55                   tree[t].delta = -1;
 56                }else{
 57                   tree[(t<<1)].lmax = tree[(t<<1)].rmax = tree[(t<<1)].mmax = 0;tree[(t<<1)].delta = 0;
 58                   tree[(t<<1) | 1].lmax = tree[(t<<1) | 1].rmax = tree[(t<<1) | 1].mmax = 0;tree[(t<<1) | 1].delta = 0;
 59                   tree[t].delta = -1;
 60                }
 61             }
 62        }
 63        //更新
 64        void update(int t){
 65             tree[t].mmax = max(tree[t<<1].mmax, max(tree[(t<<1)|1].mmax, tree[t<<1].rmax + tree[(t<<1)|1].lmax));
 66             //更新tree[t]的lmax
 67             if (tree[t<<1].lmax == tree[t<<1].r - tree[t<<1].l + 1) tree[t].lmax = tree[t<<1].lmax + tree[(t<<1)|1].lmax;
 68             else tree[t].lmax = tree[t<<1].lmax;
 69
 70             //同理
 71             if (tree[(t<<1)|1].rmax == tree[(t<<1)|1].r - tree[(t<<1)|1].l + 1) tree[t].rmax = tree[t<<1].rmax + tree[(t<<1)|1].rmax;
 72             else tree[t].rmax = tree[(t<<1)|1].rmax;
 73        }
 74        void build(int t, int l, int r){
 75             tree[t].l = l;
 76             tree[t].r = r;
 77             tree[t].lmax = tree[t].mmax = tree[t].rmax = tree[t].r - tree[t].l + 1;
 78             tree[t].delta = -1;
 79             if (l == r) return;
 80             int mid = (l + r) >> 1;
 81             build(t << 1, l, mid);
 82             build((t << 1)|1, mid + 1, r);
 83        }
 84        void insert(int t, int l, int r, int val){//t为节点编号,val为权值
 85             pushdown(t);
 86             if (l <= tree[t].l && tree[t].r <= r){
 87                if (val == 1) {tree[t].rmax = tree[t].lmax = tree[t].mmax = tree[t].r - tree[t].l + 1;tree[t].delta = 1;}
 88                else {tree[t].rmax = tree[t].lmax = tree[t].mmax = 0;tree[t].delta = 0;}
 89                return;
 90             }
 91             int mid = (tree[t].l + tree[t].r)>>1;
 92             //if (i == 3 && tree[t].l == 10 && tree[t].r == 11)
 93             //printf("");
 94             if (l <= mid) insert(t << 1, l, r , val);
 95             if (r > mid) insert((t << 1) | 1, l, r, val);
 96
 97             update(t);
 98        }
 99 }A;
100 int n, p;
101
102 void init(){
103      scanf("%d%d", &n, &p);
104      A.build(1, 1, n);
105 }
106 void work(){
107      for (i = 1; i <= p; i++){
108          int t;
109          scanf("%d", &t);
110          if (t == 3) printf("%d\n", A.tree[1].mmax);
111          else if (t == 2){
112               int l, r;
113               scanf("%d%d", &l, &r);
114               A.insert(1, l, l + r - 1, 1);
115          }else if (t == 1){
116                int l, r;
117                scanf("%d%d", &l, &r);
118                A.insert(1, l, l + r - 1, 0);
119          }
120      }
121 }
122
123 int main(){
124
125     init();
126     work();
127     return 0;
128 }

时间: 2024-10-20 12:04:32

【POJ1823】【线段树】Hotel的相关文章

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

题目传送:Hotel 思路:线段树,区间合并,区间替换,查询最左断点,看胡浩版本的线段树好几天了,今天看这个看了好久,慢慢来吧,具体都写在注释里了 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #inc

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

ACM: Hotel 解题报告 - 线段树-区间合并

Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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 th

Hotel(线段树合并)

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14958   Accepted: 6450 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(线段树+区间合并)

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

1593: [Usaco2008 Feb]Hotel 旅馆 (线段树)

1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 451  Solved: 262[Submit][Status][Discuss] Description 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排

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

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

题目传送门 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