D. Slalom

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Little girl Masha likes winter sports, today she's planning to take part in slalom skiing.

The track is represented as a grid composed of n × m squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1, 1) to the square (n, m). She can move from a square to adjacent square: either to the right, or upwards. If the square is occupied by an obstacle, it is not allowed to move to that square.

One can see that each obstacle can actually be passed in two ways: either it is to the right of Masha's path, or to the left. Masha likes to try all ways to do things, so she would like to know how many ways are there to pass the track. Two ways are considered different if there is an obstacle such that it is to the right of the path in one way, and to the left of the path in the other way.

Help Masha to find the number of ways to pass the track. The number of ways can be quite big, so Masha would like to know it modulo109 + 7.

The pictures below show different ways to pass the track in sample tests.

Input

The first line of input data contains three positive integers: nm and k (3 ≤ n, m ≤ 106, 0 ≤ k ≤ 105) — the size of the track and the number of obstacles.

The following k lines contain four positive integers each: x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — coordinates of bottom left, and top right squares of the obstacle.

It is guaranteed that there are no obstacles at squares (1, 1) and (n, m), and no obstacles overlap (but some of them may touch).

Output

Output one integer — the number of ways to pass the track modulo 109 + 7.

Examples

input
3 3 0
output
1
input
4 5 1
2 2 3 4
output
2
input
5 5 3
2 2 2 3
4 2 5 2
4 4 4 4
output
3

Solution

和BZOJ4422是一个类型的题。线段树扫描线+差分 优化DP    (传送门)

这个题也是一样的,转移比较好想就不说了.

把每个障碍分左边右边记录下来,然后一维线段树一维扫描线。

线段树支持区间覆盖,单点修改,区间查询和即可。

