【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom
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: n, m 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
3 3 0
1
4 5 1
2 2 3 4
2
5 5 3
2 2 2 3
4 2 5 2
4 4 4 4
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)的更多相关文章
- LOJ #2537. 「PKUWC 2018」Minimax (线段树合并 优化dp)
题意 小 \(C\) 有一棵 \(n\) 个结点的有根树,根是 \(1\) 号结点,且每个结点最多有两个子结点. 定义结点 \(x\) 的权值为: 1.若 \(x\) 没有子结点,那么它的权值会在输入 ...
- UOJ#7. 【NOI2014】购票 | 线段树 凸包优化DP
题目链接 UOJ #7 题解 首先这一定是DP!可以写出: \[f[i] = \min_{ancestor\ j} \{f[j] + (d[j] - d[i]) * p[i] + q[i]\}\] 其 ...
- 【学习笔记】线段树—扫描线补充 (IC_QQQ)
[学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤
3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...
- BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞
看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...
- hdu 5091(线段树+扫描线)
上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...
随机推荐
- [转]iOS开发中的火星坐标系及各种坐标系转换算法
iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183 其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...
- Java调优知识汇总
查看java进程运行状况jps -lvm 查看java默认堆大小 java -XX:+PrintFlagsFinal | grep MaxHeapSize eclipse调试设置vm参数 在项目上右键 ...
- python-list tuple dict set
1:删除一个列表末尾的元素 pop方法 >>> a [1, 'Jack', 2, 3, 2] >>> a.pop() >>> a [1, 'Jac ...
- Mindjet MindManager思维导图工具的使用 - 项目管理系列文章
在项目管理过程中,不可避免的要使用到各种各样的项目管理工具.本文就对我自己使用的Mindjet Manager进行一下介绍,推荐给大家使用. 1. 打开软件 这里使用了2012版 2. 软件的界面 ...
- Linux 日志报错 xxx blocked for more than 120 seconds
监控作业发现一台服务器(Red Hat Enterprise Linux Server release 5.7)从凌晨1:32开始,有一小段时间无法响应,数据库也连接不上,后面又正常了.早上检查了监听 ...
- C# 实现酒店房态图
酒店管理系统最重要和实用的是能够及时.一目了然的反应房间状态的房态图,之前在开发一个的酒店管理系统中做了一个还算实用的房态图,现在分享下: 鼠标移到每个房间上面,可显示提示信息: 还可以自定义设置不同 ...
- 如何区分/dev/input/event
方法是把每一个/dev/input/event打开.通过ioctl函数来读取设备name,每一个设备name是固定的,可以根据name区分event.我这是查找触摸事件为例:代码如下: static ...
- 安卓SeekBar
public class Speak extends Fragment implements OnSeekBarChangeListener { private SeekBar bar1; priva ...
- gcc中__builtin_return_address和__VA_ARGS__
— Built-in Function: void * __builtin_return_address (unsigned int level) This function returns the ...
- JAVA开发过程中的各种小坑
1.有时候你在本地跑的ECLIPSE中得到的正确的结果,部署到服务器上使用其他容器,如tomcat或WARS的时候,跑出的结果也许就不一致, 我们程序员会经常抱怨,在我机器上跑的好好的. 在不同的容器 ...