
Counting Haybales
时间限制: 50 Sec 内存限制: 256 MB
提交: 52 解决: 18
[提交][状态][讨论版]
题目描述
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 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.
输出
样例输入
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<<],mi[M<<];
//区间求和
inline void PushPlus(int rt) {
sum[rt]=sum[rt*]+sum[rt*+];
}
//区间最小值
inline void Min(int rt) {
mi[rt]=min(mi[rt*],mi[rt*+]);
}
//建树
void Build(int l,int r,int rt) {
if(l==r) {
scanf("%d",&sum[rt]);
mi[rt]=sum[rt];
return;
}
int m=(l+r)>>;
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)>>;
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)>>;
int ans=;
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)>>;
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(,n,);
char op[];
while(m--) {
scanf("%s",op);
scanf("%d %d",&a,&b);
if(op[]=='S')printf("%d\n",QuerySum(a,b,,n,));
else if(op[]=='M')printf("%d\n",QueryMin(a,b,,n,));
else if(op[]=='P') {
scanf("%d",&c);
Update(a,b,,n,,c);
}
}
return ;
}