Counting Haybales (线段树)
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 ;
}
Counting Haybales (线段树)的更多相关文章
- 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)
描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...
- [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...
- hdu-5862 Counting Intersections(线段树+扫描线)
题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- Counting Sequences_线段树***
Description For a set of sequences of integers{a1,a2,a3,...an}, we define a sequence{ai1,ai2,ai3...a ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- HDU 3450 Counting Sequences(线段树)
Counting Sequences Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Other ...
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...
随机推荐
- [洛谷P1317]低洼地
题目大意:一组数,分别表示地平线的高度变化.高度值为整数,相邻高度用直线连接.找出并统计有多少个可能积水的低洼地?(首尾都为0) 题解:求出其中都多少个不严格下降子段和不严格上升子段所夹的位置,即为答 ...
- BZOJ1513 [POI2006]Tet-Tetris 3D 【二维线段树】
题目链接 BZOJ1513 题解 真正地理解了一波线段树标记永久化的姿势 每个节点维护两个值\(v\)和\(tag\) \(v\)代表儿子中的最值 \(tag\)代表未下传的最值 显然节点的区间大于等 ...
- #define、const、typedef的区别
#define.const.typedef的区别 #define 并不是定义变量, 只是用来做文本替换 例如: #define PI 3.1415926 float angel; angel=30*P ...
- 统计无符号整数二进制中1的个数(Hamming weight)
1.问题来源 之所以来记录这个问题的解法,是因为在在线编程中经常遇到,比如编程之美和京东的校招笔试以及很多其他公司都累此不疲的出这个考题.看似简单的问题,背后却隐藏着很多精妙的解法.查找网上资料,才知 ...
- [HNOI2003]消防局的设立 (贪心)
[HNOI2003]消防局的设立 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达, ...
- TSP问题之状压dp法
首先,我们先来认识一下什么叫做TSP问题 旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题.货郎担问题,是数学领域中著名问题之一.假设有一个旅行商人 ...
- bzoj Gty的超级妹子树 块状树
Gty的超级妹子树 Time Limit: 7 Sec Memory Limit: 32 MBSubmit: 500 Solved: 122[Submit][Status][Discuss] De ...
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- 使用命令wsimport生成WebService客户端
使用命令wsimport生成WebService客户端 wsimpost命令有几个重要的参数: -keep:是否生成java源文件 -d:指定输出目录 -s:指定源代码输出目录 -p ...
- BZOJ 4514: [Sdoi2016]数字配对
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1606 Solved: 608[Submit][Statu ...