POJ 3667&& NYOJ 534 线段树(求满足条件的最左面区间)

来源:互联网 发布:哔哩哔哩有mac版吗 编辑:程序博客网 时间:2024/06/13 06:15

         这道题目是月赛时的一道题,也是POJ上的一道原题,月赛时想了一个多小时,没写出来。昨天又写了一天,总算写出来了。这道题和一般线段树不一样,需要在线段树中增加几个量。

        lsum代表某个节点左面的连续最大区间,rsum代表某个节点右面的连续最大区间,sum代表某个节点的最大连续区间,同样要用到lazy的思想。不同的是,一般线段树只存在向下更新,即根节点向孩子结点更新,对于这道题来说,同样有子节点向父节点更新,因为父节点的lsum,rsum,sum是在两个子节点lsum,rsum,sum的基础上的。也就是说,当更新完子节点后,父节点的lsum,rsum,sum还要再被子节点的lsum,rsum,sum更新一次。

题目:

Hotel
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 6163 Accepted: 2523

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
ac代码:

#include <iostream>#include <cstdio>#include <string.h>using namespace std;const int N=50010;#define max(x,y) ((x)>(y)?(x):(y))struct tree{int left,right,flag;int lsum,rsum,sum;int getmid(){  return (left+right)/2;}void change(int pos);}tt[N*4];void tree::change(int pos){   tt[pos*2].flag=tt[pos*2+1].flag=tt[pos].flag;int x,y;if(tt[pos].flag){  x=y=0;}else{  x=tt[pos*2].right-tt[pos*2].left+1;  y=tt[pos*2+1].right-tt[pos*2+1].left+1;}tt[pos*2].lsum=tt[pos*2].rsum=tt[pos*2].sum=x;tt[pos*2+1].lsum=tt[pos*2+1].rsum=tt[pos*2+1].sum=y;tt[pos].flag=-1;}void built_tree(int lp,int rp,int pos){tt[pos].left=lp;tt[pos].right=rp;tt[pos].flag=0;tt[pos].lsum=tt[pos].rsum=tt[pos].sum=rp-lp+1;if(lp==rp)return;int mid=tt[pos].getmid();built_tree(lp,mid,pos*2);built_tree(mid+1,rp,pos*2+1);}int find(int x,int pos){if(tt[pos].right==tt[pos].left){  return tt[pos].left;}if(tt[pos].flag!=-1){  tt[pos].change(pos);}if(tt[pos*2].sum>=x){  return find(x,pos*2);}else if(tt[pos*2].rsum+tt[pos*2+1].lsum>=x){  return tt[pos*2].right-tt[pos*2].rsum+1;}else if(tt[pos*2+1].sum>=x){  return find(x,pos*2+1);}else{  return 0;}}void update(int lp,int rp,int modl,int pos){if(tt[pos].left==lp&&tt[pos].right==rp){if(modl==1){  tt[pos].lsum=tt[pos].rsum=tt[pos].sum=0;}else{tt[pos].lsum=tt[pos].rsum=tt[pos].sum=tt[pos].right-tt[pos].left+1;}tt[pos].flag=modl;return;}if(tt[pos].flag!=-1){  tt[pos].change(pos);}int mid=tt[pos].getmid();if(rp<=mid)update(lp,rp,modl,pos*2);else if(lp>mid)update(lp,rp,modl,pos*2+1);else{  update(lp,mid,modl,pos*2);  update(mid+1,rp,modl,pos*2+1);}tt[pos].sum=max(max(tt[pos*2].sum,tt[pos*2+1].sum),tt[pos*2].rsum+tt[pos*2+1].lsum);tt[pos].lsum=tt[pos*2].lsum;tt[pos].rsum=tt[pos*2+1].rsum;if(tt[pos*2].lsum==tt[pos*2].right-tt[pos*2].left+1)tt[pos].lsum+=tt[pos*2+1].lsum;if(tt[pos*2+1].rsum==tt[pos*2+1].right-tt[pos*2+1].left+1)tt[pos].rsum+=tt[pos*2].rsum;}int main(){//freopen("1.txt","r",stdin);int n,m;while(~scanf("%d%d",&n,&m)){  built_tree(1,n,1);  int mod,x,y,ans;  while(m--){    scanf("%d",&mod);if(mod==1){  scanf("%d",&x);  ans=0;  ans=find(x,1);  printf("%d\n",ans);  if(ans){    update(ans,ans+x-1,1,1);  }}else{  scanf("%d%d",&x,&y);  update(x,x+y-1,0,1);}  }}return 0;}


原创粉丝点击