[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. SIFT feature

    转载:http://www.cnblogs.com/wangguchangqing/p/4853263.html 1.SIFT概述 SIFT的全称是Scale Invariant Feature Tr ...

  2. Linux MMC framework2:基本组件之host

    声明:本文很多内容和思路参考了http://www.wowotech.net/comm/mmc_host_driver.html,对原作者表示感谢! 1.前言 本文是Linux MMC framewo ...

  3. Linux的capability深入分析(2)【转】

    转自:https://blog.csdn.net/wangpengqi/article/details/9821231 rpm -ql libcap-2.16-5.2.el6.i686  /lib/l ...

  4. linux添加定时任务crond

    1.crontab –e:编辑当前定时任务 保存完重新crond : service crond restart 2. crontab用法 crontab –e : 修改 crontab 文件,如果文 ...

  5. CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解

    环境说明: 192.168.8.39 node2.chinasoft.com 192.168.8.42 node4.chinasoft.com 192.168.8.40 ansible管理服务器 19 ...

  6. Android中PopupWindow用法

    参考资料链接:http://developer.android.com/reference/android/widget/PopupWindow.html 在Android中有很多级别的Window, ...

  7. Java上传文件FTP服务器代码

    1. 在实际的应用重,通常是通过程序来进行文件的上传. 2. 实现java上传文件到ftp服务器中 新建maven项目 添加依赖 <dependency> <groupId>c ...

  8. PYTHON-模块 sys os random shutil

    import sys # 环境变量# print(sys.path)# # 查看已经加载的模块# print(sys.modules)# # 获取终端调用时的参数# print(sys.argv)# ...

  9. 【splunk】仪表盘导入导出

    仪表盘导出: splunk目录/etc/users/admin/search/local/data/ui/views  目录下,拷贝所有的xml文件 导入: 创建仪表盘->编辑来源   将上面导 ...

  10. PHP 将数组的值赋值给一组变量

    经常需要将一个字符串分割成一组值,然后赋值给不同的变量. 逐行赋值非常繁琐,于是查了一下 PHP 中是否有类似 python 中 a, b = (a, b) 的操作. 果然有 $info = arra ...