zoukankan      html  css  js  c++  java
  • 【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

    【分析】
    题目太水都不好意思发了。
    贴诗。
      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
    ", 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 }
    View Code
  • 相关阅读:
    Java中Runnable和Thread的区别
    git 代理设置
    Android的bitmap和优化
    String、StringBuffer与StringBuilder之间区别
    工作流的一些记录
    UIAutomation调用计算器模拟自动执行
    从客户端(Content="<EM ><STRONG ><U >这是测试这...")中检测到有潜在危险的Request.Form 值。
    泛型
    基础加强
    数据库和ado
  • 原文地址:https://www.cnblogs.com/hoskey/p/4334416.html
Copyright © 2011-2022 走看看