Hiho the interval modification of 20-week segment tree

Source: Internet
Author: User

Interval modification time limit for segment tree:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

For small Ho to show the understanding of the line tree, small Hi said very satisfied, but satisfied enough? So little hi changed the question and gave it to Little ho:

Suppose that there are n items placed on the shelves from left to right, and sequentially numbered 1 to N, where the price of the commodity labeled I is pi. Small hi's every operation is divided into two possibilities, the first is to modify the price-small hi to give a period of time [L, R] and a new price newp, all the labels in this interval of the price of the goods into NEWP. The second is asking--little hi gives an interval [L, R], and what little Ho does is to figure out the total price of all the items that are labeled in this interval, and then tell the little hi.

So how does little ho solve this problem?

Tip: To promote scientific development in addition to human curiosity, there are people lazy heart!

Input

Each test point (input file) has and has only one set of test data.

The 1th behavior of each set of test data is an integer n, meaning as described earlier.

The 2nd behavior of each group of test data n integers, respectively, describes the weight of each commodity, where the I integer represents the weight pi of the commodity labeled I.

The 3rd behavior of each set of test data is an integer q, which represents the number of operations performed by small hi.

Each group of test data n+4~n+q+3 line, each line describes an operation, each line begins with a number belonging to 0 or 1, respectively, indicating that the line describes a query and the price of a product change two cases. For line n+i+3, if the line describes a query, then the next two integers are Li, RI, which represents a range of small hi queries [Li, RI]; If the line describes a change in the price of a commodity, then three integer LI,RI,NEWP, indicating the label in the interval [Li, RI] The prices of the goods are all modified to NEWP.

For 100% of data, meet N<=10^5,q<=10^5, 1<=li<=ri<=n,1<=pi<=n, 0<pi, newp<=10^4.

Output

For each set of test data, for each small hi inquiry, according to the order in the input, each output line, indicating the results of the query: marking in the interval [Li, Ri] The sum of the prices of all the goods.

Sample input
104733 6570 8363 7391 4511 1433 2281 187 5166 378 61 5 10 15771 1 7 36490 8 100 1 41 6 8 1571 3 4 1557
Sample output
473114596
Direct Set Template
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 2e9#defineMet (b) memset (a,b,sizeof a)typedefLong Longll;using namespacestd;Const intN = 1e7+5;Const intM = 4e6+5;intn,sum[n],m;intTree[n],lazy[n];voidPushup (intPOS) {Tree[pos]=tree[pos<<1]+tree[pos<<1|1];}voidPushdown (intPosintLintR) {    if(Lazy[pos]) {Tree[pos<<1]=l*Lazy[pos]; Tree[pos<<1|1]=r*Lazy[pos]; Lazy[pos<<1]=Lazy[pos]; Lazy[pos<<1|1]=Lazy[pos]; Lazy[pos]=0; }return;}voidBuildintLintRintPOS) {Lazy[pos]=0; if(l==r) {scanf ("%d",&Tree[pos]); return; }    intMid= (l+r) >>1; Build (L,mid,pos<<1); Build (Mid+1,r,pos<<1|1);    Pushup (POS); return;}voidUpdateintLintRintValintLintRintPOS) {    if(l>=l&&r<=R) {Tree[pos]=val* (r-l+1); Lazy[pos]=Val; return; }    intMid= (l+r) >>1; Pushdown (Pos,mid-l+1, R-mid); if(l<=mid) Update (l,r,val,l,mid,pos<<1); if(mid<r) Update (l,r,val,mid+1,r,pos<<1|1); Pushup (POS);}intQueryintLintRintLintRintPOS) {    if(L&LT;=L&AMP;&AMP;R&LT;=R)returnTree[pos]; intMid= (l+r) >>1; Pushdown (Pos,mid-l+1, R-mid); intans=0; if(L<=mid) Ans+=query (l,r,l,mid,pos<<1); if(R>mid) Ans+=query (l,r,mid+1,r,pos<<1|1); returnans;}intMain () {scanf ("%d",&N); Build (1N1); scanf ("%d",&m); intSign,l,r,val;  while(m--) {scanf ("%d",&Sign ); if(sign) {scanf ("%d%d%d",&l,&r,&val); Update (L,r,val,1N1); }        Else{scanf ("%d%d",&l,&R); printf ("%d\n", Query (L,r,1N1)); }    }    return 0;}

Hiho the interval modification of 20-week 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.