There are n soda living in a straight line. soda are numbered by 1,2,…,n1,2,…,n from left to right. The distance between two adjacent soda is 1 meter. Every soda has a teleporter. The teleporter of ii-th soda can teleport to the soda whose distance between ii-th soda is no less than lili and no larger than riri. The cost to use ii-th soda's teleporter is cici.

The 11-st soda is their leader and he wants to know the minimum cost needed to reach ii-th soda (1≤i≤n)(1≤i≤n).

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤2×105)(1≤n≤2×105), the number of soda. 
The second line contains nn integers l1,l2,…,lnl1,l2,…,ln. The third line contains nn integers r1,r2,…,rnr1,r2,…,rn. The fourth line contains nn integers c1,c2,…,cnc1,c2,…,cn. (0≤li≤ri≤n,1≤ci≤109)(0≤li≤ri≤n,1≤ci≤109)

Output

For each case, output nn integers where ii-th integer denotes the minimum cost needed to reach ii-th soda. If 11-st soda cannot reach ii-the soda, you should just output -1.

Sample Input

1
5
2 0 0 0 1
3 1 1 0 5
1 1 1 1 1

Sample Output

0 2 1 1 -1 

Hint

If you need a larger stack size,
please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.

其实每个点只更新一次就可以

但是由于我们每次更新都要遍历一个范围

因此我们就不能每次都暴力的跑出更新的值

所以我们就想到使用并查集的方法进行优化

用并查集跑数组的好处就是可跳过没必要的点

从而降低时间复杂度

#define LOCAL
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int l[200005],r[200005];
long long c[200005];
int father[200005];
int n;
long long dis[200005];
void intal()
{for(int i=0;i<=n+5;i++) father[i]=i;
}
struct node
{int id;long long num;node (int p,long long q): id(p),num(q){}friend bool operator < (node p,node q){return p.num>q.num;}
};
int fin(int x)
{if(x==father[x]) return x;return father[x]=fin(father[x]);
}
void dijk()
{memset(dis,0x3f,sizeof(dis));priority_queue<node>q;dis[1]=0;father[1]=2;q.push(node(1,c[1]));while(q.size()){//cout<<q.size()<<endl;int tmp1=q.top().id;long long tmp2=q.top().num;q.pop();for(int i=1;i>=-1;i-=2){int lf,rt;lf=tmp1+i*l[tmp1];rt=tmp1+i*r[tmp1];lf=max(0,lf),lf=min(n+1,lf);rt=max(0,rt),rt=min(n+1,rt);if(lf>rt) swap(lf,rt);for(int j=lf;j<=rt;j++){//cout<<"lf"<<lf<<"rt"<<rt<<j<<" "<<fin(j)<<father[2]<<endl;j=fin(j);if(j>rt||j<0||j>n) break;dis[j]=tmp2;father[j]=fin(j+1);q.push(node(j,tmp2+c[j]));}//cout<<"wdnmd"<<endl;}}
}
int main()
{#ifdef LOCALfreopen("in.txt","r",stdin);#endifint t;  scanf("%d",&t);while(t--){scanf("%d",&n);intal();for(int i=1;i<=n;i++) scanf("%d",&l[i]);for(int i=1;i<=n;i++) scanf("%d",&r[i]);for(int i=1;i<=n;i++) scanf("%lld",&c[i]);dijk();for(int i=1;i<=n;i++){if(dis[i]>1e18) printf("-1%c",i==n?'\n':' ');else printf("%lld%c",dis[i],i==n?'\n':' ');}}
}

In Touch(dijk+并查集优化)相关推荐

  1. BZOJ 2143 飞飞侠(线段树优化建边 / 并查集优化最短路)【BZOJ修复工程】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 题目链接 https://hydro.ac/d/bzoj/p/2143 是 hydro 的 BZOJ ...

  2. 【割边缩点】解题报告:POJ - 3694 - Network(Tarjan割边缩点 + LCA + 并查集优化)

    POJ - 3694 - Network 给定一张N个点M条边的无向连通图,然后执行Q次操作,每次向图中添加一条边,并且询问当前无向图中"桥"的数量.N≤105,M≤2∗105,Q ...

  3. POJ - 3694 Network(边双缩点+LCA+并查集优化)

    题目链接:点击查看 题目大意:给出一个由n个点组成的无向图,现在有m次操作,每次操作都会向图中增加一条无向边,每次操作后询问当前图中有多少个桥 题目分析:题意很好理解,思路也很好想,就是代码量有点小多 ...

  4. 【BZOJ2342】双倍回文,manacher+并查集优化

    Time:2016.08.16 Author:xiaoyimi 转载注明出处谢谢 传送门 思路: woc第一次写马拉车怎么这么蛋疼啊 p[i]表示以i为中点,能向增长的最多的回文长度的一半 比如&qu ...

  5. 并查集及其简单应用:优化kruskal算法

    并查集是一种可以在较短的时间内进行集合的查找与合并的树形数据结构 每次合并只需将两棵树的根合并即可 通过路径压缩减小每颗树的深度可以使查找祖先的速度加快不少 代码如下: int getfather(i ...

  6. 一笔画问题 (DFS或并查集)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=42 一笔画问题是欧拉回路的一个变形,可以一笔画的条件有两个:1.所有顶点必须连通  2. ...

  7. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. 1 #include <iostream> 2 #include <algori ...

  8. POJ1456贪心(set或者并查集区间合并)

    题意:       给你n商品,每个商品有自己的价值还有保质期,一天最多只能卖出去一个商品,问最大收益是多少? 思路:       比较好想的贪心,思路是这样,每一次我们肯定拿价值最大的,至于在那天拿 ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的"谜之战舰"带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼 ...

最新文章

  1. linux用户变量设置位置,linux---位置参数
  2. ASP绕过防注入的新思路
  3. tensorflow tf.py_func
  4. python第二大奇数_python-2.7 – matplotlib,包含奇数个子图
  5. 定义派生类和继承类(从汇编分析)
  6. mysql 5.7 引擎_MySQL 5.7 学习:功能性能的提升
  7. Python内置函数功能汇总
  8. jquery学习之-查找父元素方法parent() parents() closest()的区别
  9. lt;备份gt;10月12日 内核编译与日志
  10. Button with Round Progress
  11. 07. 如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉
  12. atitit 面试问题表 侧重于项目和业务描述方面.v2 良好的标准:: 1.回答问题比较流畅,较少出现停顿现象,较少犹豫 2.回答有条理清晰 不杂乱 3.回答较为丰富内容 4.精神状态紧张
  13. 项目(1)——文件压缩
  14. Python随机生成6位数密码
  15. 欧洲对地观测卫星和无人机发展现状简介
  16. 注意力(Attention)
  17. Angular 依赖注入框架里 useExisting 和 useClass 的使用场景
  18. 在Mac上使用android studio调试android手机
  19. Oracle 数据库中的 时间 时区
  20. 用Win32DiskImager写入U盘容量变小,恢复容量方法

热门文章

  1. JavaWeb环境配置、虚拟路径、虚拟主机
  2. 【设计模式】C++/C#/Java实现Memento模式
  3. 七彩虹主板CVN系列首发 这个主板有点冷
  4. 如何查看自己的Eigen版本
  5. 计算机开机总显示密码错误如何解决,电脑密码错误
  6. could not resolve package...
  7. 完美世界3D格斗手游[格斗宝贝]今日公測
  8. Word2010中搜狗拼音输入法(各种输入法)消失了的解决办法
  9. 不一样角度带您了解 Flutter 中的滑动列表实现 | 开发者说·DTalk
  10. 虚函数,虚表深度剖析