【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. 系统优化 /etc/sysctl.conf

    # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl ...

  2. 看板与Scrum:哪个更适合你的团队?

    敏捷是理想型指标和原则,看板和Scrum是帮助团队坚持敏捷原则并完成工作的基本框架.本文详细介绍了在Scrum和看板之间做出选择时要考虑的关键因素,以及如果我们无法做出决定时该怎么办. Scrum和看 ...

  3. 12款优秀 jQuery Ajax 分页插件和教程

    12款优秀 jQuery Ajax 分页插件和教程 在这篇文章中,我为大家收集了12个基于 jQuery 框架的 Ajax 分页插件,这些插件都提供了详细的使用教程和演示.Ajax 技术的出现使得 W ...

  4. MATLAB 安装使用libsvm详细步骤

    根据本文后面部分博友提出的在配置过程中出现的问题,其中需要特别强调的一点:整个过程,都是在 libsvm-3.12\matlab目录下操作的.如果这一点你忽视了,你不可能解决配置中报的Bug,即使重新 ...

  5. 各个层次的gcc警告

    http://blog.csdn.net/lizzywu/article/details/9419145 各个层次的gcc警告从上到下覆盖 变量(代码)级:指定某个变量警告 int a __attri ...

  6. 【转】【C#】ZIP、RAR 压缩与解压缩

    压缩文件夹 源码如下 using System; using System.Data; using System.Configuration; using System.Web; using Syst ...

  7. sdi 采集卡---环视频拼接直播方案

    http://www.upano.cn/# 360度无死角直播1080p 30fps http://search.jd.com/Search?keyword=sdi%E9%87%87%E9%9B%86 ...

  8. linux -- ubuntu 脚本开机自启动

    有一个脚本想在ubuntu 虚拟机开机时,自动运行. 创建脚本文件 在“/opt/lampp/”下新建一个脚本文件:lampp_start.sh #!/bin/bash /opt/lampp/lamp ...

  9. 【Mongo】聚合函数

    http://blog.csdn.net/miyatang/article/details/20997313 SQL Terms, Functions, and Concepts MongoDB Ag ...

  10. OCX控件打包成CAB并实现数字签名过程

      OCX打包CAB并签名过程 一.打包cab 制作cab文件时需要将所有的相关文件都包含进去,可以通过Depends(VC自带的)检查需要的文件.使用inf文件将这些东西都写进去. 1.制作inf文 ...