Counting Haybales
Counting Haybales
题目描述
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.
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.
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
分析:线段树模板题,注意lazy延迟标记更新;
代码:
#include <cstdio> #include <cstring> #define maxn 200000 + 10 #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 #define ll long long ll min(ll a, ll b) {return a<b ? a : b;} ll max(ll a, ll b) {return a>b ? a : b;} int n,m; ll mi; char p[10]; struct Node { ll sum, Min, Max, lazy; } T[maxn<<2]; void PushUp(int rt) { T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum; T[rt].Min = min(T[rt<<1].Min, T[rt<<1|1].Min); T[rt].Max = max(T[rt<<1].Max, T[rt<<1|1].Max); } void PushDown(int L, int R, int rt) { int mid = (L + R) >> 1; ll t = T[rt].lazy; T[rt<<1].sum += t * (mid - L + 1); T[rt<<1|1].sum += t * (R - mid); T[rt<<1].Min +=t; T[rt<<1|1].Min += t; T[rt<<1].Max += t; T[rt<<1|1].Max += t; T[rt<<1].lazy +=t; T[rt<<1|1].lazy += t; T[rt].lazy = 0; } void Build(int L, int R, int rt) { if(L == R) { scanf("%lld", &T[rt].sum); T[rt].Min = T[rt].Max = T[rt].sum; return ; } int mid = (L + R) >> 1; Build(Lson); Build(Rson); PushUp(rt); } void Update(int l, int r, int v, int L, int R, int rt) { if(l==L && r==R) { T[rt].lazy += v; T[rt].sum += 1ll*v * (R - L + 1); T[rt].Min += v; T[rt].Max += v; return ; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) Update(l, r, v, Lson); else if(l > mid) Update(l, r, v, Rson); else { Update(l, mid, v, Lson); Update(mid+1, r, v, Rson); } PushUp(rt); } ll Query(int l, int r, int L, int R, int rt) { if(l==L && r== R) { //printf("(%d, %d)---Min: %d Max: %d Sum: %d \n", L, R, T[rt].Min, T[rt].Max, T[rt].sum); mi=min(mi,T[rt].Min); return T[rt].sum; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) return Query(l, r, Lson); else if(l > mid) return Query(l, r, Rson); return Query(l, mid, Lson) + Query(mid + 1, r, Rson); } int main() { int i,j; scanf("%d%d", &n,&m); Build(1, n, 1); ll a, b, c; while(m--) { scanf("%s",p); if(p[0]=='M') { scanf("%lld%lld", &a, &b); mi=1e18; Query(a, b, 1, n, 1); printf("%lld\n",mi); } else if(p[0]=='S') { scanf("%lld%lld", &a, &b); printf("%lld\n", Query(a, b, 1, n, 1)); } else { scanf("%lld%lld%lld", &a, &b, &c); Update(a, b, c, 1, n, 1); } } //system("pause"); return 0; }