题目链接:http://codeforces.com/contest/786/problem/B


  典型线段树优化连边,线段树上的每一个点表示这个区间的所有点,然后边数就被优化为了至多${nlogn}$条,注意要区分$2$,$3$操作(建两棵线段树),然后最短路即可。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1001000
#define llg long long
#define INC 300000
#define SIZE 2000000
#define inf (llg)1e18
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,dis[maxn],dl[SIZE+],head,tail,pos[maxn];
bool bj[maxn]; vector<llg>a[maxn],val[maxn]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} void link(llg x,llg y,llg z) {a[x].push_back(y),val[x].push_back(z);} void build(llg o,llg l,llg r)
{
if (l==r)
{
pos[l]=o;
return ;
}
llg mid=(l+r)>>,lc=o<<,rc=o<<|;
build(lc,l,mid); build(rc,mid+,r);
} void find(llg o,llg l,llg r,llg L,llg R,llg val,bool f,llg v)
{
if (l>=L && r<=R)
{
if (f) link(pos[v],o,val);else link(o+INC,pos[v],val);
return ;
}
llg mid=(l+r)>>,lc=o<<,rc=o<<|;
if (mid>=L) find(lc,l,mid,L,R,val,f,v);
if (mid<R) find(rc,mid+,r,L,R,val,f,v);
} void link_fa(llg o,llg l,llg r)
{
for (llg i=l;i<=r;i++) link(o,pos[i],),link(pos[i],o+INC,);
if (l==r) return ;
llg mid=(l+r)>>,lc=o<<,rc=o<<|;
link_fa(lc,l,mid); link_fa(rc,mid+,r);
} void init()
{
llg t,v,u,w,l,r,s,q;
n=getint(),q=getint(),s=getint();
build(,,n);
for (llg i=;i<=q;i++)
{
t=getint();
if (t==)
{
v=getint(),u=getint(),w=getint();
link(pos[v],pos[u],w);
}
if (t==)
{
v=getint(),l=getint(),r=getint(),w=getint();
find(,,n,l,r,w,,v);
}
if (t==)
{
v=getint(),l=getint(),r=getint(),w=getint();
find(,,n,l,r,w,,v);
}
}
link_fa(,,n);
for (llg i=;i<maxn;i++) dis[i]=inf;
dis[pos[s]]=;
dl[]=pos[s],head=,tail=;
} void spfa()
{
llg w,x,v;
do
{
head%=SIZE; head++;
x=dl[head]; w=a[x].size(); bj[x]=;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (dis[v]>dis[x]+val[x][i])
{
dis[v]=dis[x]+val[x][i];
if (!bj[v])
{
bj[v]=;
tail%=SIZE; tail++;
dl[tail]=v;
}
}
}
}while (head!=tail);
} int main()
{
yyj("graph");
init();
spfa();
for (llg i=;i<=n;i++) if (dis[pos[i]]==inf) printf("-1 "); else printf("%lld ",dis[pos[i]]);
return ;
}

Codeforces 786 B. Legacy的更多相关文章

  1. Codeforces 786 C. Till I Collapse

    题目链接:http://codeforces.com/contest/786/problem/C 大力膜了一发杜教的代码感觉十分的兹瓷啊! 我们知道如果$k$是给定的我们显然是可以直接一遍$O(n)$ ...

  2. Codeforces 786 A. Berzerk

    题目链接:http://codeforces.com/problemset/problem/786/A 这个题出做$DIV2$的$C$以及$DIV1$的A会不会难了一点啊... 做法和题解并不一样,只 ...

  3. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  4. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  5. Codeforces 787D. Legacy 线段树建模+最短路

    D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  6. CodeForces 786B Legacy(线段树优化建图+最短路)

    [题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...

  7. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

  8. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. How to diagnose vehicle fault code by BMW ICOM and ISTA-D software

    Today illustrate how to diagnose vehicle fault code by BMW diagnostic tool BMW ICOM and ISTA-D softw ...

  2. 常用linux命令行

    1.ls命令 ls -a 列出目录所有文件,包含以.开始的隐藏文件 ls -A 列出除.及..的其它文件 ls -r 反序排列 ls -t 以文件修改时间排序 ls -S 以文件大小排序 ls -h ...

  3. Django框架----在Python脚本中调用Django环境

    在项目根目录下新建脚本文件script.py import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTI ...

  4. Spring 注入的两种方式

    Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...

  5. pat 团体赛练习题集 L2-008. 最长对称子串

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...

  6. 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况

    在app.config中的configuration节内添加子节Startup,详细如下: <?xml version="1.0"?><configuration ...

  7. 推荐 Net C# 逆向反编译四大工具利器

    参考:https://blog.csdn.net/kongwei521/article/details/54927689/

  8. (6CBIR模拟问题)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

    个方面: 最初的图像检索研究主要集中在如何选择合适的全局特征去描述图像内容和采用什么样的相似性度量方法进行图像匹配. 第二个研究热点是基于区域的图像检索方法,其主要思想是图像分割技术提取出图像中的物体 ...

  9. 尚硅谷面试第一季-14Redis持久化类型及其区别

    课堂重点: Redis提供了两种不同形式的持久化方案,分别是RDB和AOF. RDB使用Snapshot快照做全量的存储. RDB优缺点: AOF 以日志的方式记录每个写操作,只最佳,不该写文件.增量 ...

  10. uniGUI试用笔记(六)

    uniGUI提供了一个文件上传控件TUniFileUpload,进行数据的导入就变得比较容易.首先将TUniFileUpload控件放置在窗体上,按下导入按钮后,执行TUniFileUpload的文件 ...