【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 ...
随机推荐
- 空间管理 您的位置: 51Testing软件测试网 » lilisx2006的个人空间 » 日志 在一个没有测试经理的小公司如何做好测试
如何在一个没有测试经理的小公司做好测试? 首先,没有测试经理意味着测试人员没有最直接的管理者,往往这种时候的管理者是开发经理或技术总监,但他们何其忙耶?同时,在无人监管的情况下,测试是一个很容易偷懒的 ...
- Python 字符串语法,for
#-*- coding:utf-8 -*- #字符串的拼接方式一 a = "hello" b = "world" c = a + " " + ...
- Windoows窗口程序二
WNDCLASS属性style取值: CS_GLOBALCLASS--应用程序全局窗口类 CS_BYTEALIGNCLIENT--窗口客户区的水平位置8倍数对齐 CS_BYTEALIGNWINDOW- ...
- webstorm软件使用记录
右边的那条线的去除:setting-editor-appearance-show right margin 勾选去掉
- e551. 精简的Applet
Every applet must subclass Applet. import java.applet.*; import java.awt.*; public class BasicApplet ...
- ffmpeg 源码分析
http://blog.csdn.net/liuhongxiangm/article/details/8824761 https://code.google.com/p/ffmpegsource/is ...
- pyqt的多Button的点击事件的槽函数的区分发送signal的按钮。
关键函数:QPushButton的setObjectName()/objectName() 个人注解:按功能或者区域,将按钮的点击事件绑定的不同的槽函数上. from PyQt5.QtWidgets ...
- C语言简单选择排序
#include <stdio.h> int main(int argc, char const *argv[]) { // 将数组按照从小到大排序 , , , , , }; int i, ...
- JavaScript-事件冒泡简介及应用
一.什么是事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事 ...
- Linux安装php运行环境
安装apache: yum install httpd httpd-devel 启动apache: /etc/init.d/httpd start 此时输入服务器的IP地址,应该看到apache的服 ...