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 ...
随机推荐
- AtCoder Beginner Contest 044 A - 高橋君とホテルイージー / Tak and Hotels (ABC Edit)
Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There is a hotel with ...
- Symfony2 UserSecurityEncoder实现自己的验证方式
fosuserbundle默认使用sha512加密 如果要实现自己的加密方式 需要继承Symfony\Component\Security\Core\Encoder\BasePasswordEncod ...
- SSM的理解
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架.其中spring是一个轻量 ...
- Linux 安装 mysql 数据库
1. 克隆虚拟机 2. 上传安装文件 1.上传文件 2.解压文件 tar -xvf 文件 3. 安装数据库 安装顺序: .debuginfo .shared .client .server 1. rp ...
- Java精选面试题之Spring Boot 三十三问
Spring Boot Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一: Spring Boot.Spring MVC 和 ...
- sqlalchemy 和 django 插入操作后自动返回自增ID
result = session.execute('insert into ***') session.commit() last_insert_id = result.lastrowid 注意:如果 ...
- 01: flask基础
1.1 flask介绍 参考博客: https://www.cnblogs.com/sss4/p/8097653.html 1.django.tornado.flask比较 1. Django:1 ...
- 网络存储结构简明分析—DAS、NAS和SAN 三者区别
存储的总体分类 主流存储结构 网络存储结构大致分为三种:直连式存储(DAS:Direct Attached Storage).存储区域网络(SAN:Storage Area Network ...
- CF767C 记录错误
链接 https://codeforces.com/contest/767/problem/C 思路 之所以把这个题放进来,是因为要记录错误 情况不止一种 所以答案存储就是>=2了 代码 #in ...
- 【做题】POJ3469 Dual Core CPU——第一道网络流
刚学了Dinic就开始做题,然后就崩了. 题意:若干个任务,可以放在两个CPU中任意一个上完成,各有一定代价.其中又有若干对任务,如果它们不在同一个CPU上完成,会产生额外代价.最小化并输出代价. 一 ...