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. 【Lucene】三个高亮显示模块的简单示例-Highlighter

    Lucene针对高亮显示功能提供了两种实现方式,分别是Highlighter和FastVectorHighlighter 这里的三个示例都是使用Highlighter: 示例代码: package c ...

  2. 常用API——字符串String型函数

    上图: 声明 var myString = new String(“Every good boy does fine.”); var myString = “Every good boy does f ...

  3. WinHEC 2015 深圳之行

    在上个月,本人有幸在MVP Team和MVP Lead的帮助下,参加了微软在深圳召开的WinHEC 2015大会. 在本次大会上,见证了微软和国内企业的合作,并且发布一些关于在Windows 10方面 ...

  4. mysql 单表排序,相同值排序

    两种方式: 第一种是利用笛卡尔积,两对比排序 -- 学校类型数据 SELECT t.examid,'-' AS unitcode,t.schooltype,'-' AS classname,t.bkr ...

  5. OpenSessionInViewFilter配置和作用

    Spring为我们解决Hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sessio ...

  6. Centos 安装jdk1.8

    我是根据右边链接进行安装的 ,但是第一步不同噢.http://www.cnblogs.com/spiders/archive/2016/09/06/5845727.html 1.下载rpm安装文件. ...

  7. 如何创建一个GitLab Web Hooks?

    Git Hooks Git 能在特定的重要动作发生时触发自定义的脚本. 这些脚本都被存储在 Git 目录下的 hooks 子目录中(.git/hooks).当 git init 初始化一个仓库时,Gi ...

  8. ELF Format 笔记(十五)—— 符号哈希表

    ilocker:关注 Android 安全(新手) QQ: 2597294287 符号哈希表用于支援符号表的访问,能够提高符号搜索速度. 下表用于解释该哈希表的组织,但该格式并不属于 ELF 规范. ...

  9. NIO与AIO,同步/异步,阻塞/非阻塞

    1.flip(),compact(),与clear()的使用 flip()内部实现,先将limit设为当前位置,再将缓冲区的postion设为0,所以是为将缓冲区的数据写出到其它通道或者get()作准 ...

  10. C#学习笔记-数据的传递以及ToolStripProgressBar

    代码: 方法一:窗体的代码-->可以直接通过预设的Click事件来实现控制进度条. public partial class Form1 : Form { public Form1() { In ...