Hotel-poj 3667 (for continuous sub-range)

Source: Internet
Author: User

Test Instructions: There are two types of operations1, from left to right to find an interval is a continuous sequence of D, and then overwrite, return the first number of intervals, if there is no output 02, releasing the interval analysis of continuous d from L: It is to find a continuous interval of D from left to right, you can use three values to manipulate Lsum,rsum,sum, is the largest continuous value from left to right, from right to left, the maximum continuous interval of the entire interval, and (I-tunnel Warfare-hdu 1540) somewhat similar, but the update time is not the same, this sum simply save the maximum continuous interval ************** #include <algorithm>
#include <stdio.h>
usingnamespaceStd

#defineLson r<<1
#defineRson r<<1|1

Const intMAXN = 1e5+5;

structSegmenttree
{///Represents the largest continuous interval on the left, the largest continuous interval on the right, and the maximum continuous interval
intL, R, lsum, rsum, sum;
intOp///op equals 0 when not operating, equals 1 when room occupied, 2 released
intLen () {returnr-l+1;}
intMid () {return(l+r) >>1;}
}a[maxn<<2];

voidPushdown (intR
{
if(A[r]. L! = A[r]. R && A[r].op)
{
A[lson].lsum = A[lson].rsum = A[lson].sum = (A[r].op = =1?0: A[lson].len ());
A[rson].lsum = A[rson].rsum = A[rson].sum = (A[r].op = =1?0: A[rson].len ());

A[lson].op = A[rson].op = A[r].op;
A[r].op =0;
}
}
voidPushup (intR
{///Be sure to take care to update the upper layer before merging operations
A[r].lsum = a[lson].lsum, a[r].rsum = a[rson].rsum;

if(A[lson].lsum = = A[lson].len ())
A[r].lsum + = A[rson].lsum;
if(A[rson].rsum = = A[rson].len ())
A[r].rsum + = A[lson].rsum;

A[r].sum = Max (A[lson].sum, Max (A[rson].sum, a[lson].rsum+a[rson].lsum));
}
voidBuild (intRintLintR
{
A[R]. L = L, A[r]. R = r, A[r].op =0;
A[r].lsum = A[r].rsum = A[r].sum = A[r].len ();

if(L = = R)return;

Build (Lson, L, A[r].mid ());
Build (Rson, A[r].mid () +1, R);
}
voidUpDate (intRintLintRintOp
{
if(A[r]. L==l && A[r]. R==R)
{///is equal to 1 of the room is completely occupied, then the rest is 0, otherwise all released
A[r].lsum=a[r].rsum=a[r].sum = (op==1?0: A[r].len ());
A[r].op = op;

return;
}
Pushdown (R);

if(R <= a[r].mid ())
UpDate (Lson, L, R, op);
Elseif(L > A[r].mid ())
UpDate (Rson, L, R, op);
Else
{
UpDate (Lson, L, A[r].mid (), op);
UpDate (Rson, A[r].mid () +1, R, op);
}

Pushup (R);///Update parent node up, only the underlying changes are worth the action
}
intQuery (intRintE
{///Judging principle, from left to right, to find the most left to be able to put down the continuous interval e

Pushdown (R);

if(A[r].lsum >= e)returnA[R]. L///determine if the front is sufficient
if(A[lson].sum >= e)returnQuery (Lson, E);///left dial hand tree enough words to go to the left subtree
if(A[lson].rsum +a[rson].lsum >= e)///Judging if the middle is enough
returnA[lson]. R-a[lson].rsum +1;
returnQuery (Rson, E);///just go to the right subtree.
}

intMain ()
{
intN, M, L, R, op;

while(SCANF ("%d%d", &n, &m)! = EOF)
{
Build (1,1, N);

while(m--)
{
scanf"%d", &op);

if(OP = =1)
{
scanf"%d", &r);

if(R > a[1].sum)///there is no continuous interval that can be lowered
printf"0\n");
Else
{

L = Query (1, R);
R = l+r-1;///find the right side.
printf"%d\n", L);
UpDate (1, L, R,1);
}
}
Else
{
scanf"%d%d", &l, &r);
UpDate (1, L, l+r-1,2);
}
}
}

return0; 

}

Hotel-poj 3667 (for continuous sub-range)

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.