Counting haybales Interval Update multiple segment tree

Source: Internet
Author: User
Tags uppercase letter

Counting Haybales

Time Limit:10 Sec Memory limit:128 MB
Submit:77 solved:31
[Submit] [Status] [Discuss]

Description

farmer John was trying to hire contractors to help rearrange He farm, but so far all of them has quit when they saw the complicated sequence of instructions FJ wanted them to follow . Left-to-complete project by himself, he realizes that indeed, he had made the project perhaps more complicated than NE Cessary. Please help him follow he instructions to complete the farm upgrade.

FJ ' s farm consists of NN fields in a row, conveniently numbered 1 ... N1 ... N. In each field there can is any number of haybales. Farmer John ' s instructions contain three types of entries:

1) Given a contiguous interval of fields, add a new Hayb Ale to each field.

2) Given a contiguous interval of fields, determine the minimum number of haybales in a field within that interval .

3) Given a contiguous interval of fields, count the total number of haybales inside that interval.

The farmer John is going to rebuild his farm. He had N plots of land, lined in rows, numbered 1 ... N. There are any number of haystack on each piece of land. He can issue three kinds of instructions:
1) for a continuous interval of land, each piece of land increases the same amount of haystack.
2) for a continuous interval of land, output the minimum number of haystack.
3) for a continuous interval of land, the total number of output haystack.

The first line is two positive integers, N (1≤n≤200,000) and Q (1≤q≤100,000).
The next line is n non-negative integers, up to 100,000, indicating how many haystack are on each piece of land.
The following q lines, one for each line, start with a single capital letter (m,p or S), followed by two positive integers A and B (1≤a≤b≤n), or three positive integers a, B, and C (1≤a≤b≤n; 1≤c≤100,000). When and only if the first letter is P, it is a three positive integer.
When the letter is m, the output interval a ... b The most grass heap number.
When the letter is P, in the interval a ... B, each piece of land added C heap of grass.
When the letter is m, the output interval a ... b The sum of the number of haystack.

Each line is a number that responds to the ' M ' or ' S ' command.

Input

The first line contains the positive integers, N (1≤n≤200,000) and Q (1≤q≤100,000).

The next line contains N nonnegative integers, each at most 100,000, indicating how many haybales is initially in each fi Eld.

Each of the next Q lines contains a single uppercase letter, either M, P or S, followed by either, positive integers AA and BB (1≤a≤b≤n), or three positive integers AA, BB, and CC (1≤a≤b≤n; 1≤c≤100,000). There'll be three positive integers if and only if the uppercase letter is P.

If the letter was M, print the minimum number of haybales in the interval of ... B.

If the letter was P, put C new haybales in each field in the interval of ... B.

If the letter was S, print the total number of haybales found within interval in fields from A ... B.

Output

A line in the output should appear in response to every ' M ' or ' s ' entry in FJ ' s instructions.

Sample Input

4 5
3 1 2 4
M 3 4
S 1 3
P 2 3 1
M 3 4
S 1 3

Sample Output

2
6
3
8

Hintsource

Platinum Acknowledgements Claris Provide translations

Each time the interval is updated, it is found that the leaf node is updated on the Internet, and I have a difference before using the notation. But the method is better understood than the notation.

#include <cstdio>#include<algorithm>#definell Long Long#defineMAXN 800010#defineLson l,m,rt<<1#defineRson m+1,r,rt<< 1 | 1using namespacestd;intn,m;ll mi;Charp[Ten];structnode{ll Sum,mi;} T[MAXN];voidPushup (intRT) {T[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum; T[RT].MI=min (t[rt<<1].mi,t[rt<<1|1].mi);}voidBuildintLintRintRT) {    //t[rt].lazy=0;    if(l==R) {scanf ("%lld",&T[RT].MI); T[rt].sum=T[RT].MI;return; }    intM= (l+r) >>1;    Build (Lson);    Build (Rson); Pushup (RT);}voidUpdata (intLintRintCintLintRintRT) {    if(l==R) {T[rt].sum+ = (ll) c* (r-l+1); T[RT].MI+=c;return; }    intM= (l+r) >>1; if(l<=m) updata (L,r,c,lson); if(r>m) updata (L,r,c,rson); Pushup (RT);} ll query (intLintRintLintRintRT) {    if(L&GT;=L&AMP;&AMP;R&LT;=R) {//ZaiMi=min (MI,T[RT].MI); returnt[rt].sum; }    if(l>r| | R&LT;L) {return  0;}//Buzai    intM= (l+r) >>1; returnQuery (L,r,rson) +query (L,r,lson);}intMain () {//freopen ("data.in", "R", stdin);    intI,j,a,b,c; scanf ("%d%d",&n,&m); Build (1N1);  while(m--) {scanf ("%s", p); if(p[0]=='M') {scanf ("%d%d",&a,&b); Mi=1e18; Query (A, B,1N1); printf ("%lld\n", MI); }        Else {            if(p[0]=='S') {scanf ("%d%d", &a,&b); printf"%lld\n", query (A, B,1N1));} Else{scanf ("%d%d%d",&a,&b,&c); Updata (A,b,c,1N1); }        }    }}

Counting haybales Interval Update multiple 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.