【CF720D】Slalom

题意:一个n*m的网格,其中有k个矩形障碍,保证这些障碍不重叠。问你从(1,1)走到(n,m),每步只能往右或往上走,不经过任何障碍的方案数。两种方案被视为不同,当且仅当存在一个障碍,它在第一种方案里被从右侧绕过,而在第二种方案里被从左侧绕过(第一种左,第二种右同理)。

$n,m\le 10^6,k\le 10^5$。

题解:首先我们将相同方案的不同路线放到一起,并用其中最低的那个路线来代表这个方案。然后考虑扫描线,当新加入一个障碍的左侧时,这个侧面以左的所有路线都被迫走到这个障碍的上沿处。用线段树维护一下就好,细节比较多。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
typedef long long ll;
const int P=1000000007;
const int maxk=100010;
const int maxn=1000010;
int n,m,k,tot;
struct node
{
int x,l,r,k;
}p[maxk*3];
int s[maxn<<2],tag[maxn<<2],siz[maxn<<2];
bool cmp(const node &a,const node &b)
{
return (a.x==b.x)?((a.k==b.k)?(a.l>b.l):(a.k<b.k)):(a.x<b.x);
}
inline void pushdown(int l,int r,int x)
{
if(tag[x]==1)
{
s[lson]=s[rson]=0,tag[lson]=tag[rson]=1;
int mid=(l+r)>>1;
siz[lson]=mid-l+1,siz[rson]=r-mid,tag[x]=0;
}
if(tag[x]==2)
{
s[lson]=s[rson]=0,tag[lson]=tag[rson]=2,siz[lson]=siz[rson]=0,tag[x]=0;
}
}
inline void pushup(int x)
{
s[x]=s[lson]+s[rson],siz[x]=siz[lson]+siz[rson];
if(s[x]>=P) s[x]-=P;
}
void modify(int l,int r,int x,int a,int b)
{
if(l==r)
{
s[x]=b;
return ;
}
pushdown(l,r,x);
int mid=(l+r)>>1;
if(a<=mid) modify(l,mid,lson,a,b);
else modify(mid+1,r,rson,a,b);
pushup(x);
}
void updata(int l,int r,int x,int a,int b,int c)
{
if(a<=l&&r<=b)
{
if(c==1) tag[x]=1,siz[x]=r-l+1,s[x]=0;
else tag[x]=2,siz[x]=s[x]=0;
return ;
}
pushdown(l,r,x);
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b,c);
if(b>mid) updata(mid+1,r,rson,a,b,c);
pushup(x);
}
int query(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b) return s[x];
pushdown(l,r,x);
int mid=(l+r)>>1,ret=0;
if(a<=mid) ret+=query(l,mid,lson,a,b);
if(b>mid) ret+=query(mid+1,r,rson,a,b);
if(ret>=P) ret-=P;
return ret;
}
int count(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b) return siz[x];
pushdown(l,r,x);
int mid=(l+r)>>1;
if(b<=mid) return count(l,mid,lson,a,b);
if(a>mid) return count(mid+1,r,rson,a,b);
return count(l,mid,lson,a,b)+count(mid+1,r,rson,a,b);
}
int find(int l,int r,int x,int a)
{
if(l==r) return l;
pushdown(l,r,x);
int mid=(l+r)>>1;
if(a<=siz[lson]) return find(l,mid,lson,a);
return find(mid+1,r,rson,a-siz[lson]);
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
} int main()
{
//freopen("cf720D.in","r",stdin);
n=rd(),m=rd(),k=rd();
int i,a,b,c,d;
for(i=1;i<=k;i++)
{
a=rd(),b=rd(),c=rd(),d=rd();
p[++tot].x=a,p[tot].l=b,p[tot].r=d,p[tot].k=2;
p[++tot].x=a+1,p[tot].l=b,p[tot].r=d,p[tot].k=1;
p[++tot].x=c+1,p[tot].l=b,p[tot].r=d,p[tot].k=3;
}
p[++tot].x=1,p[tot].l=2,p[tot].r=m,p[tot].k=1;
p[++tot].x=1,p[tot].l=2,p[tot].r=m,p[tot].k=3;
p[++tot].x=n+1,p[tot].l=1,p[tot].r=m-1,p[tot].k=2;
sort(p+1,p+tot+1,cmp);
modify(1,m,1,1,1);
for(a=1;a<=tot;a=b+1)
{
for(b=a;b<tot&&p[b+1].x==p[b].x&&p[b+1].k==p[b].k;b++);
if(p[a].k==2)
{
for(i=a;i<=b;i++) if(p[i].r!=m)
{
c=count(1,m,1,1,p[i].r+1);
if(!c) d=0;
else d=find(1,m,1,c);
if(d!=p[i].r+1)
{
modify(1,m,1,p[i].r+1,query(1,m,1,d+1,p[i].r+1));
}
}
}
else if(p[a].k==1)
{
for(i=a;i<=b;i++) updata(1,m,1,p[i].l,p[i].r,1);
}
else
{
for(i=a;i<=b;i++) updata(1,m,1,p[i].l,p[i].r,2);
}
}
printf("%d",query(1,m,1,m,m));
return 0;
}

