[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=3387

[算法]

f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数

f[i][1]表示从第i个栅栏的右端点走到原点的最少移动步数

我们可以用线段树优化转移

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 50010
const int V = ; struct info
{
int x,y;
} a[MAXN]; int i,N,S,x,y;
int f[MAXN][]; struct SegmentTree
{
struct Node
{
int l,r;
int mx,tag;
} Tree[V << ];
inline void build(int index,int l,int r)
{
int mid;
Tree[index].l = l;
Tree[index].r = r;
Tree[index].mx = Tree[index].tag = ;
if (l == r) return;
mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
}
inline void pushdown(int index)
{
if (Tree[index].tag)
{
Tree[index << ].mx = max(Tree[index << ].mx,Tree[index].tag);
Tree[index << | ].mx = max(Tree[index << | ].mx,Tree[index].tag);
Tree[index << ].tag = max(Tree[index << ].tag,Tree[index].tag);
Tree[index << | ].tag = max(Tree[index << | ].tag,Tree[index].tag);
Tree[index].tag = ;
}
}
inline void modify(int index,int l,int r,int val)
{
int mid;
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].mx = Tree[index].tag = val;
return;
}
pushdown(index);
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << ,l,r,val);
else if (mid + <= l) modify(index << | ,l,r,val);
else
{
modify(index << ,l,mid,val);
modify(index << | ,mid + ,r,val);
}
}
inline int query(int index,int pos)
{
int mid;
if (Tree[index].l == Tree[index].r) return Tree[index].mx;
pushdown(index);
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= pos) return query(index << ,pos);
else return query(index << | ,pos);
}
} T; int main()
{ scanf("%d%d",&N,&S);
S += V;
for (i = ; i <= N; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].x += V;
a[i].y += V;
}
T.build(,,V << );
f[][] = f[][] = ;
a[].x = a[].y = V;
for (i = ; i <= N; i++)
{
x = T.query(,a[i].x);
y = T.query(,a[i].y);
f[i][] = min(f[x][] + abs(a[i].x - a[x].x),f[x][] + abs(a[i].x - a[x].y));
f[i][] = min(f[y][] + abs(a[i].y - a[y].x),f[y][] + abs(a[i].y - a[y].y));
T.modify(,a[i].x,a[i].y,i);
}
printf("%d\n",min(abs(S - a[N].x) + f[N][],abs(S - a[N].y) + f[N][])); return ; }

[BZOJ 3387] Fence Obstacle Course的更多相关文章

  1. 【BZOJ 3387】 线段树= =

    57 跨栏训练为了让奶牛参与运动,约翰建造了 K 个栅栏.每条栅栏可以看做是二维平面上的一条线段,它们都平行于 X 轴.第 i 条栅栏所覆盖的 X 轴坐标的区间为 [ Ai,Bi ], Y 轴高度就是 ...

  2. 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树

    [BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...

  3. POJ2374 Fence Obstacle Course

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

  4. POJ 2374 Fence Obstacle Course(线段树+动态规划)

    Fence Obstacle Course Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2524   Accepted:  ...

  5. Fence Obstacle Course

    Fence Obstacle Course 有n个区间自下而上有顺序的排列,标号\(1\sim n\),第i个区间记做\([l_i,r_i]\),现在从第n个区间的起点s出发(显然s在\([l_n,r ...

  6. [BZOJ 1724] Fence Repair

    这大概是BZOJ里除了A+B Problem最水的一道题了吧 题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1724 这道题其实有一些思路还是可 ...

  7. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  8. poj2374 Fence Obstacle Course[线段树+DP]

    https://vjudge.net/problem/POJ-2374 吐槽.在这题上面磕了许久..英文不好题面读错了qwq,写了个错的算法搞了很久..A掉之后瞥了一眼众多julao题解,**,怎么想 ...

  9. POJ2374 Fence Obstacle Course 【线段树】

    题目链接 POJ2374 题解 题意: 给出\(n\)个平行于\(x\)轴的栅栏,求从一侧栅栏的某个位置出发,绕过所有栅栏到达另一侧\(x = 0\)位置的最短水平距离 往上说都是线段树优化dp 我写 ...

随机推荐

  1. CSS选择器优先级计算

    优先级从高到低排列,浏览器优先满足前面的规则 1,!important优先级最高 2,内联样式 3,作者>读者>浏览器 4,优先级权重加法 id选择器+100/个 类/伪类选择器+10/个 ...

  2. mongoose 操作 mongodb 笔记 (自己的笔记,自己看的)

    mongodb下载/安装 mongoose   npm install --save mongoose mongoose 数据库连接 const mongoose = require('mongoos ...

  3. springboot测试类

    Controller测试类 /** * Created by zhiqi.shao on 2017/5/12. */ @RunWith(SpringJUnit4ClassRunner.class) @ ...

  4. 【剑指Offer】26、二叉搜索树与双向链表

      题目描述:   输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.   解题思路:   首先要理解此题目的含义,在双向链表中,每个结 ...

  5. html表单练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. [bzoj4477 Jsoi2015]字符串树 (可持久化trie)

    传送门 Solution 复习下tire( ̄▽ ̄)/ 裸的可持久化tire,我用树剖求了下LCA Code #include <cstdio> #include <cstring&g ...

  7. jquery 绑定回车(Enter )事件

    全局: $(function(){ document.onkeydown = function(e){ var ev = document.all ? window.event : e; if(ev. ...

  8. 突如其来的“中断异常”,我(Java)该如何处理?

    3.try-catch块 try语句块中代码执行时发生三种情况: try语句块中代码正常执行完毕,没有任何异常,那么catch语句块的代码将不会被执行. import java.util.*; pub ...

  9. C#学习笔记_04_流程控制

    04_流程控制 程序的执行结构: 顺序结构 分支结构 循环结构 可以通过某些特定的控制语句来控制代码的执行结构 分支流程控制 if else 基本语法 可以只有if没有else,但是不能没有if只有e ...

  10. Codeforces 805A/B/C

    A. Fake NP 传送门:http://codeforces.com/contest/805/problem/A 本题是一个数学问题. 给定两个正整数l,r(l≤r),对于区间[l..r]上的任一 ...