【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动

Description

约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[Ai.,Bi](-100000≤Ai<Bi≤10^5),y坐标为i.牛棚的外栏即x轴,原点是牛棚的门.奶牛们开始处于(S,N),她们需要回到牛棚的门(下图中用“*’表示).
 
 约翰的初衷是为了给奶牛们练习跳跃,但是奶牛们似乎更愿意四蹄着地,慢慢她沿着栅栏
走.当她们走到栅栏的尽头,就会朝着牛棚的个栏方向(即y轴负方向)行走,直到碰上另一条栅栏或是牛棚外栏.这时候她们便要选择继续向左走,还是向右走.奶牛们希望走的路程最短.由于可方向的路程一定,你只需求出z方向走的最短路程,使奶牛回到原点.

Input

    第1行:N,S(-100000≤S≤100000).
    第2到N+1行:每行2个整数Ai,Bi,(-100000≤Ai≤Bi≤100000).

Output

    最小的x方向的步数

Sample Input

4 0
-2 1
-1 2
-3 0
-2 1

Sample Output

4

HINT

题解:本题做法有很多。我是先列了个朴素的方程,设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栅栏行动 线段树的更多相关文章

  1. [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 ...

  2. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  3. [bzoj4391] [Usaco2015 dec]High Card Low Card 贪心 线段树

    ---题面--- 题解: 观察到以决策点为分界线,以点数大的赢为比较方式的游戏都是它的前缀,反之以点数小的赢为比较方式的都是它的后缀,也就是答案是由两段答案拼凑起来的. 如果不考虑判断胜负的条件的变化 ...

  4. POJ2374 Fence Obstacle Course

    题意 Language:Default Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...

  5. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

  6. BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏

    3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 47  Solved: 37[ ...

  7. BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved:  ...

  8. BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 45  Solved:  ...

  9. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

随机推荐

  1. 【安装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 ...

  2. stm32独立看门狗

    转载:http://blog.sina.com.cn/s/blog_7f1dc53b01010mqa.html 实验现象: 开始LED1亮,LED2熄灭,若不隔时间按KEY1则发现LED2因独立看门狗 ...

  3. 修改jvm内存大小

  4. R语言--saprkR基本使用

    1.在sparkR的shell中交互式使用 sparkR --masterspark://10.130.2.20:7077 sparkR --masterlocal[6] #sparkR --mast ...

  5. Web下的整体测试 --性能测试及优化思路

    随着Internet的日益普及,现在基于B/S结构的大型应用越来越多,可如何对这些应用进行测试成为日益迫切的问题.有许多测试人员来信问我B/S的测试如何做,由于工作较繁忙,对大家提出的问题也是头痛医头 ...

  6. Qt中的串口编程之一

    QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...

  7. css -- 通俗理解inline、block、inline-block

    display:inline; 内联元素,简单来说就是在同一行显示. display:block; 块级元素,简单来说就是就是有换行,会换到第二行. display:inline-block; 就是在 ...

  8. ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 。连属性都不能用

    ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 .连属性都不能用

  9. ueditor1_4_3_3编辑器的应用

    教程使用的是ueditor1_4_3_3版本. 首先到官网http://ueditor.baidu.com/website/download.html下载jsp  utf-8版 下载好以后,解压,把解 ...

  10. oracle查询一个用户下的所有表

    select table_name from all_tables where owner_name=upper('scott'); 用户名一定要大写//