【CF720D】Slalom 扫描线+线段树的更多相关文章

  1. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  2. 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树

    [BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...

  3. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  4. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  5. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  6. P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)

    题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...

  7. BZOJ 2584: [Wc2012]memory(扫描线+线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2584 题意:给出平面n个线段,任意两个线段严格不相交,且每个线段不平行于坐标轴.移 ...

  8. [BZOJ 1218] [HNOI2003] 激光炸弹 【n logn 做法 - 扫描线 + 线段树】

    题目链接:BZOJ - 1218 题目分析 可以覆盖一个边长为 R 的正方形,但是不能包括边界,所以等价于一个边长为 R - 1 的正方形. 坐标范围 <= 5000 ,直接 n^2 的二维前缀 ...

  9. hdu4419 Colourful Rectangle 12年杭州网络赛 扫描线+线段树

    题意:给定n个矩形,每个矩形有一种颜色,RGB中的一种.相交的部分可能为RG,RB,GB,RGB,问这n个矩形覆盖的面积中,7种颜色的面积分别为多少 思路:把x轴离散化做扫描线,线段树维护一个扫描区间 ...

随机推荐

  1. Java 11正式发布,这几个逆天新特性教你写出更牛逼的代码

    就在前段时间,Oracle 官方宣布 Java 11 (18.9 LTS) 正式发布,可在生产环境中使用! 这无疑对我们来说是一大好的消息.作为一名java开发者来说,虽然又要去学习和了解java11 ...

  2. Android 虚拟键盘弹出把底部栏顶上去的解决办法

    在AndroidManifest中使用ActivityGroup的activity中加上:android:windowSoftInputMode="adjustPan"

  3. Cannot call sendError() after the response has been committed - baiyangliu

    当response提交后,不能调用sendError(),什么意思? 出现这个错误,一定是多次response导致的.可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你 ...

  4. 放假前来个笑话:IT人士群聚喝酒的讲究(超级搞笑)

    大家喝的是啤酒,这时你入座了…… 你给自己倒了杯可乐,这叫低配置. 你给自已倒了杯啤酒,这叫标准配置. 你给自己倒了杯茶水,这茶的颜色还跟啤酒一样,这叫木马. 你给自己倒了杯可乐,还滴了几滴醋,不仅颜 ...

  5. phpcms首页如加上用户登录的信息?

    请看效果图 我用的是cookie的方法,请先打开discuz的文件 \source\function\function_member.php 找到函数function setloginstatus() ...

  6. 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。

    使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...

  7. selenium chrome登陆手机 pc淘宝

    接口登录淘宝,困难度极高,没有人已经实现过. 淘宝登录selenium 手机版  pc版. 由于每天需要使用ip代理大批量的异地登录淘宝帐号,这种情况必然会出现淘宝滑动验证码,使用ActionChai ...

  8. 8 -- 深入使用Spring -- 1...3 容器后处理器

    8.1.3 容器后处理器(BeanFactoryPostProcessor) 容器后处理器负责处理容器本身. 容器后处理器必须实现BeanFacotryPostProcessor接口.实现该接口必须实 ...

  9. Javascript获取IFrame内容(兼容IE&FF)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wintys.blog.51cto.com/425414/123303 作者: W ...

  10. 在netbeans下使用调试PHP的插件XdeBug

    本人的开发环境: wamp最新官网wampserver2.2d-x32版. 下载点:http://nchc.dl.sourceforge.net/project/wampserver/WampServ ...