Poj 3667 Hotel (segment update and merge)

Source: Internet
Author: User

There are n rooms and M operations. 1 A indicates that an empty room with a continuous length of A is found. If there are multiple solutions, the left-side priority indicates that the room is checked in. 2 B Len clears the Len room whose start point is B, that is, check out.

The CO is used as the delay mark. If it is-1, it indicates that the current interval is in two States. If it is 0, it indicates that no one stays in the interval, if it is 1, it also indicates a state, indicating full customers. Three values are saved in the online segment Tree node, including the maximum number of consecutive idle rooms on the left of the endpoint (lmx) and the right (RMX), and the total number of idle rooms (MX ). When querying, query the consecutive idle rooms on the left of the endpoint, connect the left sub-interval and the left sub-interval with the right sub-interval, and check whether the right part meets the question. Range merge is the same. If the Lmax value of the Left subinterval is equal to the DIS value of the Left subinterval, The lmx of the parent interval must be added in addition to the lmx value of the Left subinterval. The same applies to the right endpoint.

/* After the code style is updated */# include <iostream> # include <cstdio> # include <cstring> using namespace STD; # define LL (x) (x <1) # define RR (x) (x <1 | 1) # define mid (a, B) (a + (B-a)> 1 )) const int n = 50005; struct node {int LFT, rht; int flag, MX, lmx, RMX; int Len () {return rht-LFT + 1;} int mid () {return mid (LFT, rht);} void Init () {MX = lmx = RMX = Len ();} void fun (int tmp) {If (TMP = 1) MX = lmx = RMX = 0; else MX = lmx = RMX = Len (); flag = TMP; // forgot to add the latency tag}; int N, M; struct segtree {node tree [N * 4]; void down (int ind) {If (tree [ind]. flag) {tree [LL (IND)]. fun (tree [ind]. flag); tree [RR (IND)]. fun (tree [ind]. flag); tree [ind]. flag = 0 ;}} void up (int ind) {tree [ind]. lmx = tree [LL (IND)]. lmx; tree [ind]. RMX = tree [RR (IND)]. RMX; tree [ind]. MX = max (tree [LL (IND)]. MX, tree [RR (IND)]. MX); If (tree [LL (IND)]. lmx = tree [LL (IND)]. len () tree [ind]. lmx + = tree [RR (IND)]. lmx; If (tree [RR (IND)]. RMX = tree [RR (IND)]. Len () tree [ind]. RMX + = tree [LL (IND)]. RMX; tree [ind]. MX = max (tree [ind]. MX, max (tree [ind]. lmx, tree [ind]. RMX); tree [ind]. MX = max (tree [ind]. MX, tree [LL (IND)]. RMX + tree [RR (IND)]. lmx);} void build (int lft, int rht, int IND) {tree [ind]. LFT = LFT; tree [ind]. rht = rht; tree [ind]. flag = 0; tree [ind]. init (); If (LFT! = RHT) {int mid = tree [ind]. mid (); Build (LFT, mid, LL (IND); Build (Mid + 1, rht, RR (IND) ;}} void updata (INT St, int ed, int ind, int valu) {int LFT = tree [ind]. LFT, rht = tree [ind]. rht; If (ST <= LFT & rht <= ed) tree [ind]. fun (valu); else {down (IND); int mid = tree [ind]. mid (); If (ST <= mid) updata (St, Ed, LL (IND), valu); If (Ed> mid) updata (St, Ed, rr (IND), valu); up (IND) ;}} int query (INT valu, int IND) {If (tree [ind]. LFT = tree [ind]. RHT) return t REE [ind]. LFT; else {down (IND); // remember when querying .... int Pos; If (tree [LL (IND)]. MX> = valu) Pos = query (valu, LL (IND); else if (tree [LL (IND)]. RMX + tree [RR (IND)]. lmx> = valu) Pos = tree [LL (IND)]. rht-tree [LL (IND)]. RMX + 1; else Pos = query (valu, RR (IND); up (IND); // remember when querying .... return POS ;}} seg; int main () {While (scanf ("% d", & N, & M )! = EOF) {seg. build (1, n, 1); While (M --) {int A, B, C, Pos = 0; scanf ("% d", & ); if (A = 1) {scanf ("% d", & B); If (SEG. tree [1]. MX> = B) {pos = seg. query (B, 1); seg. updata (Pos, POS + b-1,);} printf ("% d \ n", POS);} else {scanf ("% d", & B, & C); seg. updata (B, B + C-1, 1,-1) ;}} return 0 ;}

/* Before code style update */# include <iostream> # include <cstdio> using namespace STD; const int n = 50005; struct node {int left, right, CO; int Lmax, rmax, mMax; int mid () {return left + (right-left)/2;} int DIS () {return right-left + 1 ;} void change (int A) {Co = A; If (CO = 0) Lmax = rmax = mMax = DIS (); else Lmax = rmax = mMax = 0 ;}}; void unin (node & A, node & B, node & C) {. lmax = B. lmax;. rmax = C. rmax;. mMax = max (B. mMax, C. mMax), B. rmax + C. LMA X); If (B. lmax = B. DIS (). lmax + = C. lmax; If (C. rmax = C. DIS (). rmax + = B. rmax;} struct segtree {node tree [N * 4]; void build (INT left, int right, int R) {tree [R]. left = left; tree [R]. right = right; tree [R]. lmax = tree [R]. rmax = tree [R]. mMax = tree [R]. DIS (); tree [R]. CO =-1; if (left <right) {int mid = tree [R]. mid (); Build (left, mid, R * 2); Build (Mid + 1, right, R * 2 + 1) ;}} void updata (INT be, int end, int R, int Co) {If (be <= tre E [R]. left & tree [R]. right <= END) {tree [R]. change (CO);} else {If (tree [R]. CO! =-1) {tree [R * 2]. change (tree [R]. CO); tree [R * 2 + 1]. change (tree [R]. CO); tree [R]. CO =-1;} int mid = tree [R]. mid (); If (be <= mid) updata (BE, end, R * 2, CO); If (end> mid) updata (BE, end, R * 2 + 1, CO); unin (tree [R], tree [R * 2], tree [R * 2 + 1]);} int query (INT Len, int R) {If (tree [R]. lmax> = Len) return tree [R]. left; else if (tree [R * 2]. mMax> = Len) return query (Len, R * 2); else if (tree [R * 2]. rmax + tree [R * 2 + 1]. lmax> = Len) return tree [R * 2]. right-tree [R * 2]. rmax + 1; else return query (Len, R * 2 + 1) ;}} seg; int main () {int n, m; scanf ("% d ", & N, & M); seg. build (1, n, 1); For (INT I = 0; I <m; I ++) {int A, B, C; scanf ("% d ", & A); if (a = 1) {scanf ("% d", & B); If (SEG. tree [1]. mMax> = B) {int Pos = seg. query (B, 1); printf ("% d \ n", POS); seg. updata (Pos, POS + b-1, 1, 1);} else puts ("0");} else {scanf ("% d", & B, & C ); SEG. updata (B, B + C-1, 1, 0) ;}} 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.