题目描述

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 NN fields in a row, conveniently numbered 1 \ldots N1…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.

FJ像雇佣几个人在他的农场里帮忙,他需要进行很多种操作,请你帮他搞定。

FJ的农场有N块田地排长一行,编号是从1到N的。每一块田地都有很多草包。FJ要进行下面几种操作:

1)给定一段连续的田地,给每一个田地都增加一些新的草包。

2)给定一段连续的田地,找出草包最少的田地有多少草包。

3)给定一段连续的田地,统计一共有多少草包。

输入输出格式

输入格式:

The first line contains two positive integers, NN (1 \leq N \leq 200,0001≤N≤200,000) and

QQ (1 \leq Q \leq 100,0001≤Q≤100,000).

The next line contains NN nonnegative integers, each at most 100,000,

indicating how many haybales are initially in each field.

Each of the next QQ lines contains a single uppercase letter, either M, P or S,

followed by either two positive integers AA and BB (1 \leq A \leq B \leq N1≤A≤B≤N),

or three positive integers AA, BB, and CC (1 \leq A \leq B \leq N1≤A≤B≤N;

1 \leq C \leq 100,0001≤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 \ldots BA…B.

If the letter is P, put CC new haybales in each field in the interval of fields

from A \ldots BA…B.

If the letter is S, print the total number of haybales found within interval of

fields from A \ldots BA…B.

输出格式:

A line in the output should appear in response to every 'M' or 'S' entry in FJ's

instructions.

输入输出样例

输入样例#1: 复制

4 5
3 1 2 4
M 3 4
S 1 3
P 2 3 1
M 3 4
S 1 3
输出样例#1: 复制

2
6
3
8
思路:裸的线段树,区间修改+区间查询。标签写着并差集,不知道并差集可不可做。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 200010
using namespace std;
int n,m;
struct nond{
int l,r;
long long min,sum,flag;
}tree[MAXN*];
void up(int now){
tree[now].sum=tree[now*].sum+tree[now*+].sum;
tree[now].min=min(tree[now*].min,tree[now*+].min);
}
void build(int now,int l,int r){
tree[now].l=l;
tree[now].r=r;
if(tree[now].l==tree[now].r){
scanf("%lld",&tree[now].sum);
tree[now].min=tree[now].sum;
return ;
}
int mid=(tree[now].l+tree[now].r)/;
build(now*,l,mid);
build(now*+,mid+,r);
up(now);
}
void down(int now){
tree[now*].flag+=tree[now].flag;
tree[now*+].flag+=tree[now].flag;
tree[now*].min+=tree[now].flag;
tree[now*+].min+=tree[now].flag;
tree[now*].sum+=(tree[now*].r-tree[now*].l+)*tree[now].flag;
tree[now*+].sum+=(tree[now*+].r-tree[now*+].l+)*tree[now].flag;
tree[now].flag=;
}
void change(int now,int l,int r,long long opt){
if(tree[now].l==l&&tree[now].r==r){
tree[now].min+=opt;
tree[now].flag+=opt;
tree[now].sum+=(tree[now].r-tree[now].l+)*opt;
return ;
}
if(tree[now].flag) down(now);
int mid=(tree[now].l+tree[now].r)/;
if(r<=mid) change(now*,l,r,opt);
else if(l>mid) change(now*+,l,r,opt);
else{
change(now*,l,mid,opt);
change(now*+,mid+,r,opt);
}
up(now);
}
long long query(int now,int l,int r,int opt){
if(tree[now].l==l&&tree[now].r==r){
if(opt==) return tree[now].sum;
else return tree[now].min;
}
if(tree[now].flag) down(now);
int mid=(tree[now].l+tree[now].r)/;
if(r<=mid) return query(now*,l,r,opt);
else if(l>mid) return query(now*+,l,r,opt);
else{
if(opt==) return query(now*,l,mid,opt)+query(now*+,mid+,r,opt);
else return min(query(now*,l,mid,opt),query(now*+,mid+,r,opt));
}
}
int main(){
scanf("%d%d",&n,&m);
build(,,n);
for(int i=;i<=m;i++){
char s[];int x,y,z;
scanf("%s%d%d",s,&x,&y);
if(s[]=='S') cout<<query(,x,y,)<<endl;
if(s[]=='M') cout<<query(,x,y,)<<endl;
if(s[]=='P'){
scanf("%lld",&z);
change(,x,y,z);
}
}
} /*
4 5
3 1 2 4
M 3 4
S 1 3
P 2 3 1
M 3 4
S 1 3
*/
 

洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales的更多相关文章

  1. P3130 [USACO15DEC]计数haybalesCounting Haybales

    P3130 [USACO15DEC]计数haybalesCounting Haybales 1)给定一段连续的田地,给每一个田地都增加一些新的草包. 2)给定一段连续的田地,找出草包最少的田地有多少草 ...

  2. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

  3. 洛谷P1144-最短路计数-最短路变形

    洛谷P1144-最短路计数 题目描述: 给出一个\(N\)个顶点\(M\)条边的无向无权图,顶点编号为\(1-N\).问从顶点\(1\)开始,到其他每个点的最短路有几条. 思路: \(Dijkstra ...

  4. 洛谷P3130 haybalesCounting Haybale P 题解

    题目 [USACO15DEC]haybalesCounting Haybale P 题解 最近刚刚自学了线段树这个数据结构,恰巧做到了这道线段树的模板题.其实也没有什么好多说的,接触过线段树的大犇肯定 ...

  5. 洛谷——P3914 染色计数

    P3914 染色计数 题目描述 有一颗NN个节点的树,节点用1,2,\cdots,N1,2,⋯,N编号.你要给它染色,使得相邻节点的颜色不同.有MM种颜色,用1,2,\cdots,M1,2,⋯,M编号 ...

  6. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  7. 洛谷——P1176 路径计数2

    P1176 路径计数2 题目描述 一个N \times NN×N的网格,你一开始在(1,1)(1,1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问到达(N,N)(N,N),即右下角有 ...

  8. 洛谷 P3914 染色计数

    P3914 染色计数 题目描述 有一颗NN个节点的树,节点用1,2,\cdots,N1,2,⋯,N编号.你要给它染色,使得相邻节点的颜色不同.有MM种颜色,用1,2,\cdots,M1,2,⋯,M编号 ...

  9. 洛谷 P1176 路径计数2

    P1176 路径计数2 题目描述 一个N×N的网格,你一开始在(1, 1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问到达(N, N),即右下角有多少种方法. 但是这个问题太简单了, ...

随机推荐

  1. 【POJ 2750】 Potted Flower(线段树套dp)

    [POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   ...

  2. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

  3. CURRENMONTH TAG in Automation Framework

    /** * @param input * <CURRENTMONTH><CURRENTMONTH+1> * @return Month "MM" */ pr ...

  4. 数位$dp$

    数位\(dp\)搞了一上午才搞懂.靠这种傻\(X\)的东西竟然花了我一上午的时间. 数位\(dp\) 概念 数位\(dp\)就是强制你分类一些数,例如给你一段区间,然后让你求出不包含\(2\)的数的个 ...

  5. Fiddler 故障

    如果只要打开 Fiddler 就没法进网页,重启 Fiddler 问题依旧.卸载并重装 Fiddler 后,提示 8888 端口被占,错误弹窗中包含“ipoverusbsvc:2492”,说明有设备在 ...

  6. <Sicily>Fibonacci

    一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...

  7. Rendering and compositing out of process iframes

    For Developers‎ > ‎Design Documents‎ > ‎Out-of-Process iframes (OOPIFs)‎ > ‎ Rendering and ...

  8. TCP学习前的准备——可靠数据传输协议

    由于传输层所依赖的网络层是不可靠的,通过逐渐考虑实际情况不断引入新技术来实现可靠数据传输. 完全可信的信道 有比特差错的信道 新的协议功能: 1.    差错检测:检验和 2.    接收方反馈:序号 ...

  9. Kubernetes安装配置(包括master和node)

    部署Kubernetes云计算平台,至少准备两台服务器,此处为4台,包括一台Docker仓库: Kubernetes Master节点:192.168.124.20 Kubernetes Node1节 ...

  10. 如何解决本地仓库和远程仓库的冲突(Conflict)

    Background: 我有一个github仓库管理我的代码,我将这个仓库的代码clone到我的工作电脑和私人电脑本地方便我上班和在家时都可以对我的代码进行更新. 一天,我在家修改过代码之后并未提交, ...