Poj 3667 splay interval merge exercise

Source: Internet
Author: User
Hotel
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:12446   Accepted:5363

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 hasN(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 sizeDi(1 ≤Di≤ N) and approach the front desk to check in. Each groupIRequests a setDiContiguous rooms from canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbersR..R+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. canmuu always chooses the valueRTo be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. CheckoutIHas the parametersXIAndDiWhich specify the vacating of roomsXI..XI+Di-1 (1 ≤XIN-Di+ 1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist canmuu by processingM(1 ≤M<50,000) checkin/Checkout requests. The hotel is initially unoccupied.

Input

* Line 1: two space-separated integers:NAndM
* Lines 2 ..M+ 1: LineI+ 1 contains request expressed as one of two possible formats: (a) two space separated integers representing a check-in request: 1 andDi(B) Three space-separated integers representing a check-out: 2,XI, AndDi

Output

* Lines 1...: For each check-in request, output a single line with a single integerR, 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


Code:

/* ***********************************************Author :rabbitCreated Time :2014/11/1 12:57:54File Name :4.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=200200;struct Node;Node *null;struct Node{Node *ch[2],*fa;int size;int lsum,rsum,msum,col,val;Node(){ch[0]=ch[1]=fa=null;col=0;}inline void setc(Node *p,int d){ch[d]=p;p->fa=this;}inline bool d(){return fa->ch[1]==this;}void clear(){size=1;ch[0]=ch[1]=fa=null;lsum=rsum=msum=val=1;col=-1;}inline void push_up(){if(this==null)return;size=ch[0]->size+ch[1]->size+1;lsum=ch[0]->lsum;rsum=ch[1]->rsum;msum=max(ch[0]->msum,ch[1]->msum);if(val){msum=max(msum,ch[0]->rsum+ch[1]->lsum+1);if(lsum==ch[0]->size)lsum+=ch[1]->lsum+1;if(rsum==ch[1]->size)rsum+=ch[0]->rsum+1;}}void update(int c){if(this==null)return;lsum=rsum=msum=c*size;val=col=c;}inline void push_down(){if(this==null)return;if(col!=-1){ch[0]->update(col);ch[1]->update(col);col=-1;}}};inline void rotate(Node *x){Node *f=x->fa,*ff=x->fa->fa;f->push_down();x->push_down();int c=x->d(),cc=f->d();f->setc(x->ch[!c],c);x->setc(f,!c);if(ff->ch[cc]==f)ff->setc(x,cc);else x->fa=ff;f->push_up();}inline void splay(Node *&root,Node *x,Node *goal){while(x->fa!=goal){if(x->fa->fa==goal)rotate(x);else{x->fa->fa->push_down();x->fa->push_down();x->push_down();bool f=x->fa->d();x->d()==f?rotate(x->fa):rotate(x);rotate(x);}}x->push_up();if(goal==null)root=x;}Node *get_kth(Node *r,int k){Node *x=r;x->push_down();while(x->ch[0]->size+1!=k){if(k<x->ch[0]->size+1)x=x->ch[0];else{k-=x->ch[0]->size+1;x=x->ch[1];}x->push_down();}return x;}Node pool[maxn],*tail,*node[maxn],*root;void build(Node *&x,int l,int r,Node *fa){if(l>r)return;int mid=(l+r)/2;x=tail++;x->clear();x->fa=fa;node[mid]=x;build(x->ch[0],l,mid-1,x);build(x->ch[1],mid+1,r,x);x->push_up();}void init(int n){tail=pool;null=tail++;null->fa=null->ch[0]=null->ch[1]=null;null->size=0;null->val=null->lsum=null->rsum=null->msum=0;null->col=-1;Node *p=tail++;p->val=p->msum=p->rsum=p->lsum=0;p->col=-1;p->size=1;p->ch[0]=p->ch[1]=p->fa=null;root=p;p=tail++;p->val=p->msum=p->rsum=p->lsum=0;p->col=-1;p->size=1;p->ch[0]=p->ch[1]=p->fa=null;root->setc(p,1);build(root->ch[1]->ch[0],1,n,root->ch[1]);root->ch[1]->push_up();root->push_up();}void update(int l,int r,int c){splay(root,get_kth(root,l),null);splay(root,get_kth(root,r+2),root);root->ch[1]->ch[0]->update(c);root->ch[1]->push_up();root->push_up();}int query(int l,int r){splay(root,get_kth(root,l),null);splay(root,get_kth(root,r+2),root);return root->ch[1]->ch[0]->msum;}int find(Node *x,int k,int pos){x->push_down();//cout<<x->msum<<endl;if(x->ch[0]->msum>=k)return find(x->ch[0],k,pos);pos+=x->ch[0]->size;if(x->val&&x->ch[0]->rsum+x->ch[1]->lsum+1>=k)return pos-x->ch[0]->rsum;return find(x->ch[1],k,pos+1);}int main(){     //freopen("data.in","r",stdin);     //freopen("data.out","w",stdout);     int n,m; while(~scanf("%d%d",&n,&m)){ init(n); //cout<<root->msum<<" "<<root->ch[0]->msum<<" "<<root->ch[1]->msum<<endl; while(m--){ int op,x,y; scanf("%d",&op); if(op==1){ scanf("%d",&x); if(root->msum<x){ puts("0");continue; } int k=find(root,x,0); printf("%d\n",k); update(k,k+x-1,0); } else{ scanf("%d%d",&x,&y); update(x,x+y-1,1); } } }     return 0;}/*int main(){int n,m;while(cin>>n>>m) {init(n);while(m--){int op,x,y,z;cin>>op;if(op==1){cin>>x>>y>>z;update(x,y,z);}else{cin>>x>>y;cout<<query(x,y)<<endl;}}}return 0;}*/


Poj 3667 splay interval merge exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.