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. MongoDB-常见问题

    前言        MongoDB数据库的版本为3.0.7.记录在使用MongoDB数据库时遇到的各种问题,目前是遇到一个整理一个,没有进行分类,等整理较多的时候,进行分类整理一下. 1.使用可视化工 ...

  2. SQL SERVER错误:已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)

    在SSMS(Microsoft SQL Server Management Studio)里面,查看数据库对应的表的时候,会遇到"Lock Request time out period e ...

  3. Solr嵌套子文档的弊端以及一种替代方式

    背景:在考察了多种工具后,我们决定使用solr来作为多标签用户管理体系的查询方案. 原计划:电话,call客,跟进等等记录上报到kafka,然后通过flume+morphline录入到solr中.每一 ...

  4. YII2 项目安装步骤及异常记录

    项目环境: 操作系统:windows 7 版本管理:git 该项目我是在mac上面创建的,mac上面的环境也是一波三折啊!但我同事的环境是在windows上面,因为是前端同学,所以只好我再次操刀了.. ...

  5. BOOST.Asio——Overview

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  6. Linux Daemon进程

    模型 创建孤儿进程 fork() 创建新的会话 setid() 改变当前目录为根目录chdir() 重设文件权限掩码umask() 关闭文件描述符close() 创建孤儿进程 创建孤儿进程只需要将父进 ...

  7. mysqldump 备份命令使用中的一些经验总结

    mysqldump的一个小坑(自测) 正文:经常使用接触mysql复制功能的朋友应该对mysqldump命令不陌生吧,鄙人最近也在研究学习这一块的内容,经过几天的测试,发现mysqldump使用中容易 ...

  8. MalformedByteSequenceException: Invalid byte 1 of 1-byte

    修改了线上程序的xml配置文件,重启后报如下错误: MalformedByteSequenceException: Invalid byte 1 of 1-byte 百度了下大体的意思是说文件的编码错 ...

  9. spring mvc 快速入门

    ---------- 转自尚学堂 高淇 --------- Spring  MVC 背景介绍 Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC ...

  10. 【原】CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...