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 ...
随机推荐
- [洛谷P3521][POI2011]ROT-Tree Rotations
题目大意:给一棵$n(n\leqslant2\times10^5)$个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少.输出最少的逆序对个数 题解:线段树合并,对于每个节点求出交换 ...
- WM_CTLCOLOR消息
文章参考地址:http://blog.csdn.net/hisinwang/article/details/8070393 在每个控件开始绘制之前,都会向其父窗口发送WM_CTLCOL ...
- JQuery选择器$()的工作原理浅析
每次申明一个jQuery对象的时候,返回的是jQuery.prototype.init对象,很多人就会不明白,init明明是jQuery.fn的方法啊,实际上这里不是方法,而是init的构造函数,因为 ...
- Fabric证书解析
一.证书目录解析 通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下: ca: 存放组织的根证书和对应的私 ...
- [NOI2003] 文本编辑器 (splay)
复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...
- DIV的变高与变宽
代码: <!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title&g ...
- 【转载】字符串最小表示法-O(n)算法
原博客链接:http://blog.csdn.net/zy691357966/article/details/39854359 未授权,侵权删. 因为这篇博客写得真好..转载了.. 红色的字是原博主写 ...
- bzoj1575 [Usaco2009 Jan]气象牛Baric
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1575 [题解] 动态规划,令f[i,j]表示前i个选了j个,且第i个必选的最小值. 转移就枚 ...
- 关于C++随机函数
#include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main ...
- linux中nginx重定向方法总结
linux中nginx 301重定向跳转方法总结 第一种情况:访问aaaaaaa站定向到bbbbbbbbbbb站 复制代码代码如下: server { server_naaaaaaame www.aa ...