Counting Haybales

时间限制: 50 Sec  内存限制: 256 MB
提交: 52  解决: 18
[提交][状态][讨论版]

题目描述

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
【分析】一道典型的线段树题,可直接套模板。
#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 (线段树)的更多相关文章

  1. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

    描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...

  2. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...

  3. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  4. hdu-5862 Counting Intersections(线段树+扫描线)

    题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  5. Counting Sequences_线段树***

    Description For a set of sequences of integers{a1,a2,a3,...an}, we define a sequence{ai1,ai2,ai3...a ...

  6. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  7. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  8. HDU 3450 Counting Sequences(线段树)

    Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Other ...

  9. HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

随机推荐

  1. 【题解】SDOI2011消耗战

    虚树模板题~洛谷P2495 第一次写虚树,感觉好厉害呀~首先,这道题目的树形dp是非常显然的,要控制一个点&其子树所有点,要么在子树内部割边,要么直接切点该点与父亲的连边.所以dp[u]表示控 ...

  2. 关于 WizTools.org RESTClient的使用

    今天分享一个很好用的测试service的工具,很好用 提供两种方法使用这个东东. 第一种方法 通过cmd命令窗口. (1)cd C:\Users\li_weifeng\Desktop\c4d2(文件存 ...

  3. Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition

    C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...

  4. java的GC与内存泄漏

    从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...

  5. 为什么 Java中1000==1000为false而100==100为true?AND "2+2=5"?

    前提:我们知道,如果两个引用指向同一个对象,用==表示它们是相等的.如果两个引用指向不同的对象,用==表示它们是不相等的,即使它们的内容相同. 运行下面代码:

  6. ubuntu安装mysqlclient

    安装mysql: sudo apt-get install mysql-server mysql-client 然后mysql -V查看mysql是否安装成功 sudo apt-get install ...

  7. linux驱动学习(八) i2c驱动架构(史上最全) davinc dm368 i2c驱动分析【转】

    转自:http://blog.csdn.net/ghostyu/article/details/8094049 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 预备知识 lin ...

  8. linux环境下的GUN make学习笔记(一)

    第一章:概述 1.1:make概述 在linux环境下使用make工具能够比较容易的构建一个属于自己的工程,整个工程的编译只需要一个命令就可以完成编译.连接以至于最后的执行.不过我们需要投入一些时间去 ...

  9. ipad/iphone中的前端调试

    需要在ipad上调试代码, 经过一番搜索(多在google搜到的), 稍微整理下 : ). 1. Settings -> Safari -> Advanced/Developer -> ...

  10. 通过文件配置:firewalld.service(5)

    firewalld.service Name firewalld.service — firewalld service configuration files Synopsis /etc/firew ...