Counting Haybales

时间:2023-01-14 17:05:56

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 ;
}

Counting Haybales的更多相关文章

  1. Counting Haybales &lpar;线段树)

    Counting Haybales 时间限制: 50 Sec  内存限制: 256 MB提交: 52  解决: 18[提交][状态][讨论版] 题目描述 Farmer John is trying t ...

  2. 洛谷 P3184 &lbrack;USACO16DEC&rsqb;Counting Haybales数草垛

    P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq N ...

  3. &lbrack;Usaco2015 DEC&rsqb; Counting Haybales

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4392 [算法] 线段树 时间复杂度 : O(MlogN) [代码] #include ...

  4. bzoj 4747&colon; &lbrack;Usaco2016 Dec&rsqb;Counting Haybales

    23333,在扒了一天题解之后发现我竟然还能秒题,虽然这是个pj的sb题... (排个序,然后upper_bound和lower_bound一用就行了(是不是有O(1)的查询方法啊??貌似要离散啊,一 ...

  5. &lbrack;Usaco2016 Dec&rsqb;Counting Haybales

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4747 先将原数组排序,然后二分查找即可.时间复杂度\(O((N+Q)logN)\). #i ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计&lpar;MLE&rpar;)

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  8. POJ&lowbar;2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  9. ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

    ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...

随机推荐

  1. 使用Adminlite &plus; ASP&period;NET MVC5&lpar;C&num;&rpar; &plus; Entityframework &plus; AutoFac &plus; AutoMapper写了个api接口文档管理系统

    一.演示: 接口查看:http://apidoc.docode.top/ 接口后台:http://apiadmin.docode.top/ 登录:administrator,123456 二.使用到的 ...

  2. Android性能优化

    Android最佳性能实践 Android最佳性能实践(一)——合理管理内存 Android最佳性能实践(二)——分析内存的使用情况 Android最佳性能实践(三)——高性能编码优化 Android ...

  3. Java 集合的基本用法

    package jaxpsax; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; i ...

  4. C&num; 数据类型详解

    在asp.net中C#数据类型包括有值类型.简单类型.整型.布尔型.字符型.浮点型.结构类型等等,有需要学习的朋友可进入参考参考. 4.1 值类型 各种值类型总是含有相应该类型的一个值.C#迫使你初始 ...

  5. RapeLay(电车之狼R)的结局介绍 &lpar;隐藏结局攻略&rpar;

    RapeLay(电车之狼R)的结局介绍 (隐藏结局) 必备知识要让MM怀孕非常easy.起初刚进入调教模式后.仅仅要H一次 MM就開始有时期状态. 生理(连上有红晕) ->不详状态(闭目第一次) ...

  6. linux下的webserver BOA及CGIC库的使用指南(转帖)

    我把网页挂载到nfs 下面的文件中(需要新建一个文件www ),不过这样很方便! 安装过程 ====================================================== ...

  7. MVC4 EF linq从客户端中检测到有潜在的危险的Request&period;Path值

    今天做项目的时候遇到了这样的问题贴出来给大家分享下啦, 使用MVC4 EF linq跳转视图的时候出现,从客户端中检测到有潜在的危险的Request.Path值错误,如下图所示: 解决办法如下:  r ...

  8. ZOJ 3790 Consecutive Blocks 模拟题

    problemCode=3790">Consecutive Blocks 先离散一下,然后模拟,把一种颜色i所在的位置都放入G[i]中.然后枚举一下终点位置,滑动窗体使得起点和终点间花 ...

  9. sql 添加用户

    use master GO EXEC sp_addlogin 'infos1', '1', 'master' exec sp_grantdbaccess 'infos311' -- 给访问权限 USE ...

  10. Swift基础语法

    简介 特点 (1)优于OC,快速,安全 (2)取消了预编译指令包括宏定义(OC用的太多了) (3)取消了OC指针和不安全访问的使用(看不到星星了) (4)舍弃 Objective-C 早期应用 Sma ...