P3130 [USACO15DEC]计数haybalesCounting Haybales

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

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

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


错误日志: \(pushdown\) 时记录懒标记用了 \(int\) , 下次(在没有把握的情况下)应全部替换


Solution

线段树, 懒标记, 靠前练手

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
using namespace std;
LL RD(){
LL out = 0,flag = 1;char c = getchar();
while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return flag * out;
}
const LL maxn = 400019;
LL num, na;
LL v[maxn];
#define lid (id << 1)
#define rid (id << 1) | 1
struct seg_tree{
LL l, r;
LL min, sum, lazy;
}tree[maxn << 2];
void pushup(LL id){
tree[id].sum = tree[lid].sum + tree[rid].sum;
tree[id].min = min(tree[lid].min, tree[rid].min);
}
void build(LL id, LL l, LL r){
tree[id].l = l, tree[id].r = r;
if(l == r){
tree[id].min = tree[id].sum = v[l];
return ;
}
LL mid = (l + r) >> 1;
build(lid, l, mid), build(rid, mid + 1, r);
pushup(id);
}
void pushdown(LL id){
if(tree[id].lazy != 0){
LL val = tree[id].lazy;
tree[lid].sum += val * (tree[lid].r - tree[lid].l + 1);
tree[rid].sum += val * (tree[rid].r - tree[rid].l + 1);
tree[lid].min += val;
tree[rid].min += val;
tree[lid].lazy += val;
tree[rid].lazy += val;
tree[id].lazy = 0;
}
}
void update(LL id, LL val, LL l, LL r){
pushdown(id);
if(tree[id].l == l && tree[id].r == r){
tree[id].lazy += val;
tree[id].sum += val * (tree[id].r - tree[id].l + 1);
tree[id].min += val;
return ;
}
LL mid = (tree[id].l + tree[id].r) >> 1;
if(mid < l)update(rid, val, l, r);
else if(mid >= r)update(lid, val, l, r);
else update(lid, val, l, mid), update(rid, val, mid + 1, r);
pushup(id);
}
LL get_sum(LL id, LL l, LL r){
pushdown(id);
if(tree[id].l == l && tree[id].r == r){
return tree[id].sum;
}
LL mid = (tree[id].l + tree[id].r) >> 1;
if(mid < l)return get_sum(rid, l, r);
else if(mid >= r)return get_sum(lid, l, r);
else return get_sum(lid, l, mid) + get_sum(rid, mid + 1, r);
}
LL get_min(LL id, LL l, LL r){
pushdown(id);
if(tree[id].l == l && tree[id].r == r){
return tree[id].min;
}
LL mid = (tree[id].l + tree[id].r) >> 1;
if(mid < l)return get_min(rid, l, r);
else if(mid >= r)return get_min(lid, l, r);
else return min(get_min(lid, l, mid), get_min(rid, mid + 1, r));
}
int main(){
num = RD(), na = RD();
for(LL i = 1;i <= num;i++)v[i] = RD();
build(1, 1, num);
char cmd;
while(na--){
cin>>cmd;
LL l = RD(), r = RD();
if(cmd == 'M')printf("%lld\n", get_min(1, l, r));
else if(cmd == 'S')printf("%lld\n", get_sum(1, l, r));
else{
LL val = RD();
update(1, val, l, r);
}
}
return 0;
}

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

  1. 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales

    P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...

  2. 洛谷P3130 haybalesCounting Haybale P 题解

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

  3. 计数排序(counting-sort)——算法导论(9)

    1. 比较排序算法的下界 (1) 比较排序     到目前为止,我们已经介绍了几种能在O(nlgn)时间内排序n个数的算法:归并排序和堆排序达到了最坏情况下的上界:快速排序在平均情况下达到该上界.   ...

  4. Objective-C内存管理之引用计数

    初学者在学习Objective-c的时候,很容易在内存管理这一部分陷入混乱状态,很大一部分原因是没有弄清楚引用计数的原理,搞不明白对象的引用数量,这样就当然无法彻底释放对象的内存了,苹果官方文档在内存 ...

  5. 最小生成树计数 bzoj 1016

    最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...

  6. swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  7. [LeetCode] Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  8. C++ 引用计数技术及智能指针的简单实现

    一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...

  9. css-列表或标题的多级计数

    利用css实现多级计数,比如1/1.1/1.1.1这种层层嵌套的计数,主要利用到counter-reset/counter-increment/counter/content/:before. 一.标 ...

随机推荐

  1. Scrum Meeting 7 -2014.11.13

    之前srcum没写好是我的错.以后会每天更新的. 老师反映之前项目小组从pdf中提取作者效果不好,我们讨论决定进行一定的优化.在整合测试的同时开始服务器程序部署. Member Today’s tas ...

  2. 团队项目-北航MOOC系统Android客户端 NABC

    北航MOOC系统Android客户端 NABC (N) Need 需求 MOOC的全名是Massive Open Online Course,被称作大型开放式网络课程.2012年,美国的顶尖大学陆续设 ...

  3. 提不起劲想赶紧完工 Scrum Meeting 博客汇总

    提不起劲想赶紧完工 Scrum Meeting 博客汇总 一.Alpha阶段 1,第一次Scrum Meeting 2,第二次Scrum Meeting 3,第三次Scrum Meeting 4,第四 ...

  4. bata5

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  5. Week2-作业1 《构建之法》1、2、16章观后感

    这几天阅读了<构建之法>中的几章,受益匪浅,刷新了很多我对软件工程的认知.这本书让我很惊喜,阅读起来不像其他书一样枯燥,有很多人物的设计,以及对话的形式,非常有趣. 第一章.概述 读完第一 ...

  6. Java基本程序设计结构

    一.要求: 1.设平面上有一个m×n 的网格,将左下角的网格点标记为(0,0)而右上角的网格点标记为(m,n).某人想从(0,0)出发沿网格线行进到达(m,n),但是在网格点(i,j)处他只能向上行进 ...

  7. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  8. js 添加事件兼容性

    var tools = { //添加事件 addHandle: function (e, type, handle) { if (e.addEventListener) { e.addEventLis ...

  9. (2)YARN的工作流程

    Writing YARN Applications 文档中的启动过程: Application submission client向Yarn ResourceManager提交一个Applicatio ...

  10. DAY4-Flask项目

    项目出现的问题: 问题处在import requests.requests库已经安装了啊; 找了半天也不知道具体错误在哪里,根据提示想是不是http.py这个模块与Python内置的同名模块冲突了?所 ...