HDU 2871 memory control

Source: Internet
Author: User

Hdu_2871

Hu Hao's blog says that this can be used as a warm-up question for splay, and the result is 270 rows.CodeBut it ended up in TLE and had to be written into a line segment tree.

The reset operation is the simplest. You can initialize the root directly and add a delay mark.

We can use LC [] (left contiguous) to indicate the number of consecutive vacancies in a range starting from the left. RC [] (right contiguous) the number of consecutive vacancies in a range starting from the right. MC (max contiguous) indicates the longest consecutive vacancies in this range. With these three arrays, we can easily implement the new operation.

For the free operation, it is very easy to know whether this point is empty. But to obtain the Left and Right endpoints of memory unit, we can add two additional arrays Lu [] (left Unit), Ru [Right Unit] indicates the Left and Right endpoints. In this way, the free operation query function is implemented, and the free deletion function can be used to directly dye the segment and dye it to 0.

For the get operation, we add an additional array tree [] to indicate the total number of memory units in the current range, however, we only assign the tree [] value of the unit on the left side of each memory units to 1, so that the count function is realized and it is convenient to search, the last position found is the left endpoint of the memory units.

# Include <stdio. h>
# Include < String . H>
# Define Maxd 50010
Int N, m, tree [ 4 * Maxd], [ 4 * Maxd], MC [ 4 * Maxd], LC [ 4 * Maxd], RC [ 4 * Maxd], Lu [4 * Maxd], Ru [ 4 * Maxd];
Int Getmax ( Int X, Int Y)
{
Return X> Y? X: Y;
}
Void Update ( Int Cur, Int X, Int Y)
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
MC [cur] = getmax (MC [ls], MC [RS]);
MC [cur] = getmax (MC [cur], RC [ls] + Lc [RS]);
LC [cur] = Lc [ls] + (LC [ls] = mid-x + 1 ? LC [RS]: 0 );
RC [cur] = RC [RS] + (RC [RS] = Y-mid? RC [ls]: 0 );
Tree [cur] = tree [ls] + tree [RS];
}
Void Pushdown ( Int Cur, Int X, Int Y)
{
If (To [cur]! =- 1 )
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
To [ls] = to [RS] = to [cur];
Tree [ls] = tree [RS] = 0 ;
MC [ls] = Lc [ls] = RC [ls] = (to [cur]? 0 : Mid-x + 1 );
MC [RS] = Lc [RS] = RC [RS] = (to [cur]? 0 : Y-mid );
If (To [cur])
Lu [ls] = LU [RS] = LU [cur], Ru [ls] = Ru [RS] = Ru [cur];
To [cur] =- 1 ;
}
}
Void Build ( Int Cur, Int X, Int Y)
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
To [cur] =- 1 , Tree [cur] = 0 ;
MC [cur] = Lc [cur] = RC [cur] = Y-x + 1 ;
If (X = y)
Return ;
Build (LS, X, mid );
Build (RS, Mid + 1 , Y );
}
Int Searchlen ( Int Cur, Int X, Int Y, Int Len)
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
If (X = y)
Return X;
Pushdown (cur, x, y );
If (MC [ls]> = Len)
Return Searchlen (LS, X, mid, Len );
Else If (RC [ls] + Lc [RS]> = Len)
Return Mid-RC [ls] + 1 ;
Else
Return Searchlen (RS, Mid + 1 , Y, Len );
}
Void Color ( Int Cur, Int X, Int Y, Int S, Int T,Int C)
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
If (X> = S & Y <= T)
{
To [cur] = C;
Tree [cur] = 0 ;
MC [cur] = Lc [cur] = RC [cur] = (C? 0 : Y-x +1 );
If (C)
{
Lu [cur] = s, Ru [cur] = T;
If (X = s)
{
If (X! = Y)
{
Pushdown (cur, x, y );
Color (LS, X, mid, S, T, C );
Update (cur, x, y );
}
Else
Tree [cur] =1 ;
}
}
Return ;
}
Pushdown (cur, x, y );
If (Mid> = s)
Color (LS, X, mid, S, T, C );
If (Mid + 1 <= T)
Color (RS, Mid + 1 , Y, S, T, C );
Update (cur, x, y );
}
Int Search ( Int Cur, Int X, Int Y, Int Goal, Int & L, Int & R)
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
If (X = y)
{
If (MC [cur] = 0 )
{
L = LU [cur], r = Ru [cur];
Return 1 ;
}
Return 0 ;
}
Pushdown (cur, x, y );
If (Goal <= mid)
Return Search (LS, X, mid, goal, L, R );
Else
Return Search (RS, Mid + 1 , Y, goal, L, R );
}
Void Reset ()
{
To [ 1 ] = Tree [ 1 ] = 0 ;
MC [ 1 ] = Lc [ 1 ] = RC [ 1 ] = N;
Printf (" Reset now \ n " );
}
Void Newunit ( Int Len)
{
Int X;
X = searchlen ( 1 , 1 , N, Len );
Printf ( " New at % d \ n " , X );
Color (1 , 1 , N, x, x + len- 1 , 1 );
}
Void Free ( Int X)
{
Int L, R;
If (Search ( 1 , 1 , N, x, L, R ))
{
Printf (" Free from % d to % d \ n " , L, R );
Color ( 1 , 1 , N, l, R, 0 );
}
Else
Printf ( " Reject free \ n " );
}
Int Getunit ( Int Cur, Int X, Int Y, Int K)
{
Int Mid = (x + y )/ 2 , Ls = 2 * Cur, RS = 2 * Cur + 1 ;
If (X = y)
Return Lu [cur];
Pushdown (cur, x, y );
If (K <= tree [ls])
Return Getunit (LS, X, mid, k );
Else
Return Getunit (RS, Mid + 1 , Y, K-tree [ls]);
}
Void Solve ()
{
Int I, J, K, X;
Char B [ 10 ];
For (I = 0 ; I <m; I ++)
{
Scanf ( " % S " , B );
If (B [ 0 ] = ' R ' )
Reset ();
Else If (B [ 0 ] = ' N ' )
{
Scanf ( " % D " , & X );
If (MC [ 1 ] <X)
Printf ( " Reject new \ n " );
Else
Newunit (X );
}
Else If (B [ 0 ] = ' F ' )
{
Scanf ( " % D " , & X );
Free (X );
}
Else
{
Scanf ( " % D " , & K );
If (Tree [ 1 ] <K)
Printf ( " Reject get \ n " );
Else
Printf ( " Get at % d \ n " , Getunit ( 1 , 1 , N, k ));
}
}
}
Int Main ()
{
While (Scanf ( " % D " , & N, & M) = 2 )
{
Build ( 1 , 1 , N );
Solve ();
Printf (" \ N " );
}
Return 0 ;
}


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.