【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树
【BZOJ4094】[Usaco2013 Dec]Optimal Milking
Description
Input
Output
Sample Input
1
2
3
4
5
5 2
2 7
1 10
Sample Output
【样例解释】
第1天,最优方案为2+4=6 ( 方案1+3+2一样)
第2天,最优方案为7+4=11
第3天,最优方案为10+3+2=15
题解:本题是一道有着奇妙区间合并方法的线段树
设s[x][k](k=0,1,2,3)表示当前区间,k&2代表左端点是否选择,k&1代表右端点是否选择
区间合并不难吧?
#include <cstdio>
#include <cstring>
#include <iostream>
#define lson x<<1
#define rson x<<1|1
using namespace std;
int n,m;
long long ans;
int s[200010][4];
int max(int x,int y,int z){return max(max(x,y),z);}
int max(int a,int b,int c,int d){return max(max(a,b),max(c,d));}
void pushup(int x)
{
for(int i=0;i<4;i++)
s[x][i]=max(s[lson][i&2]+s[rson][2+(i&1)],s[lson][(i&2)+0]+s[rson][i&1],s[lson][(i&2)+1]+s[rson][i&1]);
}
void build(int l,int r,int x)
{
if(l==r)
{
scanf("%d",&s[x][3]);
return ;
}
int mid=l+r>>1;
build(l,mid,lson),build(mid+1,r,rson);
pushup(x);
}
void updata(int l,int r,int x,int y,int v)
{
if(l==r)
{
s[x][3]=v;
return ;
}
int mid=l+r>>1;
if(y<=mid) updata(l,mid,lson,y,v);
else updata(mid+1,r,rson,y,v);
pushup(x);
}
int main()
{
scanf("%d%d",&n,&m);
build(1,n,1);
int i,a,b;
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
updata(1,n,1,a,b);
ans+=max(s[1][0],s[1][1],s[1][2],s[1][3]);
}
printf("%lld",ans);
return 0;
}
【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树的更多相关文章
- bzoj 4094: [Usaco2013 Dec]Optimal Milking
4094: [Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 . ...
- 【BZOJ 3050】【USACO2013 Jan】Seating 线段树
线段树维护4个标记, 昨天互测时题意理解错了,今天上午才发现. #include<cstdio> #include<cstring> #include<algorithm ...
- BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库
正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...
- BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案
Description Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales ...
- BZOJ 4094 USACO 2013 Dec. Optimal Milking
线段树 每个节点保存4个值,both表示左右端点都取,neither表示左右端点都不取,left表示只取左端点,right表示只取右端点. 维护的特殊姿势: $cur$的$both=max(ls.l+ ...
- [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)
题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...
- BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树
BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意: 约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...
- 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树
[BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...
- BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)
5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 182 Solved: 131[ ...
随机推荐
- 获取touchstart,touchmove,touchend 坐标
简单说下如何用jQuery 和 js原生代码获取touchstart,touchmove,touchend 坐标值: jQuery 代码: $('#id').on('touchstart',funct ...
- ASP.NET C# 获取当前日期 时间 年 月 日 时 分 秒
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...
- 各个层次的gcc警告
http://blog.csdn.net/lizzywu/article/details/9419145 各个层次的gcc警告从上到下覆盖 变量(代码)级:指定某个变量警告 int a __attri ...
- asp.net MVC提高开发速度(创建项目模板)
- php十行代码将xml转成数组
<?php header("Content-Type:text/html;charset=utf-8"); function xml2array($filename){ $x ...
- win8 学习笔记二 输出日志
Win8 Store App的日志输出不像Desktop App 那么简单,见 这篇文档(http://www.cnblogs.com/xiaokang088/archive/2011/12/27/2 ...
- 如何正确理解javascript的模块化的
- 电脑开机提示 NTLDR is missing
电脑开机,黑屏并提示NTLDR is missing Press Ctrl+Alt+Del to restart,并且反复重启都不行,这是怎么回事呢?首先我们来分析下这段英语的含义:NTLDR是指NT ...
- centos7 systemctl
下机为systemctl指令systemctl enable *.service #开机运行服务systemctl disable *.service #取消开机运行systemctl start * ...
- HTML&CSS精选笔记_盒子模型
盒子模型 认识盒子模型 所谓盒子模型就是把HTML页面中的元素看作是一个矩形的盒子,也就是一个盛装内容的容器.每个矩形都由元素的内容.内边距(padding).边框(border)和外边距(margi ...