HDU 2871 Memory Control (line segment tree, interval merging), hdu2871

Source: Internet
Author: User

HDU 2871 Memory Control (line segment tree, interval merging), hdu2871

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2871

Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4418 Accepted Submission (s): 1056


Problem DescriptionMemory units are numbered from 1 up to N.
A sequence of memory units is called a memory block.
The memory control system we consider now has four kinds of operations:
1. Reset all memory units free.
2. New x Allocate a memory block consisted of x continuous free memory units with the least start number
3. Free x Release the memory block which provided des unit x
4. Get x Return the start number of the xth memory block (Note that we count the memory blocks allocated from left to right)
Where 1 <= x <= N. You are request to find out the output for M operations.
 
InputInput contains multiple cases.
Each test case starts with two integer N, M (1 <= N, M <= 50000), indicating that there are N units of memory and M operations.
Follow by M lines, each line contains one operation as describe above.
 
OutputFor each "Reset" operation, output "Reset Now ".
For each "New" operation, if it's possible to allocate a memory block,
Output "New at A", where Ais the least start number, otherwise output "Reject New ".
For each "Free" operation, if it's possible to find a memory block occupy unit x,
Output "Free from A to B", where A and B refer to the start and end number of the memory block, otherwise output "Reject Free ".
For each "Get" operation, if it's possible to find the xth memory blocks,
Output "Get at A", where A is its start number, otherwise output "Reject Get ".
Output one blank line after each test case.
 
Sample Input
6 10New 2New 5New 2New 2Free 3Get 1Get 2Get 3Free 3Reset
 
Sample Output
New at 1Reject NewNew at 3New at 5 Free from 3 to 4Get at 1Get at 5Reject GetReject FreeReset Now
 
Source2009 Multi-University Training Contest 7-Host by FZU

Question:

Reset: clear memory

New x: memory with x length applied

Free x: releases the memory block of x.

Get x: the starting point of the memory block where x is located.

Analysis:

The New operation uses the line segment tree to locate the leftmost position that can hold x, and maintains the maximum continuous length of 0 from the left/right of the node and the maximum continuous length of 0 in the corresponding interval.

Save the existing memory block and use binary search/update. Note that the Reset should be cleared.


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define LL long # define itn int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define eps 1e-10 # define maxm # define maxn 200007us Ing namespace std; int setv [maxn <2]; int lsum0 [maxn <2], rsum0 [maxn <2], msum0 [maxn <2]; struct Memory {int l, r ;}; vector <Memory> v; inline void pushup (int k, int l, int r) {int lc = k * 2 + 1, rc = k * 2 + 2, m = l + r> 1; if (lsum0 [lc] = m-l) lsum0 [k] = lsum0 [lc] + lsum0 [rc]; else lsum0 [k] = lsum0 [lc]; if (rsum0 [rc] = r-m) rsum0 [k] = rsum0 [rc] + rsum0 [lc]; else rsum0 [k] = rsum0 [rc]; msum0 [k] = max (rsum0 [lc] + lsum0 [rc], max (msum0 [lc], msum0 [Rc]);} inline void pushdown (int k, int l, int r) {if (setv [k]! =-1) {int lc = k * 2 + 1, rc = k * 2 + 2, m = l + r> 1; setv [lc] = setv [rc] = setv [k]; lsum0 [lc] = rsum0 [lc] = msum0 [lc] = setv [k]? 0: m-l; lsum0 [rc] = rsum0 [rc] = msum0 [rc] = setv [k]? 0: r-m; setv [k] =-1 ;}} void update (int a, int B, int v, int k, int l, int r) {if (B <= l | r <= a) return; if (a <= l & r <= B) {setv [k] = v; lsum0 [k] = rsum0 [k] = msum0 [k] = v? 0: r-l;} else {pushdown (k, l, r); update (a, B, v, k * 2 + 1, l, l + r> 1); update (a, B, v, k * 2 + 2, l + r> 1, r); pushup (k, l, r) ;}} int query (int w, int k, int l, int r) {int lc = k * 2 + 1, rc = k * 2 + 2, m = l + r> 1; if (r-l = 1) return l; if (r-l! = 1) pushdown (k, l, r); if (msum0 [lc]> = w) return query (w, lc, l, m ); if (rsum0 [lc] + lsum0 [rc]> = w) return m-rsum0 [lc]; return query (w, rc, m, r);} int bin_search (int k) {int l = 0, r = v. size ()-1, ans =-1; while (l <= r) {itn mid = l + r> 1; if (v [mid]. l <= k) {l = mid + 1; ans = mid;} else {r = mid-1 ;}} return ans ;} int main () {# ifndef ONLINE_JUDGE freopen ("/home/fcbruce/documentation/code/t", "r", stdin); # endif // ONLINE_JUDGE int n, m, w, A, B, p; char op [10]; while (~ Scanf ("% d", & n, & m) {v. clear (); update (0, n, 0, 0, n); while (m --) {scanf ("% s", op ); if (op [0] = 'R') {v. clear (); puts ("Reset Now"); update (0, n, 0, 0, n); continue;} if (op [0] = 'n ') {scanf ("% d", & w); if (w <= msum0 [0]) {a = query (w, 0, 0, n); B = a + w; update (a, B, 1, 0, 0, n); printf ("New at % d \ n", a + 1); Memory temp; temp. l = a + 1; temp. r = B; int id = bin_search (a + 1) + 1; v. insert (v. begin () + id, temp);} else puts ("Reject New"); continue;} if (op [0] = 'F ') {scanf ("% d", & p); int id = bin_search (p); if (id =-1 | v [id]. r <p) puts ("Reject Free"); else {printf ("Free from % d to % d \ n", v [id]. l, v [id]. r); update (v [id]. l-1, v [id]. r, 0, 0, 0, n); v. erase (v. begin () + id, v. begin () + id + 1);} continue;} if (op [0] = 'G') {scanf ("% d", & w ); if (w <= v. size () printf ("Get at % d \ n", v [W-1]. l); else puts ("Reject Get") ;}} puts ("");} 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.