线段树区间合并——POJ 3667

来源:互联网 发布:ai软件下载教程 编辑:程序博客网 时间:2024/06/04 18:21

对应POJ题目:点击打开链接


Hotel
Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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 61 31 31 31 32 5 51 6

Sample Output

14705

Source



题意:有1~n个房间, 有m个操作,如果是(1, x)就表示从n个房间中找出尽可能靠左的连续的x个空房间,并输出最左边的房间号,如果没有就输出0;  

(2, x,y)就表示把从第x个房间开始的连续y个房间清空。


线段树区间合并第一道。。。

就是每个结点维护4个值,room[rt]=该区间的最长连续空房间数; lroom[rt]=该区间从左边界开始往右一共有多少个连续空房间数; rroom[rt]=该区间从右边界开始往左一共有多少个连续的空房间数。

查询时先查左区间room[rt<<1] >= len 是否成立, 是则继续查左区间; 否则 查 左子区间从右边界往左数的连续空房间数+右子区间从左边界往右数的连续空房间数  是否 >= len;  即rroom[rt<<1] + lroom[rt<<1|1] >= len 是否成立,是则返回结果 ,设mid=(left + right)>>1; 则结果 ans = mid - rroom[rt<<1] + 1; 再否则 查 右子区间


#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>#define ms(x,y) memset(x,y,sizeof(x))#define eps 1e-6const int MAXN=50000+10;const int INF=1<<30;using namespace std;int room[MAXN<<2];int lroom[MAXN<<2];int rroom[MAXN<<2];int cover[MAXN<<2];void down(int rt, int len){if(cover[rt] != -1){cover[rt<<1] = cover[rt<<1|1] = cover[rt];room[rt<<1] = lroom[rt<<1] = rroom[rt<<1] = cover[rt]?0:(len-(len>>1));room[rt<<1|1] = lroom[rt<<1|1] = rroom[rt<<1|1] = cover[rt]?0:(len>>1);cover[rt] = -1;}}void up(int rt, int len){lroom[rt]=lroom[rt<<1];rroom[rt]=rroom[rt<<1|1];if(lroom[rt] == len-(len>>1)) lroom[rt] += lroom[rt<<1|1];//左子区间全为空房间,就要加上右子区间从左往右数的连续空房间数if(rroom[rt] == (len>>1)) rroom[rt] += rroom[rt<<1];//右子区间全为空房间,就要加上左子区间从右往左数的连续空房间数room[rt] = max(rroom[rt<<1] + lroom[rt<<1|1], max(room[rt<<1], room[rt<<1|1]));}void buildline(int rt, int left, int right){room[rt]=lroom[rt]=rroom[rt]= right - left + 1;if(left==right) return;int mid=(left+right)>>1;buildline(rt<<1, left, mid);buildline(rt<<1|1, mid+1, right);}void updata(int rt, int left, int right, int l, int r, int flag){if(l == left && right == r){cover[rt]=flag;room[rt]=lroom[rt]=rroom[rt]=flag?0:right-left+1;return;}down(rt, right-left+1);int mid=(left+right)>>1;if(mid >= r) updata(rt<<1, left, mid, l, r, flag);else if(mid < l) updata(rt<<1|1, mid+1, right, l, r, flag);else{updata(rt<<1, left, mid, l, mid, flag);updata(rt<<1|1, mid+1, right, mid+1, r, flag);}up(rt, right-left+1);}int query(int rt, int left, int right, int len){if(left == right) return left;down(rt, right-left+1);int mid=(left + right)>>1;if(room[rt<<1] >= len) return query(rt<<1, left, mid, len);else if(rroom[rt<<1] + lroom[rt<<1|1] >= len) return mid - rroom[rt<<1] + 1;return query(rt<<1|1, mid+1, right, len);}int main(){//freopen("in.txt","r",stdin);int n,m;scanf("%d%d", &n,&m);buildline(1,1,n);int op,a,b;while(m--){scanf("%d", &op);if(op==1){scanf("%d", &a);if(room[1] < a){printf("0\n");continue;}int res = query(1,1,n,a);printf("%d\n", res);updata(1,1,n,res,res+a-1, 1);}else{scanf("%d%d", &a,&b);updata(1,1,n,a,a+b-1, 0);}}return 0;}





0 0
原创粉丝点击