HDU 2871 vector + line segment Tree Memory Control

Source: Internet
Author: User

Question:
Now there are 1 to n (n <= 50000) continuous memory blocks, and then four operations are given:
1. Reset releases all memory blocks and outputs "Reset now ".
2. New x finds a space with X consecutive memories and occupies it. If there are multiple options, select the start number
The minimum value is "New at a", and "A" indicates the Starting number; otherwise, "reject new" is output ".
3. Free x releases a memory block occupying unit X and outputs "free from A to B". A to B indicates
Memory block occupied; otherwise, "reject free" is output ".
4. Get X finds the X block of continuous memory. If yes, the output "get at a" indicates the Starting number unit.

If the current memory block is less than X, "reject get" is output ".

Solution:

Perform several operations with the line tree and vector. It won't hurt vector. I remember that the cf85d in the online competition had found a vector. Well, learn.

The lsum, rsum, msum, and cover of the Line Segment Tree nodes are the common meanings.

Question preparation process:

It seems that this question has been done for a long time. The network competition and other things are mixed in the middle, and I slept very late yesterday, and today I am useless.

Sometimes it's strange that you haven't found anything for a long time. For example, errors and things. However, I try again and it will be over.

Build a stone wall and do not fear.

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <queue> # include <set> # include <vector> # include <algorithm> # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define maxn 50010 # define ls RT <1 # define Rs RT <1 | 1 # define havem int M = (L + r)> 1 using namespace STD; int A, B; int lsum [maxn <2], rsum [maxn <2], msum [maxn <2], cover [maxn <2], X, n, m; char op [10]; struct node {int St, end ;}; vector <node> VEC; void build (int l, int R, int RT) {rsum [RT] = lsum [RT] = msum [RT] = r-L + 1; cover [RT] = 0; If (L = r) return; havem; build (lson); Build (rson);} void push_dn (int rt, int m) {If (cover [RT]! =-1) {cover [ls] = cover [RS] = cover [RT]; If (cover [RT] = 0) {lsum [ls] = rsum [ls] = msum [ls] = m-(M> 1 ); lsum [RS] = rsum [RS] = msum [RS] = (M> 1 );} else {lsum [ls] = rsum [ls] = msum [ls] = 0; lsum [RS] = rsum [RS] = msum [RS] = 0 ;} cover [RT] =-1 ;}} int query (INT Len, int L, int R, int RT) {If (L = r) {return l ;} push_dn (RT, R-l + 1); havem; If (msum [ls]> = Len) return query (Len, lson); If (rsum [L S] + lsum [RS]> = Len) Return (m-rsum [ls] + 1); // If (msum [RS]> = Len) do not return query (Len, rson); // return 0;} void push_up (int rt, int m) {lsum [RT] = lsum [ls]; rsum [RT] = rsum [RS]; msum [RT] = max (msum [ls], msum [RS]), rsum [ls] + lsum [RS]); If (lsum [ls] = (m-(M> 1 ))) lsum [RT] + = lsum [RS]; If (rsum [RS] = (M> 1) rsum [RT] + = rsum [ls]; if (cover [ls] = cover [RS]) cover [RT] = cover [ls];} Vo Id Update (int l, int R, int CC, int L, int R, int RT) {If (L = L & R = r) {cover [RT] = cc; lsum [RT] = rsum [RT] = msum [RT] = (Cc = 0? R-l + 1:0); return;} push_dn (RT, R-l + 1); havem; If (r <= m) Update (L, R, CC, lson); else if (L> m) Update (L, R, CC, rson); else Update (L, M, CC, lson), update (m + 1, r, CC, rson); push_up (RT, R-l + 1);} bool CMP (const node & B1, const node & B2) {return b1.st <b2.st ;} int main () {char op [10]; while (scanf ("% d", & N, & M )! = EOF) {build (1, n, 1); Vec. clear (); While (M --) {scanf ("% s", OP); If (OP [0] = 'R ') {printf ("Reset now \ n"); Update (1, n, 0, 1); Vec. clear ();} else if (OP [0] = 'n') {scanf ("% d", & A); If (msum [1] <) printf ("reject new \ n"); else {B = query (A, 1, n, 1); printf ("New at % d \ n", B ); update (B, B + A-1,1, 1, n, 1); node TMP; TMP. st = B; TMP. end = B + A-1; if (VEC. size () = 0) Vec. push_back (TMP); else {vector <node>: iterator it; it = upper_bound (VEC. begin (), VEC. end (), TMP, CMP); Vec. insert (it, TMP) ;}} else if (OP [0] = 'F') {scanf ("% d", & A); node TMP; TMP. st = A; TMP. end = A; vector <node >:: iterator it; it = upper_bound (VEC. begin (), VEC. end (), TMP, CMP); If (IT = Vec. begin () | (-- It)-> end <A) printf ("reject free \ n "); else {printf ("free from % d to % d \ n", It-> St, IT-> end); Update (IT-> St, IT-> end, 0, 1, n, 1); Vec. erase (IT) ;}} else {scanf ("% d", & A); If (VEC. size () <A) printf ("reject get \ n"); else printf ("Get at % d \ n", VEC [A-1]. st) ;}} 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.