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\n",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\n",QuerySum(a,b,1,n,1));
        else if(op[0]=='M')printf("%d\n",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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值