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 ...
随机推荐
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...
- JavaScript渐变效果的实现
鼠标移上去透明度渐渐增加,鼠标移出,透明度渐渐减小. 关键代码: view source print? 1 var speed = 0; 2 if(target>obj.alpha){ 3 ...
- 在JS中,一切东东其实都是对象
对象是组成JavaScript的基本单元,在JS中,一切东东其实都是对象,而且功能非常强大,它不仅风格独特,功能也与众不同. 一.引用(reference) 引用的概念是JS的基础之一,它是指向对象实 ...
- MySQL使用笔记(六)条件数据记录查询
By francis_hao Dec 17,2016 条件数据记录查询 mysql> select field1,field2-- from table_name where 条件; 其中 ...
- CSS三大特性(继承、优先级、层叠)之个人见解
首先声明一下CSS三大特性——继承.优先级和层叠.继承即子类元素继承父类的样式,比如font-size,font-weight等f开头的css样式以及text-align,text-indent等t开 ...
- 转:一个Restful Api的访问控制方法(简单版)
最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...
- 【数据结构】bzoj1636/bzoj1699排队
Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置 ...
- 图论:Gale-Shapley算法
Gale-Shapley算法又叫做延迟认可算法,它可以解决这么一个问题 一共有N位男士和N位女士 每位男士对每位女士都有一个好感度,让他们结合成为N对夫妻,要求男士优先表白,最后问结合情况 第一轮,每 ...
- [bzoj1009][HNOI2008]GT考试——KMP+矩阵乘法
Brief Description 给定一个长度为m的禁止字符串,求出长度为n的字符串的个数,满足: 这个字符串的任何一个字串都不等于给定字符串. 本题是POJ3691的弱化版本. Algorithm ...
- JS组件入门
用React感觉component老好用了. 那如何用原生JS来模拟实现React中的component方法呢:http://huziketang.com/blog/posts/detail?post ...