J-assign the Task-hdu 3974 (multi-operation mixed zone update) segment tree

Source: Internet
Author: User
Tags mul

Test instructions: There are four kinds of operation 1, the interval [L, R] values are added C2, the interval [L, r] values are multiplied by C3, the interval [L, r] values are changed to C4, the interval [L, R] all the number of P-square and analysis: is a more troublesome interval operation, design four operations, update can not be more New to the bottom, but think carefully can think of this is the operation of the interval, so it will cause a part of the interval value is equal, so only need to update to the equal interval part of the line, and note that there is a third operation, the second and the first operation can be removed, the priority of the operation is 3>2>1,. WA took one more time because the first two operations were not removed ************************************************************************* when operation three was carried out #include <algorithm>
#include <stdio.h>
usingnamespaceStd

#defineLson u<<1
#defineRson u<<1|1

Const intMAXN = 1e5+5;
ConstintMoD = 1e4+7;

structNode
{//num Indicates the number of this interval (the values in the interval are equal, not equal to 0), the number of times the Mul is recorded, and the add indicates that it needs to be added.int L, R, Num, Mul, add, Cover;//cover equals 0 when the value of the interval is different, equals 1 means the interval is overwritten, equals 2 indicates the interval value is the sameintM () {return(l+r) >>1;}
intLen () {returnr-l+1;}
}a[maxn<<2];

voidUp (intU
{
if(A[lson].cover && A[rson].cover)
if(A[lson].num = = A[rson].num)
A[u].num = a[lson].num, A[u].cover =2;
}
voidDown (intU
{
if(A[U].L! = A[U].R)
{
if(A[u].cover = =1)
{
A[lson].cover = A[rson].cover =1;
A[lson].num = A[rson].num = A[u].num;
A[lson].add = A[rson].add =0;
A[lson].mul = A[rson].mul =1;

A[u].cover =2, A[u].mul =1, A[u].add =0;
}
if(A[u].mul >1)
{
(A[lson].mul *= A[u].mul)%=mod, (A[rson].mul *= a[u].mul)%=mod;
(A[lson].num *= A[u].mul)%=mod, (A[rson].num *= a[u].mul)%=mod;
(A[lson].add *= A[u].mul)%=mod, (A[rson].add *= a[u].mul)%=mod;

A[u].mul =1;
}

if(A[u].add)
{
(A[lson].add + = A[u].add)%=mod, (a[rson].add + = A[u].add)%=mod;
(A[lson].num + = A[u].add)%=mod, (a[rson].num + = A[u].add)%=mod;

A[u].add =0;
}
}
}
voidBuild (intUintLintR
{
A[u].add =0, A[u].cover =2, A[u].mul =1;
A[u].num =0, A[U].L = L, A[U].R = R;

if(L = = r)
return;

Build (Lson, L, A[U].M ());
Build (Rson, a[u].m () +1, R);
}
voidInsert (intUintLintRintOpintVal
{
if(A[U].L = = L && A[U].R = = R && a[u].cover)
{
if(OP = =3)
A[u].add=0, A[u].cover =1, A[u].mul =1, A[u].num = val;
Elseif(OP = =2)
(A[u].mul *= val)%=mod, (A[u].add *= val)%=mod, (A[u].num *= val)%=mod;
Else
(A[u].add + = val)%=mod, (A[u].num + = val)%=mod;

return;
}

Down (u); A[u].cover =0;

if(R <= a[u].m ())
Insert (Lson, L, R, op, Val);
Elseif(L > a[u].m ())
Insert (Rson, L, R, op, Val);
Else
{
Insert (Lson, L, A[U].M (), OP, Val);
Insert (Rson, a[u].m () +1, R, op, Val);
}

Up (U);
}
intQuery (intUintLintRintP
{

if(A[U].L = = L && A[U].R = = R && a[u].cover)
{
intAns =1;
while(p--) (Ans *= a[u].num)%=mod;
Ans = (ans * a[u].len ())%mod;

returnAns
}

Down (u);

if(R <= a[u].m ())
returnQuery (Lson, L, R, p);
Elseif(L > a[u].m ())
returnQuery (Rson, L, R, p);
Else
{
intLsum = Query (Lson, L, A[U].M (), p);
intRsum = Query (Rson, a[u].m () +1, R, p);

return(lsum+rsum)% MoD;
}
}

intMain ()
{
intN, M;

while(SCANF ("%d%d", &n, &m), n+m)
{
intU, V, op, c;

Build (1,1, N);

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

if(OP! =4)
Insert (1, U, V, op, c);
Else
printf"%d\n", Query (1, u, V, c));
}
}

return0;}

J-assign the Task-hdu 3974 (multi-operation mixed zone update) segment tree

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.