POJ 3667 线段树区间合并
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 9844 | Accepted: 4225 |
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 Di (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
思路:记录区间最长空房间,搞了一个晚上,好吃力。
代码:
#include<iostream> #include<stdio.h> #include<algorithm> #include<string> using namespace std; const int maxn=60000; struct node { int l,r,msum,rsum,lsum,mark; int dis(){return r-l+1;} int mid(){return (l+r)>>1;} void init(){msum=rsum=lsum=dis();} void clear(){msum=lsum=rsum=0;} }tree[10*maxn]; void pushdown(int rt) { if(tree[rt].l==tree[rt].r||!tree[rt].mark)return; tree[2*rt].mark=tree[2*rt+1].mark=tree[rt].mark; if(tree[rt].mark==2) { tree[2*rt].clear(); tree[2*rt+1].clear(); } else { tree[2*rt].init(); tree[2*rt+1].init(); } tree[rt].mark=0; } void build(int l,int r,int rt) { tree[rt].l=l; tree[rt].r=r; tree[rt].mark=0; tree[rt].init(); if(l==r)return; int mid=tree[rt].mid(); build(l,mid,2*rt); build(mid+1,r,2*rt+1); } void update(int rt) { tree[rt].msum=max(max(tree[2*rt].msum,tree[2*rt+1].msum),tree[2*rt].rsum+tree[2*rt+1].lsum); tree[rt].lsum=tree[2*rt].lsum; tree[rt].rsum=tree[2*rt+1].rsum; if(tree[2*rt].msum==tree[2*rt].dis())tree[rt].lsum+=tree[2*rt+1].lsum; if(tree[2*rt+1].msum==tree[2*rt+1].dis())tree[rt].rsum+=tree[2*rt].rsum; } void insert(int rt,int l,int r,int flag) { if(tree[rt].l>=l&&tree[rt].r<=r) { tree[rt].mark=flag; if(flag==1)tree[rt].init(); else tree[rt].clear(); return; } pushdown(rt); int mid=tree[rt].mid(); if(l<=mid)insert(2*rt,l,r,flag); if(r>mid)insert(2*rt+1,l,r,flag); update(rt); } int query(int rt,int d) { if(tree[rt].l==tree[rt].r)return tree[rt].l; pushdown(rt); int mid=tree[rt].mid(); if(tree[rt*2].msum>=d)return query(2*rt,d); else if(tree[2*rt].rsum+tree[2*rt+1].lsum>=d)return tree[2*rt].r-tree[2*rt].rsum+1; else if(tree[2*rt+1].msum>=d)return query(2*rt+1,d); else return 0; } int main() { int n,m,i,j,k,a,b,c; while(~scanf("%d%d",&n,&m)) { build(1,n,1); for(i=1;i<=m;i++) { scanf("%d",&a); if(a==1) { scanf("%d",&b); k=query(1,b); printf("%d\n",k); if(k) insert(1,k,k+b-1,2); } else if(a==2) { scanf("%d%d",&b,&c); insert(1,b,b+c-1,1); } } } return 0; }

浙公网安备 33010602011771号