【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树
【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动
Description
.jpg)
Input
Output
Sample Input
-2 1
-1 2
-3 0
-2 1
Sample Output
HINT
.jpg)
题解:本题做法有很多。我是先列了个朴素的方程,设f[i][0/1]表示走到第i个栅栏左/右边界的最小路程,转移如下

然后就根据绝对值分两种情况讨论,每个都建一个线段树维护一下就好了
注意此题从(S,N)开始!
#include <cstdio>
#include <iostream>
#include <cstring>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=200005;
int n,m;
int s[maxn<<2][2],t[maxn<<2][2];
void pushdown(int x,int p)
{
if(t[x][p])
{
s[lson][p]=s[rson][p]=1<<30;
t[lson][p]=t[rson][p]=1;
t[x][p]=0;
}
}
void updata(int l,int r,int x,int a,int b,int p)
{
if(l==r)
{
s[x][p]=min(s[x][p],b);
return ;
}
pushdown(x,p);
int mid=l+r>>1;
if(a<=mid) updata(l,mid,lson,a,b,p);
else updata(mid+1,r,rson,a,b,p);
s[x][p]=min(s[lson][p],s[rson][p]);
}
void cover(int l,int r,int x,int a,int b,int p)
{
if(a<=l&&r<=b)
{
t[x][p]=1,s[x][p]=1<<30;
return ;
}
pushdown(x,p);
int mid=l+r>>1;
if(a<=mid) cover(l,mid,lson,a,b,p);
if(b>mid) cover(mid+1,r,rson,a,b,p);
s[x][p]=min(s[lson][p],s[rson][p]);
}
int query(int l,int r,int x,int a,int b,int p)
{
if(a<=l&&r<=b) return s[x][p];
pushdown(x,p);
int mid=l+r>>1;
if(b<=mid) return query(l,mid,lson,a,b,p);
if(a>mid) return query(mid+1,r,rson,a,b,p);
return min(query(l,mid,lson,a,b,p),query(mid+1,r,rson,a,b,p));
}
int main()
{
scanf("%d%d",&n,&m);
m+=100002;
memset(s,0x3f,sizeof(s));
updata(1,maxn,1,100002,-100002,0),updata(1,maxn,1,100002,100002,1);
int i,a,b,ta,tb;
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
a+=100002,b+=100002;
ta=min(query(1,maxn,1,1,a,0)+a,query(1,maxn,1,a+1,maxn,1)-a);
tb=min(query(1,maxn,1,1,b,0)+b,query(1,maxn,1,b+1,maxn,1)-b);
updata(1,maxn,1,a,ta-a,0),updata(1,maxn,1,a,ta+a,1);
updata(1,maxn,1,b,tb-b,0),updata(1,maxn,1,b,tb+b,1);
if(b>a+1) cover(1,maxn,1,a+1,b-1,0),cover(1,maxn,1,a+1,b-1,1);
}
printf("%d",min(query(1,maxn,1,1,m,0)+m,query(1,maxn,1,m+1,maxn,1)-m));
return 0;
}
【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树的更多相关文章
- [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树
Description Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1…N. T ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- [bzoj4391] [Usaco2015 dec]High Card Low Card 贪心 线段树
---题面--- 题解: 观察到以决策点为分界线,以点数大的赢为比较方式的游戏都是它的前缀,反之以点数小的赢为比较方式的都是它的后缀,也就是答案是由两段答案拼凑起来的. 如果不考虑判断胜负的条件的变化 ...
- POJ2374 Fence Obstacle Course
题意 Language:Default Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...
- Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 218 Solved: ...
- BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 47 Solved: 37[ ...
- BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 43 Solved: ...
- BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: ...
- BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班
题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Description ...
随机推荐
- 【安装Python环境】之安装Selenium2时报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 12: invalid continuation byte问题
问题描述: windows8.1系统,Python3环境安装Selenium2时报错,错误如下: ..... ..... File "F:\软件\python3.6.1\lib\site-p ...
- stm32独立看门狗
转载:http://blog.sina.com.cn/s/blog_7f1dc53b01010mqa.html 实验现象: 开始LED1亮,LED2熄灭,若不隔时间按KEY1则发现LED2因独立看门狗 ...
- 修改jvm内存大小
- R语言--saprkR基本使用
1.在sparkR的shell中交互式使用 sparkR --masterspark://10.130.2.20:7077 sparkR --masterlocal[6] #sparkR --mast ...
- Web下的整体测试 --性能测试及优化思路
随着Internet的日益普及,现在基于B/S结构的大型应用越来越多,可如何对这些应用进行测试成为日益迫切的问题.有许多测试人员来信问我B/S的测试如何做,由于工作较繁忙,对大家提出的问题也是头痛医头 ...
- Qt中的串口编程之一
QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...
- css -- 通俗理解inline、block、inline-block
display:inline; 内联元素,简单来说就是在同一行显示. display:block; 块级元素,简单来说就是就是有换行,会换到第二行. display:inline-block; 就是在 ...
- ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 。连属性都不能用
ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 .连属性都不能用
- ueditor1_4_3_3编辑器的应用
教程使用的是ueditor1_4_3_3版本. 首先到官网http://ueditor.baidu.com/website/download.html下载jsp utf-8版 下载好以后,解压,把解 ...
- oracle查询一个用户下的所有表
select table_name from all_tables where owner_name=upper('scott'); 用户名一定要大写//