写扫描线都用结构体,记录一下x,y1,y2,0/1。这样排序会比较麻烦...有个不错的姿势,就是对每个x建一个vector,vector里面存一个pair,这样会非常方便。

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
#define LL long long
inline int read()
{
int x=; char ch=getchar();
while (ch<'' || ch>'') {ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x;
}
#define MOD 1000000007
#define MAXN 1000010
int N,M,K,tp;
namespace SegmentTree
{
struct SegmentTreeNode{int l,r,size,cov,sum;}tree[MAXN<<];
#define ls now<<1
#define rs now<<1|1
inline void Update(int now) {tree[now].sum=tree[ls].sum+tree[rs].sum; tree[now].sum%=MOD;}
inline void BuildTree(int now,int l,int r)
{
tree[now].l=l; tree[now].r=r; tree[now].size=r-l+; tree[now].cov=-;
if (l==r) return;
int mid=(l+r)>>;
BuildTree(ls,l,mid); BuildTree(rs,mid+,r);
Update(now);
}
inline void cover(int now,int D) {tree[now].cov=D; tree[now].sum=(LL)tree[now].size*D%MOD;}
inline void PushDown(int now)
{
if (tree[now].l==tree[now].r) return;
if (tree[now].cov!=-) cover(ls,tree[now].cov),cover(rs,tree[now].cov),tree[now].cov=-;
}
inline void Cover(int now,int L,int R,int D)
{
if (R<L) return;
int l=tree[now].l,r=tree[now].r;
PushDown(now);
if (L<=l && R>=r) {cover(now,D); return;}
int mid=(l+r)>>;
if (L<=mid) Cover(ls,L,R,D);
if (R>mid) Cover(rs,L,R,D);
Update(now);
}
inline void Modify(int now,int pos,int D)
{
int l=tree[now].l,r=tree[now].r;
PushDown(now);
if (l==r) {cover(now,D); return;}
int mid=(l+r)>>;
if (pos<=mid) Modify(ls,pos,D);
else Modify(rs,pos,D);
Update(now);
}
inline int Query(int now,int L,int R)
{
if (R<L) return ;
int l=tree[now].l,r=tree[now].r;
PushDown(now);
if (L<=l && R>=r) return tree[now].sum;
int mid=(l+r)>>,re=;
if (L<=mid) (re+=Query(ls,L,R))%=MOD;
if (R>mid) (re+=Query(rs,L,R))%=MOD;
return re;
}
}
struct LineNode{int x,y1,y2,f;}Line[MAXN<<];
bool cmp(LineNode A,LineNode B) {return A.x==B.x? A.y1==B.y1? A.y2>B.y2 : A.y1>B.y1 : A.x<B.x;}
#define Pa pair<int,int>
set<Pa>mp;
set<Pa>::iterator is;
Pa loc;
int main()
{
N=read(),M=read(),K=read();
for (int x1,x2,y1,y2,i=; i<=K; i++)
x1=read(),y1=read(),x2=read(),y2=read(),
Line[++tp].x=x1,Line[tp].y1=y1,Line[tp].y2=y2,Line[tp].f=,
Line[++tp].x=x2+,Line[tp].y1=y1,Line[tp].y2=y2,Line[tp].f=;
SegmentTree::BuildTree(,,M);
SegmentTree::Modify(,,);
sort(Line+,Line+tp+,cmp);
int X=;
for (int i=; Line[i].x==; X++,i++) if (Line[i].f) mp.insert(make_pair(Line[i].y1,Line[i].y2));
mp.insert(make_pair(,));
for (int i=; i<=N; i++)
{
for (int j=X,tmp; Line[j].x==i; j++)
if (Line[j].f)
if (Line[j].y2<M)
loc=(*--mp.lower_bound(make_pair(Line[j].y2+,))),
tmp=SegmentTree::Query(,loc.second+,Line[j].y2+),
SegmentTree::Modify(,Line[j].y2+,tmp);
for (int j=X; Line[j].x==i; j++) if (!Line[j].f) mp.erase(make_pair(Line[j].y1,Line[j].y2));
for (int j=X; Line[j].x==i; X++,j++)
if (Line[j].f) mp.insert(make_pair(Line[j].y1,Line[j].y2)),SegmentTree::Cover(,Line[j].y1,Line[j].y2,);
}
loc=*(--mp.end());
printf("%d\n",SegmentTree::Query(,loc.first+,M)%MOD);
return ;
}

【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)的更多相关文章

  1. LOJ #2537. 「PKUWC 2018」Minimax (线段树合并 优化dp)

    题意 小 \(C\) 有一棵 \(n\) 个结点的有根树,根是 \(1\) 号结点,且每个结点最多有两个子结点. 定义结点 \(x\) 的权值为: 1.若 \(x\) 没有子结点,那么它的权值会在输入 ...

  2. UOJ#7. 【NOI2014】购票 | 线段树 凸包优化DP

    题目链接 UOJ #7 题解 首先这一定是DP!可以写出: \[f[i] = \min_{ancestor\ j} \{f[j] + (d[j] - d[i]) * p[i] + q[i]\}\] 其 ...

  3. 【学习笔记】线段树—扫描线补充 (IC_QQQ)

    [学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...

  4. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  5. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  6. HDU 4419 Colourful Rectangle --离散化+线段树扫描线

    题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...

  7. BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤

    3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...

  8. BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞

    看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...

  9. hdu 5091(线段树+扫描线)

    上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...

随机推荐

  1. JDBC快速入门

    /** * JDBC快速入门: * 1.导入jar包 数据库驱动 * 2.注册驱动 * 3.获取数据库连接对象 Connection * 4.定义sql语句 * 5.获取发送执行sql语句的对象 St ...

  2. 2-C程序结构

    一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下: #include <stdio.h> #include & ...

  3. 怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块

     因为项目的需要,所以研究了一下自动生成测试代码.将经验记录下来,总会有用的.我个人认为,好记性不如多做笔记多反思总结. 1.    前提条件 开发环境已正确配置 工程已解决JUnit依赖关系(pom ...

  4. Windows Phone 8.1低功耗蓝牙开发-Nokia Treasure Tag

    1. 引言 上一篇文章<Windows 8.1 低功耗蓝牙开发>讲述了如何在Windows 8.1平台上创建低功耗蓝牙应用,并且以TI的Sensor Tag为例,给出了代码步骤和演示.其实 ...

  5. winform窗体(五)——布局方式

    一.默认布局 ★可以加panel,也可以不加: ★通过鼠标拖动控件的方式,根据自己的想法布局.拖动控件的过程中,会有对齐的线,方便操作: ★也可选中要布局的控件,在工具栏中有对齐工具可供选择,也有调整 ...

  6. EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载

    之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...

  7. MYSQL 5.7 无法启动(Could not open error log file errno 2)

    前两天电脑中毒, 病毒好像把mysql的 log.err 文件给删掉了.然后服务一直启动不了:Could not open error log file errno 2. 然后疯狂百度,搜索的结果大多 ...

  8. spring 4.x下让http请求返回json串

    当前很多应用已经开始将响应返回为json串,所以基于springframework框架开发的服务端程序,让响应返回json字符串成为了一种常用手段. 这里介绍一下如何在spring-MVC框架下方便快 ...

  9. 烂泥:智能DNS使用与配置

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司的业务现在已经扩展到海外,对外提供的统一接口都是通过域名来解析的,但是海外用户访问国 ...

  10. [WPF系列]-数据邦定之DataTemplate 对分层数据的支持

    到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...