Hotel POJ - 1823 (线段树区间更新,区间合并,求最长连续区间)

题目连接:http://poj.org/problem?id=1823

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

题意:给你n个房子,然后q个询问

当op = 1 时,a ~ a+b-1 区间被占

当op = 2 时,a ~ a+b-1 区间清空

当op = 3 时,查询1-n内最大的连续free房间数

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define ll long long
#define lson cur << 1
#define rson cur << 1 | 1
using namespace std;

const int maxn = 20000;

struct node {
    int lazy;
    int maxx, maxl, maxr, len;
}e[maxn << 2];

inline void pushup(int cur){
    e[cur].len = e[lson].len + e[rson].len;
    e[cur].maxx = max(e[lson].maxx, max(e[rson].maxx, e[lson].maxr + e[rson].maxl));
    e[cur].maxl = e[lson].maxl + (e[lson].maxl == e[lson].len ? e[rson].maxl : 0);
    e[cur].maxr = e[rson].maxr + (e[rson].maxr == e[rson].len ? e[lson].maxr : 0);
}
inline void pushdown(int cur){
    if(e[cur].lazy == 1) {
        e[lson].maxx = e[lson].maxl = e[lson].maxr = 0;
        e[rson].maxx = e[rson].maxl = e[rson].maxr = 0;
        e[rson].lazy = e[cur].lazy;
        e[lson].lazy = e[cur].lazy;
        e[cur].lazy = 0;
    }
    else if(e[cur].lazy == -1) {
        e[lson].maxx = e[lson].maxl = e[lson].maxr = e[lson].len;
        e[rson].maxx = e[rson].maxl = e[rson].maxr = e[rson].len;
        e[rson].lazy = e[cur].lazy;
        e[lson].lazy = e[cur].lazy;
        e[cur].lazy = 0;
    }
}

inline void build(int l, int r, int cur){
    e[cur].maxx = 0;
    e[cur].maxl = 0;
    e[cur].maxr = 0;
    if(l == r){
        e[cur].maxx = e[cur].maxl = e[cur].maxr = e[cur].len = 1;
        return;
    }
    int mid = l + r >> 1;
    build(l, mid, lson);
    build(mid + 1, r, rson);
    pushup(cur);
}

void update(int l, int r, int cur, int L, int R, int val){
    if(l <= L && R <= r){
        e[cur].lazy = val;
        if(val == -1) {
            e[cur].maxx = e[cur].maxl = e[cur].maxr = e[cur].len;
        }
        else {
            e[cur].maxx = e[cur].maxl = e[cur].maxr = 0;
        }
        return ;
    }
    pushdown(cur);
    int mid = L + R >> 1;
    if(l <= mid) update(l, r, lson, L, mid, val);
    if(r > mid)  update(l, r, rson, mid + 1, R, val);
    pushup(cur);
}

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    build(1, n, 1);
    while(q--) {
        int a, b, c;
        scanf("%d", &c);
        if(c == 3) {
            printf("%d\n", e[1].maxx);
        }
        else {
            scanf("%d%d", &a, &b);
            if(c == 2) c = -1;
            update(a, a + b - 1, 1, 1, n, c);
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值