Counting Haybales (线段树)

Counting Haybales

时间限制: 50 Sec  内存限制: 256 MB
提交: 52  解决: 18
[提交][状态][讨论版]

题目描述

Farmer John is trying to hire contractors to help rearrange his farm, but so far all of them have quit when they saw the complicated sequence of instructions FJ wanted them to follow. Left to complete the project by himself, he realizes that indeed, he has made the project perhaps more complicated than necessary. Please help him follow his 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 be 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.

输入

The first line contains two 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 are initially in each field.

Each of the next Q lines contains a single uppercase letter, either M, P or S, followed by either 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). There will be three positive integers if and only if the uppercase letter is P.

If the letter is M, print the minimum number of haybales in the interval of fields from A…B.

If the letter is P, put C new haybales in each field in the interval of fields from A…B.

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

输出

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

样例输入

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

样例输出

2
6
3
8
【分析】一道典型的线段树题,可直接套模板。
#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>
#define maxn (100 + 50)
#define mol 1000000009
#define inf 0x3f3f3f3f
#define M 200005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long int ll;
int sum[M<<2],mi[M<<2];
//区间求和
inline void PushPlus(int rt) {
    sum[rt]=sum[rt*2]+sum[rt*2+1];
}
//区间最小值
inline void Min(int rt) {
    mi[rt]=min(mi[rt*2],mi[rt*2+1]);
}
//建树
void  Build(int l,int r,int rt) {
    if(l==r) {
        scanf("%d",&sum[rt]);
        mi[rt]=sum[rt];
        return;
    }
    int m=(l+r)>>1;
    Build(lson);
    Build(rson);
    PushPlus(rt);
    Min(rt);
    //printf("rt=%d mi[rt]=%d
",rt,mi[rt]);
}
//更新
void Update(int L,int R,int l,int r,int rt,int c) {
    if(l==r) {
        sum[rt]+=c;
        mi[rt]+=c;
        return;
    }
    int m=(l+r)>>1;
    if(L<=m)Update(L,R,lson,c);
    if(R>m)Update(L,R,rson,c);
    PushPlus(rt);
    Min(rt);
}
//询问区间和
int QuerySum(int L,int R,int l,int r,int rt) {
    if(L<=l&&r<=R)return sum[rt];
    int m=(l+r)>>1;
    int ans=0;
    if(L<=m)ans+=QuerySum(L,R,lson);
    if(R>m)ans+=QuerySum(L,R,rson);
    return ans;
}
//询问区间最小值
int QueryMin(int L,int R,int l,int r,int rt) {
    if(L<=l&&r<=R)return mi[rt];
    int m=(l+r)>>1;
    int ans=inf;
    if(L<=m)ans=min(ans,QueryMin(L,R,lson));
    if(R>m)ans=min(ans,QueryMin(L,R,rson));
    return ans;
}

int main() {
    int T,n,a,b,m,c;
    scanf("%d%d",&n,&m);
    Build(1,n,1);
    char op[10];
    while(m--) {
        scanf("%s",op);
        scanf("%d %d",&a,&b);
        if(op[0]=='S')printf("%d
",QuerySum(a,b,1,n,1));
        else if(op[0]=='M')printf("%d
",QueryMin(a,b,1,n,1));
            else if(op[0]=='P') {
                scanf("%d",&c);
                Update(a,b,1,n,1,c);
            }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jianrenfang/p/5748717.html