Hdu 2871 Memory Control (segment update, interval merge)

Source: Internet
Author: User

This problem in the online search of the problem, others said is more than HDU hotel also a subject of metamorphosis.

Since it is a problem, it synthesizes almost all the operations of the line tree.

The test instructions of this problem is:

Here are a few things to do:

1: The first is the reset operation, which means that all the memory space is emptied.

2:new x: Represents the allocation of an X memory space, if there is memory space, then output that memory space the leftmost endpoint. Otherwise, the output Reject New

3:free x: Represents the release of memory space containing the number x. If this memory space has been freed, then output reject free

4:get x: Represents the start position of the X memory interval returned

1) Reset operation: We can update all the interval first, update and hotel, is the representative of the interval to zero, and also the vector empty. where vector g This array is to save each interval starting at the end position ( where the start and end of the interval is saved with the STL vector, because the vector is easier to empty a certain interval

2) New operation: First we have to determine whether the length of the x to be allocated is less than or equal to tree[1].ms (the existing allocation length of the total interval, if this is not satisfied directly is negative.

As for the search starting point, then the query, this and the hotel is the same. Be careful not to forget that the Pushdown function is in the process of operation.

int query (int v,int len) {    if (TREE[V].L==TREE[V].R) return TREE[V].L;    Pushdown (v);    int mid= (TREE[V].L+TREE[V].R) >>1;    int temp=v<<1;    if (Tree[temp].ms>=len) return query (Temp,len);    else if (Tree[temp].rs+tree[temp+1].ls>=len)  return tree[temp].r-tree[temp].rs+1;    else return query (Temp+1,len);}
The end point, that is not better judgment = =, because we already know the beginning and the length of the interval, and then the end must also be able to find out.

Now there is one more critical step: to tag the interval ID so that we can find it in the Get function.

A fantastic way to do this is to find out what the ID of the binary is.

The principle of dichotomy is:

We have two points on the starting point and then we can get its subscript. (Note that the subscript here is starting from 0, so we have to subtract 1 when we pre-process the Get)

int bin_search (int k) {    int l=0,r=g.size ()-1;    while (r>=l) {        int mid= (l+r) >>1;        if (g[mid].s<=k) l=mid+1;  //??        else r=mid-1;    }    return r;}
Finally, don't forget to press the start and end points into the ID range, and update the interval between start and end.

Here with the operation of the STL vector, it is too magical, the next time to fill a vector of STL, the text of the system to learn a bit.

<span style= "White-space:pre" ></span>g.insert (G.begin () +id,buf);                Update (buf.s,buf.e,1,1);
3) free operation, when the ID is-1 or G[ID].E is less than x, then it is not feasible

Otherwise the output starts with the end point and then Update (g[id].s,g[id].e,1,0), which represents emptying them.

Do not forget to empty the vector again.

4) Get operation, should be considered as the simplest one to put.

The g.size () here means that there are now several intervals (if the original interval is free, then the interval in the back will be automatically updated.

Note that since the interval we recorded is starting from 0, and the topic is starting from 1, the output should be output g[x-1].s.

The other functions are the same as the hotel.

Attached code:

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <    Map> #include <vector>using namespace std; #define MAXN 55555struct node{int l,r; int Ls,rs,ms,flag;} tree[maxn*4];struct line{int s,e;};    vector<line> g;void pushup (int v) {int temp=v<<1;    Tree[v].ls=tree[temp].ls;    tree[v].rs=tree[temp+1].rs;    if (tree[v].ls==tree[temp].r-tree[temp].l+1) tree[v].ls+=tree[temp+1].ls;    if (tree[v].rs==tree[temp+1].r-tree[temp+1].l+1) tree[v].rs+=tree[temp].rs;    Tree[v].ms=max (tree[temp].ms,tree[temp+1].ms); Tree[v].ms=max (TREE[V].MS,TREE[TEMP].RS+TREE[TEMP+1].LS);}    void pushdown (int v) {int temp=v<<1;            if (tree[v].flag!=-1) {if (Tree[v].flag) {tree[temp].flag=tree[temp+1].flag=tree[v].flag;            tree[temp].ls=tree[temp].rs=tree[temp].ms=0;        tree[temp+1].ls=tree[temp+1].rs=tree[temp+1].ms=0; } if (tree[v].flag==0) {tree[temp].flag=tree[temp+1]. Flag=tree[v].flag;            tree[temp].ls=tree[temp].rs=tree[temp].ms=tree[temp].r-tree[temp].l+1;        tree[temp+1].ls=tree[temp+1].rs=tree[temp+1].ms=tree[temp+1].r-tree[temp+1].l+1;    } tree[v].flag=-1;    }}void Build (int l,int R,int v) {tree[v].l=l;    Tree[v].r=r;    Tree[v].flag=-1;    tree[v].ls=tree[v].rs=tree[v].ms=r-l+1;    if (l==r) return;    int mid= (TREE[V].L+TREE[V].R) >>1;    int temp=v<<1;    Build (L,mid,temp); Build (mid+1,r,temp+1);} void update (int l,int r,int V,int cnt) {if (l<=tree[v].l&&tree[v].r<=r) {if (CNT) tree[v].ls=tree        [V].rs=tree[v].ms=0,tree[v].flag=1;        else tree[v].ls=tree[v].rs=tree[v].ms=tree[v].r-tree[v].l+1,tree[v].flag=0;    Return    } if (tree[v].flag!=-1) pushdown (v);    int temp=v<<1;    int mid= (TREE[V].L+TREE[V].R) >>1;    if (r<=mid) update (L,R,TEMP,CNT);    else if (l>mid) update (L,R,TEMP+1,CNT);        else{Update (L,MID,TEMP,CNT); Update (mid+1,R,TEMP+1,CNT); } pushup (v);}    int query (int v,int len) {if (TREE[V].L==TREE[V].R) return TREE[V].L;    Pushdown (v);    int mid= (TREE[V].L+TREE[V].R) >>1;    int temp=v<<1;    if (Tree[temp].ms>=len) return query (Temp,len);    else if (Tree[temp].rs+tree[temp+1].ls>=len) return tree[temp].r-tree[temp].rs+1; else return query (Temp+1,len);}    int bin_search (int k) {int l=0,r=g.size ()-1;        while (r>=l) {int mid= (L+R) >>1;  if (g[mid].s<=k) l=mid+1;        //??    else r=mid-1; } return R;}    int main () {int n,m;        while (scanf ("%d%d", &n,&m) ==2) {g.clear ();        Char ss[11]={0};        int x;        Build (1,n,1);            while (m--) {scanf ("%s", SS);                if (!STRCMP (ss, "Reset")) {g.clear ();                Update (1,n,1,0);            printf ("Reset now\n");                } else if (!STRCMP (ss, "New")) {scanf ("%d", &x);             if (tree[1].ms<x) {       printf ("Reject new\n");                Continue                } line buf;                Buf.s=query (1,X);                Buf.e=buf.s+x-1;                int Id=bin_search (BUF.S) +1;                printf ("New at%d\n", BUF.S);                G.insert (G.begin () +id,buf);            Update (buf.s,buf.e,1,1);                } else if (!STRCMP (SS, "free")) {scanf ("%d", &x);                int Id=bin_search (x); if (id==-1| |                g[id].e<x) {printf ("Reject free\n");                    } else{printf ("Free from%d to%d\n", G[ID].S,G[ID].E);                    Update (g[id].s,g[id].e,1,0);                G.erase (G.begin () +id,g.begin () +id+1);                }} else if (!STRCMP (ss, "Get")) {scanf ("%d", &x);                if (X<=g.size () &&x>0) {printf ("Get at%d\n", G[X-1].S); } elseprintf ("Reject get\n");    }} puts (""); }}/*6 10New 2New 5New 2New 2Free 3Get 1Get 2Get 3Free 3reset*/


This line of tree problem is really good idea, but it took me a long time to read ...

Hope to absorb good experience, come on!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 2871 Memory Control (segment update, interval merge)

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.