Counting haybales (segment tree)

Source: Internet
Author: User
Tags uppercase letter

Counting Haybales

Time limit: Sec memory limit:
Submitted by: Resolution: 18
Submitted State [Discussion Version]

Title DescriptionFarmer John is trying to hire contractors to help rearrange he farm, but so far all of them has quit when they saw the C omplicated 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 N fields in a row, conveniently numbered 1 ... 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 haybale 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.
InputThe first line contains the positive integers, N (1≤n≤200,000) and Q (1≤q≤100,000).

The next line contains Nnonnegative integers, each at the most 100,000, indicating how many haybales is initially in each field.

Each of the next Q lines contains a single uppercase letter, either M, P or S, followed by either both positive integer S A and B (1≤a≤b≤n), or three positive integers A, B, and C (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.
OutputA line in the output should appear in response to every ' M ' or ' s ' entry in FJ ' s instructions.
Sample input
4 1 2 4M 3 4S 1 3P 2 3 1M 3 4S 1 3
Sample output
2638
"Analysis" A typical line tree problem, can be directly set template.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineMAXN (100 + 50)#defineMOL 1000000009#defineINF 0x3f3f3f3f#defineM 200005#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespaceStd;typedefLong Long intll;intsum[m<<2],mi[m<<2];//Interval summationInlinevoidPushplus (intRT) {Sum[rt]=sum[rt*2]+sum[rt*2+1];}//Interval Minimum valueInlinevoidMin (intRT) {Mi[rt]=min (mi[rt*2],mi[rt*2+1]);}//AchievementsvoidBuild (intLintRintRT) {    if(l==r) {scanf ("%d",&Sum[rt]); MI[RT]=Sum[rt]; return; }    intM= (l+r) >>1;    Build (Lson);    Build (Rson);    Pushplus (RT);    Min (RT); //printf ("rt=%d mi[rt]=%d\n", Rt,mi[rt]);}//UpdatevoidUpdate (intLintRintLintRintRtintc) {if(l==R) {Sum[rt]+=C; MI[RT]+=C; return; }    intM= (l+r) >>1; if(l<=L)    Update (L,R,LSON,C); if(r>m)    Update (L,R,RSON,C);    Pushplus (RT); Min (RT);}//ask for intervals andintQuerysum (intLintRintLintRintRT) {    if(L&LT;=L&AMP;&AMP;R&LT;=R)returnSum[rt]; intM= (l+r) >>1; intans=0; if(l<=m) ans+=querysum (L,r,lson); if(r>m) ans+=querysum (L,r,rson); returnans;}//Ask the interval minimum valueintQuerymin (intLintRintLintRintRT) {    if(L&LT;=L&AMP;&AMP;R&LT;=R)returnMi[rt]; intM= (l+r) >>1; intans=inf; if(l<=m) ans=min (ans,querymin (L,r,lson)); if(r>m) ans=min (ans,querymin (L,r,rson)); returnans;}intMain () {intT,n,a,b,m,c; scanf ("%d%d",&n,&m); Build (1N1); Charop[Ten];  while(m--) {scanf ("%s", op); scanf ("%d%d",&a,&b); if(op[0]=='S') printf ("%d\n", Querysum (A, B,1N1)); Else if(op[0]=='M') printf ("%d\n", Querymin (A, B,1N1)); Else if(op[0]=='P') {scanf ("%d",&c); Update (A, B,1N1, c); }    }    return 0;}
View Code

Counting haybales (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.