[HDU5361]In Touch
[HDU5361]In Touch
题目大意:
有\(n(n\le2\times10^5)\)个点,每个点有三个属性\(l_i,r_i,c_i\)。表示若\(|i-j|\in[l_i,r_i]\),\(i\)到\(j\)有一条长度为\(c_i\)的单向边。求从\(1\)出发到各个点的距离。
思路:
线段树优化建图后跑Dijkstra即可。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<climits>
#include<functional>
#include<ext/pb_ds/priority_queue.hpp>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=4e5+1;
typedef long long int64;
int tot,l[N],r[N],c[N],pos[N];
struct Edge {
int to,w;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &w) {
e[u].push_back((Edge){v,w});
}
inline void reset() {
for(register int i=1;i<=tot;i++) {
e[i].clear();
}
tot=0;
}
class SegmentTree {
#define mid ((b+e)>>1)
private:
struct Node {
int left,right;
};
Node node[N];
int new_node() {
node[++tot]=(Node){};
return tot;
}
public:
int root;
void build(int &p,const int &b,const int &e) {
p=new_node();
if(b==e) {
pos[b]=p;
return;
}
build(node[p].left,b,mid);
build(node[p].right,mid+1,e);
add_edge(p,node[p].left,0);
add_edge(p,node[p].right,0);
}
void link(const int &p,const int &b,const int &e,const int &l,const int &r,const int &x,const int &y) const {
if(l>r) return;
if(b==l&&e==r) {
add_edge(x,p,y);
return;
}
if(l<=mid) link(node[p].left,b,mid,l,std::min(mid,r),x,y);
if(r>mid) link(node[p].right,mid+1,e,std::max(mid+1,l),r,x,y);
}
#undef mid
};
SegmentTree sgt;
struct Vertex {
int id;
int64 dis;
bool operator > (const Vertex &rhs) const {
return dis>rhs.dis;
}
};
int64 dis[N];
__gnu_pbds::priority_queue<Vertex,std::greater<Vertex> > q;
__gnu_pbds::priority_queue<Vertex,std::greater<Vertex> >::point_iterator p[N];
inline void dijkstra() {
for(register int i=1;i<=tot;i++) {
p[i]=q.push((Vertex){i,dis[i]=i==pos[1]?0:LLONG_MAX});
}
while(!q.empty()&&q.top().dis!=LLONG_MAX) {
const int x=q.top().id;
q.pop();
for(register unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i].to,&w=e[x][i].w;
if(dis[x]+w<dis[y]) {
q.modify(p[y],(Vertex){y,dis[y]=dis[x]+w});
}
}
}
q.clear();
}
int main() {
for(register int T=getint();T;T--) {
const int n=getint();
sgt.build(sgt.root,1,n);
for(register int i=1;i<=n;i++) l[i]=getint();
for(register int i=1;i<=n;i++) r[i]=getint();
for(register int i=1;i<=n;i++) c[i]=getint();
for(register int i=1;i<=n;i++) {
sgt.link(1,1,n,std::max(1,i-r[i]),i-l[i],pos[i],c[i]);
sgt.link(1,1,n,i+l[i],std::min(i+r[i],n),pos[i],c[i]);
}
dijkstra();
for(register int i=1;i<=n;i++) {
printf("%lld%c",dis[pos[i]]!=LLONG_MAX?dis[pos[i]]:-1," \n"[i==n]);
}
reset();
}
return 0;
}
[HDU5361]In Touch的更多相关文章
- HDU5361 In Touch(线段树 + 最短路)
传送门 恰逢才做过VFK的A+B Problem,发现这道题也可以那样搞.区间连边的时候,我们就可以给那个区间在线段树对应的标号上连边. 线段树也可以不建出来,直接当做一个标号的合集,不占用内存,只用 ...
- 【HDU5361】In Touch
题意有n个人住在一条直线上,从左到右编号为1,2,3....n ...
- mkdir,rmdir,cp,rm,mv,cat,touch用法
一.mkdir新建目录 1.进入tmp目录,查看该目录下面的子目录 [root@localhost ~]# cd /tmp[root@localhost tmp]# lshsperfdata_root ...
- UC浏览器中touch事件的异常记录
以前也在UC上面栽过几个坑,不过都是页面显示方面的.上个周的时候,商品详情页重做,要添加个上拉显示详情的效果. 有两个条件需要判断: 1.是否到达底部: 2.到达底部之后拖动的y轴距离. 效果写完后, ...
- 移动端web开发,click touch tap区别
转自: http://blog.csdn.net/sly94/article/details/51701188 移动端用tap时会有穿透问题 一:click与tap比较 click与tap都会触发点击 ...
- 手机端html5触屏事件(touch事件)
touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...
- 移动端开发概览【webview和touch事件】
作为一个前端,而且作为一个做移动端开发的前端,那意味着你要有三头六臂,跟iOS开发哥哥一起打酱油,跟Android开发哥哥一起修bug... Android vs Ios 我在webkit内核的chr ...
- 手持设备点击响应速度,鼠标事件与touch事件的那些事
前言 现在一直在做移动端的开发,这次将单页应用的网页内嵌入了app,于是老大反映了一个问题:app应用点击响应慢!我开始不以为然,于是拿着网页版的试了试,好像确实有一定延迟,于是开始了研究,最后选择了 ...
- touch
Linux touch 命令 在 Linux 下运用 touch 命令创建一个空文件.当然我们也可以使用其他命令例如 vi, nano 或是任意一个编辑工具来实现.但是你可能需要更多的步骤来完成操 ...
随机推荐
- Elastic-Job-Lite分析——作业调度器 JobScheduler 的创建过程
-----------------------------------1. 创建注册中心的对象----------------------------------------------------- ...
- chattr的使用
让某个文件只能往里面追加内容,不能删除,一些日志文件适用于这种操作: chattr +a /home/caolei/.bash_history 查看lsattr /home/caolei/.bash_ ...
- openstack swift节点安装手册3-最后的安装配置及验证
以下步骤都在controller节点上执行 1.远程获取/etc/swift/swift.conf文件: curl -o /etc/swift/swift.conf https://git.opens ...
- Ubuntu Eclipse C++运行问题:launch failed.Binary not found
在Ubuntu下的Eclipse C++环境出现launch failed.Binary not found问题时,可采用如下解决方案: (1)首先检查系统中是否成功安装g++.如果console输出 ...
- db_recovery_file_dest_size
select name,space_limit,space_used,number_of_files from v$recovery_file_dest; alter system set db_re ...
- 判断Javascript变量是否为空 undefined 或者null(附样例)
1.变量申明未赋值 var type; //type 变量未赋值 1. type==undefined //true 2. type===undefined //true 3. typeof(type ...
- Clipboard深度实践与采坑记录
1.css禁止选择导致IOS无法复制 body{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: ...
- redhat5 设置静态ip
Last login: Sat Oct 14 16:19:13 2017 # 进入ip凭证文件设置地方 [root@oracle ~]# cd /etc/sysconfig/network-scrip ...
- Flask之数据库设置
4 数据库 知识点 Flask-SQLALchemy安装 连接数据库 使用数据库 数据库迁移 邮件扩展 4.1 数据库的设置 Web应用中普遍使用的是关系模型的数据库,关系型数据库把所有的数据都存储在 ...
- bzoj2243树链剖分+区间合并
树链上区间合并的问题比区间修改要复杂,因为每一条重链在线段树上分布一般都是不连续的,所以在进行链上操作时要手动将其合并起来,维护两个端点值 处理时的方向问题:lca->u是一个方向,lca-&g ...