题目链接: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. [转载]window.location.href的用法(动态输出跳转)

    无论在静态页面还是动态输出页面中window.location.href都是不错的用了跳转的实现方案   javascript中的location.href有很多种用法,主要如下. self.loca ...

  2. modelform添加属性

    暗暗啊

  3. DBeaver数据库管理工具连接Sybase数据库

    DBeaver数据库管理工具连接Sybase数据库 1. 下载DBeaver 官方网站:http://dbeaver.jkiss.org/ DBeaver5.3.1解压缩版(不用安装和配置):http ...

  4. Linux Centos 7.4 内核升级

    Linux Centos 7.4 内核升级 原始内核版本:3.10.0-693.2.2.el7.x86_64 升级内核版本:4.14.9-1.el7.elrepo.x86_64 1.导入key Key ...

  5. Nginx 容器教程

    春节前,我看到 Nginx 加入了 HTTP/2 的 server push 功能,就很想试一下. 正好这些天,我在学习 Docker,就想到可以用 Nginx 容器.万一哪里改乱了,直接删掉,再重启 ...

  6. U盘中病毒,文件消失或不显示

    最近非常流行的一个病毒,将电脑或者U盘里的文件全部用快捷方式替换,真实文件被隐藏起来,下面我们就具体了解下此种病毒吧,做好预防与杀毒工作. 一.病毒名称 病毒名称:移动盘同名文件夹病毒;文件夹EXE病 ...

  7. 线程同步——用户模式下线程同步——Slim读写锁实现线程同步

    //Slim读/写锁实现线程同步 SRWlock 的目的和关键段相同:对同一资源进行保护,不让其它线程访问. 但是,与关键段不同的是,SRWlock允许我们区分哪些想要读取资源的线程(读取者线程) 和 ...

  8. [c/c++] programming之路(5)、吓人小程序、变量、进制等

    一.设计一个吓人的东西 首先创建MFC项目(勾选“基于对话框”后点击完成即可) 添加三个按钮 双击按钮进入响应代码段 void CMFCWindowsDlg::OnBnClickedButton1() ...

  9. TensorFlow 自定义模型导出:将 .ckpt 格式转化为 .pb 格式

    本文承接上文 TensorFlow-slim 训练 CNN 分类模型(续),阐述通过 tf.contrib.slim 的函数 slim.learning.train 训练的模型,怎么通过人为的加入数据 ...

  10. 配置vim

    VundleVuldle是一个全自动的插件管理器,让我们通过维护插件列表的方式管理插件.它为安装.更新.删除插件提供了方便的命令.在安装Git的情况下(本文不赘述Git的安装),输入命令: git c ...