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

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 18289 Accepted: 7937

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 Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and D(b) Three space-separated integers representing a check-out: 2, Xi, and Di

Output

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5
题意:给出一段长为n的区间,有两种操作:1、查询最靠左并且长度为d的连续区间,并输出其左端点下标;2、清空以l为左端点
长度为d的连续区间
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 50000 + 10;
int n, m;
struct xx{
    int l, r, lm, rm, m, lazy;
} T[N<<2];

void Pushdown(int k){
    T[k<<1].lazy = T[k<<1|1].lazy = T[k].lazy;
    T[k<<1].lm = T[k<<1].rm = T[k<<1].m = T[k].lazy ? 0 : T[k<<1].r-T[k<<1].l+1;
    T[k<<1|1].lm = T[k<<1|1].rm = T[k<<1|1].m = T[k].lazy ? 0 : T[k<<1|1].r-T[k<<1|1].l+1;
    T[k].lazy = -1;
}

void Pushup(int k){
    T[k].lm = T[k<<1].lm;
    T[k].rm = T[k<<1|1].rm;
    if(T[k].lm == T[k<<1].r-T[k<<1].l+1) T[k].lm += T[k<<1|1].lm;
    if(T[k].rm == T[k<<1|1].r-T[k<<1|1].l+1) T[k].rm += T[k<<1].rm;    T[k].m = max(max(T[k<<1].m, T[k<<1|1].m), T[k<<1].rm+T[k<<1|1].lm);
}

void Build(int l, int r, int k){
    T[k].l = l, T[k].r = r, T[k].lazy = -1;
    T[k].lm = T[k].rm = T[k].m = r-l+1;
    if(l == r) return;
    int mid = (l+r)>>1;
    Build(l, mid, k<<1);
    Build(mid+1, r, k<<1|1);
    Pushup(k);
}

void Update(int l, int r, int v, int k){
    if(l == T[k].l && r == T[k].r){
        T[k].lm = T[k].rm = T[k].m = v ? 0 : T[k].r-T[k].l+1;
        T[k].lazy = v;
        return;
    }
    if(T[k].lazy != -1) Pushdown(k);
    int mid = (T[k].l+T[k].r)>>1;
    if(l <= mid) Update(l, min(mid, r), v, k<<1);
    if(r > mid) Update(max(mid+1, l), r, v, k<<1|1);
    Pushup(k);
}

int Find(int x, int k){
    if(T[k].l == T[k].r) return T[k].l;
    if(T[k].lazy != -1) Pushdown(k);
    int mid = (T[k].l+T[k].r)>>1;
    if(T[k<<1].m >= x) return Find(x, k<<1);
    else if(T[k<<1].rm+T[k<<1|1].lm >= x) return mid-T[k<<1].rm+1;
    else if(T[k<<1|1].m >= x) return Find(x, k<<1|1);
}

int main(){
    while(scanf("%d%d", &n, &m) == 2){
        Build(1, n, 1);
        int op, x, y;;
        for(int i = 0; i < m; i++){
            scanf("%d", &op);
            if(op&1){
                scanf("%d", &x);
                if(T[1].m < x){
                    printf("0\n");
                    continue;
                }
                y = Find(x, 1);
                printf("%d\n", y);
                if(y) Update(y, x+y-1, 1, 1);
            }
            else{
                scanf("%d%d", &x, &y);
                Update(x, x+y-1, 0, 1);
            }
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值