首页 技术 正文
技术 2022年11月6日
0 收藏 540 点赞 849 浏览 3182 个字

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.

输入

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
分析:线段树模板题,注意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[];
struct Node
{
ll sum, Min, Max, lazy;
} T[maxn<<];void PushUp(int rt)
{
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
T[rt].Min = min(T[rt<<].Min, T[rt<<|].Min);
T[rt].Max = max(T[rt<<].Max, T[rt<<|].Max);
}void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
ll t = T[rt].lazy;
T[rt<<].sum += t * (mid - L + );
T[rt<<|].sum += t * (R - mid);
T[rt<<].Min +=t;
T[rt<<|].Min += t;
T[rt<<].Max += t;
T[rt<<|].Max += t;
T[rt<<].lazy +=t;
T[rt<<|].lazy += t;
T[rt].lazy = ;
}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) >> ;
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 + );
T[rt].Min += v;
T[rt].Max += v;
return ;
}
int mid = (L + R) >> ;
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+, 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) >> ;
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 + , r, Rson);
}int main()
{
int i,j;
scanf("%d%d", &n,&m);
Build(, n, );
ll a, b, c;
while(m--)
{
scanf("%s",p);
if(p[]=='M')
{
scanf("%lld%lld", &a, &b);
mi=1e18;
Query(a, b, , n, );
printf("%lld\n",mi);
}
else if(p[]=='S')
{
scanf("%lld%lld", &a, &b);
printf("%lld\n", Query(a, b, , n, ));
}
else
{
scanf("%lld%lld%lld", &a, &b, &c);
Update(a, b, c, , n, );
}
}
//system("pause");
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286