[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的更多相关文章

  1. HDU5361 In Touch(线段树 + 最短路)

    传送门 恰逢才做过VFK的A+B Problem,发现这道题也可以那样搞.区间连边的时候,我们就可以给那个区间在线段树对应的标号上连边. 线段树也可以不建出来,直接当做一个标号的合集,不占用内存,只用 ...

  2. 【HDU5361】In Touch

    题意有n个人住在一条直线上,从左到右编号为1,2,3....n                                                                     ...

  3. mkdir,rmdir,cp,rm,mv,cat,touch用法

    一.mkdir新建目录 1.进入tmp目录,查看该目录下面的子目录 [root@localhost ~]# cd /tmp[root@localhost tmp]# lshsperfdata_root ...

  4. UC浏览器中touch事件的异常记录

    以前也在UC上面栽过几个坑,不过都是页面显示方面的.上个周的时候,商品详情页重做,要添加个上拉显示详情的效果. 有两个条件需要判断: 1.是否到达底部: 2.到达底部之后拖动的y轴距离. 效果写完后, ...

  5. 移动端web开发,click touch tap区别

    转自: http://blog.csdn.net/sly94/article/details/51701188 移动端用tap时会有穿透问题 一:click与tap比较 click与tap都会触发点击 ...

  6. 手机端html5触屏事件(touch事件)

    touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...

  7. 移动端开发概览【webview和touch事件】

    作为一个前端,而且作为一个做移动端开发的前端,那意味着你要有三头六臂,跟iOS开发哥哥一起打酱油,跟Android开发哥哥一起修bug... Android vs Ios 我在webkit内核的chr ...

  8. 手持设备点击响应速度,鼠标事件与touch事件的那些事

    前言 现在一直在做移动端的开发,这次将单页应用的网页内嵌入了app,于是老大反映了一个问题:app应用点击响应慢!我开始不以为然,于是拿着网页版的试了试,好像确实有一定延迟,于是开始了研究,最后选择了 ...

  9. touch

    Linux touch 命令   在 Linux 下运用 touch 命令创建一个空文件.当然我们也可以使用其他命令例如 vi, nano 或是任意一个编辑工具来实现.但是你可能需要更多的步骤来完成操 ...

随机推荐

  1. How to Repair GRUB2 When Ubuntu Won’t Boot

    Ubuntu and many other Linux distributions use the GRUB2 boot loader. If GRUB2 breaks—for example, if ...

  2. OA协同办公软件

    OA协同办公软件: 泛微软件. 九思软件. 华天动力. 万户OA.:北京万户网络技术有限公司创立于1998年2月,是北京华宇软件股份有限公司(股票简称:“华宇软件”,股票代码:300271)的全资子公 ...

  3. 如何用enable_shared_from_this 来得到指向自身的shared_ptr 及对enable_shared_from_this 的理解

    在看<Linux多线程服务端编程:使用muduo C++网络库> 的时候,在说到如何防止在将对象的 this 指针作为返回值返回给了调用者时可能会造成的 core dump.需使用 ena ...

  4. CentOS 6.5环境下heartbeat高可用集群的实现及工作原理详解

    Linux HA Cluster高可用服务器集群,所谓的高可用不是主机的高可用,而是服务的高可用. 什么叫高可用:一个服务器down掉的可能性多种多样,任何一个可能坏了都有可能带来风险,而服务器离线通 ...

  5. adb devices检测不到夜神模拟器

    1.dos下,cd进入到夜神模拟器的bin目录 代码: nox_adb connect 127.0.0.1:62001 2.dos下,进入进Android SDK下的platform-tools目录 ...

  6. 卓越的目标检测器Pelee

    Densenet的改良—PeleeNET Pelee: A Real-Time Object Detection System on Mobile Devices 论文地址:https://arxiv ...

  7. python接口自动化测试六:时间戳,防重复处理

    对于不可重复参数,可以用时间戳,防重复 import time title = '这是我的标题:%s'%str(int(time.time())) print(title) 转化成时间格式 t = t ...

  8. MVCJSONJQuery分页实现

    思路: 1.用Ado.NET获取数据 2.控制器中创建一个方法参数为搜索条件 3.返回前台一个Json对象,把对象用一个类封装 4.用JQuery接收数据 public ActionResult In ...

  9. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  10. Android Studio之导出JavaDoc出现编码GBK的不可映射字符

    使用Android Studio导出JavaDoc时,如果在注释中添加了中文,生成时的时候会出现错误: 编码GBK的不可映射字符. 解决的办法是在Other command line argument ...