Codeforces 786 B. Legacy
题目链接: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的更多相关文章
- Codeforces 786 C. Till I Collapse
题目链接:http://codeforces.com/contest/786/problem/C 大力膜了一发杜教的代码感觉十分的兹瓷啊! 我们知道如果$k$是给定的我们显然是可以直接一遍$O(n)$ ...
- Codeforces 786 A. Berzerk
题目链接:http://codeforces.com/problemset/problem/786/A 这个题出做$DIV2$的$C$以及$DIV1$的A会不会难了一点啊... 做法和题解并不一样,只 ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- CodeForces 786B Legacy(线段树优化建图+最短路)
[题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
- 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 ...
- 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 ...
随机推荐
- Javascript 面向对象编程1:封装
Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,他又不是一种真正的面向对象编程语言,因为它的语法中没有class(类). 那么,如果我们要把& ...
- The Little Prince-12/05
The Little Prince-12/05 "When a mystery is too overpowering, one dare not disobey. Absurd as it ...
- 安装ubuntu18.04.1
下载ubuntu:https://www.ubuntu.com/download/desktop 在虚拟机创建好ubuntu18.04.1后无法启动(选择的是linux,ubuntu64位),提示:此 ...
- Docker学习笔记之了解 Docker 的核心组成
0x00 概述 在掌握 Docker 的一些背景知识后,我们还不得不花费一节的篇幅来简单介绍有关 Docker 核心的一些知识.当然,大家不要觉得有“核心”这类的词,我们就要在这一节中深入 Docke ...
- Java 中的多线程你只要看这一篇就够了
引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个 ...
- 11: python中的轻量级定时任务调度库:schedule
1.1 schedule 基本使用 1.schedule 介绍 1. 提到定时任务调度的时候,相信很多人会想到芹菜celery,要么就写个脚本塞到crontab中. 2. 不过,一个小的定时脚本,要用 ...
- amqp 抓包
1. wireshark 2. tcpick -yR -r file.name
- js获取对象的key
var obj = {"name":"名字","age":"18"};var temp = "";f ...
- Bugku-CTF之web基础$_GET
Day3 web基础$_GET http://123.206.87.240:8002/get/ 打开之后是一段代码
- vector.resize 与 vector.reserve的区别(转载)
转载:https://blog.csdn.net/shuilan0066/article/details/3588478 reserve是容器预留空间,但并不真正创建元素对象,在创建对象之前,不能引用 ...