[Codeforces 787D] Legacy
[题目链接]
https://codeforces.com/contest/787/problem/D
[算法]
线段树优化建边 , 然后用Dijkstra算法求单源最短路
时间复杂度 : O((N + M)logN)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll inf = 1e15; struct node
{
int lc , rc;
} a[MAXN * ];
struct edge
{
int to , w , nxt;
} e[MAXN << ]; int tot , cnt , n , q , s;
int head[MAXN * ] , root[];
bool visited[MAXN * ];
ll dist[MAXN * ]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int x , int y , int w)
{
++cnt;
e[cnt] = (edge){y , w , head[x]};
head[x] = cnt;
}
inline void buildA(int &now , int l , int r)
{
if (l == r)
{
now = l;
return;
}
now = ++tot;
int mid = (l + r) >> ;
buildA(a[now].lc , l , mid);
buildA(a[now].rc , mid + , r);
addedge(now , a[now].lc , );
addedge(now , a[now].rc , );
}
inline void buildB(int &now , int l , int r)
{
if (l == r)
{
now = l;
return;
}
now = ++tot;
int mid = (l + r) >> ;
buildB(a[now].lc , l , mid);
buildB(a[now].rc , mid + , r);
addedge(a[now].lc , now , );
addedge(a[now].rc , now , );
}
inline void updateA(int now , int l , int r , int ql , int qr , int u , int w)
{
if (l == ql && r == qr)
{
addedge(u , now , w);
return;
}
int mid = (l + r) >> ;
if (mid >= qr) updateA(a[now].lc , l , mid , ql , qr , u , w);
else if (mid + <= ql) updateA(a[now].rc , mid + , r , ql , qr , u , w);
else
{
updateA(a[now].lc , l , mid , ql , mid , u , w);
updateA(a[now].rc , mid + , r , mid + , qr , u , w);
}
}
inline void updateB(int now , int l , int r , int ql , int qr , int u , int w)
{
if (l == ql && r == qr)
{
addedge(now , u , w);
return;
}
int mid = (l + r) >> ;
if (mid >= qr) updateB(a[now].lc , l , mid , ql , qr , u , w);
else if (mid + <= ql) updateB(a[now].rc , mid + , r , ql , qr , u , w);
else
{
updateB(a[now].lc , l , mid , ql , mid , u , w);
updateB(a[now].rc , mid + , r , mid + , qr , u , w);
}
}
inline void dijkstra(int s)
{
priority_queue< pair<ll , ll> , vector< pair<ll , ll> > , greater< pair<ll , ll> > > q;
dist[s] = ;
q.push(make_pair( , s));
while (!q.empty())
{
int u = q.top().second;
q.pop();
if (visited[u]) continue;
visited[u] = true;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (dist[u] + w < dist[v])
{
dist[v] = dist[u] + w;
q.push(make_pair(dist[v] , v));
}
}
}
} int main()
{ read(n); read(q); read(s);
tot = n;
buildA(root[] , , n);
buildB(root[] , , n);
while (q--)
{
int type;
read(type);
if (type == )
{
int u , v , w;
read(u); read(v); read(w);
addedge(u , v , w);
}
if (type == )
{
int l , r , v , w;
read(v); read(l); read(r); read(w);
updateA(root[] , , n , l , r , v , w);
}
if (type == )
{
int l , r , v , w;
read(v); read(l); read(r); read(w);
updateB(root[] , , n , l , r , v , w);
}
}
for (int i = ; i <= tot; i++) dist[i] = inf;
dijkstra(s);
for (int i = ; i <= n; i++) printf("%lld " , dist[i] == inf ? - : dist[i]); return ; }
[Codeforces 787D] Legacy的更多相关文章
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- codeforces 787D - Legacy 线段树优化建图,最短路
题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...
- Codeforces 787D Legacy 线段树 最短路
题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
- Codeforces Round #406 (Div. 2) 787-D. Legacy
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So ...
- CF 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 [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...
- Codeforces 786B Legacy(线段树优化建图)
题目链接 Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...
- Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)
题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...
随机推荐
- sublime的tab和spaces空格切换的坑
python是严格要求对齐或者叫缩进的: 使用sublime对python进行编程时,可以使用tab或者空格,但是不能混用.特别是从外面把代码拷贝进sublime的时候,更要注意是否一致. 简单介绍一 ...
- visual studio usage tips
reset all settings on visual stdio microsoft visual studio X\common7\ide\devenv.exe /setup /resetuse ...
- DICOM医学图像处理:Deconstructed PACS之Orthanc
背景: 此篇博文介绍一个开源的.基于WEB的DICOM Server软件.该开源软件完全使用C++编写,不依赖于第三方数据库(内置了SQLite数据库)或其他框架,支持RESTful API设计模式. ...
- from: 关于RabbitMQ
from: http://lynnkong.iteye.com/blog/1699684 1 什么是RabbitMQ? RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种, ...
- CTP报单状态 OrderStatus全部状态
- react 获取 input 的值
1.通过 onChange -- e.target.value class App extends Component { state = { username: '张三' }; // 用户名 get ...
- SpringBoot学习之@Controller和@RestController
今天我们来研究一下@Controller和@RestController的用法 @Controller 1.Controller可以用来跳转页面,并且必须配合模板来使用. @Controller // ...
- ANALYSIS AND EXPLOITATION OF A LINUX KERNEL VULNERABILITY (CVE-2016-0728)
ANALYSIS AND EXPLOITATION OF A LINUX KERNEL VULNERABILITY (CVE-2016-0728) By Perception Point Resear ...
- Hibernate中的事务处理流程详解
一.Hibernate操作的基本流程 使用 Hibernate 进行数据持久化操作,通常有如下步骤: 1.编写持久化类: POJO + 映射文件 2.获取 Configuration 对象 3.获取 ...
- Delphi快捷键大全
Delphi快捷键大全 在过程.函数.事件内部, SHIFT+CTRL+向上的方向键 可跳跃到相应的过程.函数.事件的定义.相反,在过程.函数.事件的定义处,SHIFT+CTRL+向下的方向键 可跳跃 ...