【ARC101F】Robots and Exits 树状数组
题目大意
有 \(n\) 个机器人和 \(m\) 个出口。
这 \(n\) 个机器人的初始位置是 \(a_1,a_2,\ldots,a_n\),这 \(m\) 个出口的位置是 \(b_1,b_2,\ldots,b_m\)。
你每次可以让所有机器人往左走一步或往右走一步。
当一个机器人所在的位置有一个出口时,这个机器人就会从这个出口出去。
问你有多少种让机器人全部离开的方案。
两种方案不同当且仅当有至少一个机器人从不同的出口出去。
\(n,m\leq 100000\)
题解
先把最左边的出口左边的机器人和最右边的出口右边的机器人还有已经在出口的机器人全部删掉。
对于一个机器人,设它到左边出口的距离为 \(a_i\),到右边出口的距离为 \(b_i\)。可以把它看成一个点 \((a_i,b_i)\)。
记 \(x\) 是所有机器人往左移动的最远点到初始位置的距离,\(y\) 是所有机器人往右移动的最远点到初始位置的距离。
那么每次可以选择把 \((x,y)\) 变成 \((x+1,y)\) 或者 \((x,y+1)\)。
当 \(x=a_i\) 时,第 \(i\) 个机器人会从左边的出口出去,当 \(y=b_i\) 时,机器人会从右边的出口出去。
那么可以看成从原点开始走,每次往上或右走一步。最后走的这条折线的上方和下方分别对应着从左边和右边的出口出去的机器人的集合。
那么考虑把折线往下移,变成这样:

那么一条折线可以用折线经过的点(那些涂黑的格子)来表示。
设 \(f_i\) 为最后一个经过的黑色格子是 \(i\) 的方案数。
\(f_i=1+\sum_{x_j<x_i,y_j<y_i}f_j\)
用树状数组维护。
时间复杂度: \(O(n\log n)\)
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<functional>
#include<cmath>
#include<vector>
//using namespace std;
using std::min;
using std::max;
using std::swap;
using std::sort;
using std::reverse;
using std::random_shuffle;
using std::lower_bound;
using std::upper_bound;
using std::unique;
using std::vector;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
int rd(){int s=0,c,b=0;while(((c=getchar())<'0'||c>'9')&&c!='-');if(c=='-'){c=getchar();b=1;}do{s=s*10+c-'0';}while((c=getchar())>='0'&&c<='9');return b?-s:s;}
void put(int x){if(!x){putchar('0');return;}static int c[20];int t=0;while(x){c[++t]=x%10;x/=10;}while(t)putchar(c[t--]+'0');}
int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;}
int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;}
const ll p=1000000007;
const int N=100010;
int cmp(pii a,pii b)
{
if(a.first!=b.first)
return a.first<b.first;
return a.second>b.second;
}
int n,m,t,t2;
int a[N],b[N],d[N];
pii c[N];
ll e[N];
ll add(ll a,ll b)
{
a+=b;
return a>=p?a-p:a;
}
void add(int x,ll v)
{
for(;x<=t2;x+=x&-x)
e[x]=add(e[x],v);
}
ll sum(int x)
{
ll s=0;
for(;x;x-=x&-x)
s=add(s,e[x]);
return s;
}
int main()
{
open("arc101f");
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
for(int i=1;i<=n;i++)
if(a[i]>b[1]&&a[i]<b[m])
{
int x=lower_bound(b+1,b+m+1,a[i])-b;
if(b[x]==a[i])
continue;
c[++t]=pii(a[i]-b[x-1],b[x]-a[i]);
d[++t2]=b[x]-a[i];
}
sort(d+1,d+t2+1);
t2=unique(d+1,d+t2+1)-d-1;
for(int i=1;i<=t;i++)
c[i].second=lower_bound(d+1,d+t2+1,c[i].second)-d;
sort(c+1,c+t+1,cmp);
t=unique(c+1,c+t+1)-c-1;
ll ans=1;
for(int i=1;i<=t;i++)
{
ll s=sum(c[i].second-1)+1;
ans+=s;
add(c[i].second,s);
}
ans%=p;
printf("%lld\n",ans);
return 0;
}
【ARC101F】Robots and Exits 树状数组的更多相关文章
- 【ARC101F】Robots and Exits 树状数组优化DP
ARC101F Robots and Exits 树状数组 有 $ n $ 个机器人和 $ m $ 个出口.这 $ n $ 个机器人的初始位置是 $ a_1,a_2.....a_n $ ,这 $ m ...
- 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)
[题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...
- 洛谷P2982 [USACO10FEB]慢下来Slowing down [2017年四月计划 树状数组01]
P2982 [USACO10FEB]慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) c ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
随机推荐
- 关于ipv6被拒的问题
遇到ipv6被拒,你首先要搭建一个ipv6的环境,进行测试一下,如果在ipv6环境下没有问题,那你就可以再次直接提交,或者重新打包提交.再次提交的时候,你可以录制一段在ipv6环境下运行的一段视频 上 ...
- sql左外连接和右外连接的区别例子转摘
sql左外连接和右外连接的区别 两个表:A(id,name)数据:(1,张三)(2,李四)(3,王五)B(id,name)数据:(1,学生)(2,老师)(4,校长) 左连接结果:select A. ...
- Mysql增量写入Hdfs(二) --Storm+hdfs的流式处理
一. 概述 上一篇我们介绍了如何将数据从mysql抛到kafka,这次我们就专注于利用storm将数据写入到hdfs的过程,由于storm写入hdfs的可定制东西有些多,我们先不从kafka读取,而先 ...
- python 第二百零八天 ----算法相关
查找方法 : 顺序查找法 二分查找法 import time,random #时间计算 def cal_time(func): def wrapper(*args,**kwargs) ...
- 【原】Java学习笔记006 - 流程控制
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:写一万次&q ...
- .NET CORE学习笔记系列(6)——KestrelServer
原文:http://www.cnblogs.com/artech/p/KestrelServer.html 跨平台是ASP.NET Core一个显著的特性,而KestrelServer是目前微软推出了 ...
- MySQL之库相关操作
一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...
- 【P2015】二叉苹果树 (树形DP分组背包)
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 现在这颗树枝条太多了,需要剪枝.但是 ...
- Linux和Uboot下eMMC boot分区读写
关键词:eMMC boot.PARTITION_CONFIG.force_ro等. 1. eMMC的分区 大部分eMMC都有类似如下的分区,其中BOOT.RPMB和UDA一般是默认存在的,gpp分区需 ...
- 即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?
今年 6 月份的时候时候 .NET 团队就在 GitHub 公布了 ASP.NET Core 2.2 版本的 Roadmap(文末有链接),而前两天 ASP.NET Core 2.2 预览版 2 已经 ...