[POJ 1823] Hotel 线段树区间合并

Hotel

Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 2064 Accepted: 885

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

此题是一道线段树的区间合并的入门题。。好久没有做过区间合并了。。这次拿来回顾一下。。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAX_N = 16000+100;
int lazy[MAX_N<<2],lseg[MAX_N<<2],rseg[MAX_N<<2],maxn[MAX_N<<2];
int N,P;

void push_up(int idx,int l,int r){
    lseg[idx] = lseg[idx<<1];
    rseg[idx] = rseg[idx<<1|1];
    int m = l+r>>1;
    if( lseg[idx]==m-l+1 ) lseg[idx] += lseg[idx<<1|1];
    if( rseg[idx]==r-m ) rseg[idx] += rseg[idx<<1];
    maxn[idx] = max(lseg[idx],max(rseg[idx],rseg[idx<<1]+lseg[idx<<1|1]));
    maxn[idx] = max(maxn[idx],max(maxn[idx<<1],maxn[idx<<1|1])); // 这里第一次写也忘记了
}

void push_down(int idx,int l,int r){
    if( lazy[idx]>=0 ){
        lazy[idx<<1] = lazy[idx<<1|1] = lazy[idx];
        int m = l+r >>1;
        maxn[idx<<1] = lseg[idx<<1] = rseg[idx<<1] = (m-l+1)*lazy[idx]; // 我这里写的时候逗比了,忘记了rseg[idx<<1]
        maxn[idx<<1|1] = lseg[idx<<1|1] = rseg[idx<<1|1] = (r-m)*lazy[idx]; // 我这里写的时候逗比了,忘记了lseg[idx<<1|1]
        lazy[idx] = -1;
    }
}

void build(int idx,int l,int r){
    lazy[idx] = -1;
    lseg[idx] = rseg[idx] = r-l+1;
    if( l==r ) return;
    int m = l+r>>1;
    build(idx<<1,l,m);
    build(idx<<1|1,m+1,r);
    push_up(idx,l,r);
}

void update(int L,int R,int x,int idx,int l,int r){
    if( L>r||R<l ) return;
    if( L<=l&&R>=r ){
        lseg[idx] = rseg[idx] = maxn[idx] = x*(r-l+1);
        lazy[idx] = x;
        return;
    }
    push_down(idx,l,r);
    int m = l+r>>1;
    if(L<=m) update(L,R,x,idx<<1,l,m);
    if(R>m) update(L,R,x,idx<<1|1,m+1,r);
    push_up(idx,l,r);
}

// 因为我的CB逗比了  打不了断点。。所以我非得写这么个函数来用用   其实还是蛮好用哒~
void display(int idx,int l,int r){
    printf("[idx=%d,l=%d,r=%d]:lseg[idx]=%d,rseg[idx]=%d,maxn[idx]=%d,lazy[idx]=%d\n",idx,l,r,lseg[idx],rseg[idx],maxn[idx],lazy[idx]);
    if( l==r ) return;
    int m = l+r >>1;
    display(idx<<1,l,m);
    display(idx<<1|1,m+1,r);
}

int main(){
    while(scanf("%d%d",&N,&P)!=EOF){
        build(1,1,N);
        while( P-- ){
            int cmd;
            scanf("%d",&cmd);
            if(cmd==3){
                printf("%d\n",maxn[1]);
            } else {
                int l,r;
                scanf("%d%d",&l,&r);
                if( cmd==1 ) update(l,l+r-1,0,1,1,N);
                if( cmd==2 ) update(l,l+r-1,1,1,1,N);
                //display(1,1,N);
            }
        }
    }
    return 0;
}
点赞