[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\ ...
随机推荐
- activiti自己定义流程之整合(二):使用angular js整合ueditor创建表单
基础环境搭建完成,接下来就该正式着手代码编写了,在说代码之前.我认为有必要先说明一下activit自己定义流程的操作. 抛开自己定义的表单不谈.通过之前的了解,我们知道一个新的流程開始.是在启动流程实 ...
- SharpSSH
SharpSSH sharpssh is a pure .NET implementation of the SSH2 client protocol suite. It provides an AP ...
- vue Syntax Error: Unexpected token {
> music@1.0.0 dev F:\music\music> node build/dev-server.js > Starting dev server...ERROR Fa ...
- HDU 4403 A very hard Aoshu problem (DFS暴力)
题意:给你一个数字字符串.问在字符串中间加'='.'+'使得'='左右两边相等. 1212 : 1+2=1+2, 12=12. 12345666 : 12+3+45+6=66. 1+2+3+4 ...
- react 调用 function 的写法 及 解决 react onClick 方法自动执行
1.react 调用方法的写法 (1)方式一 onClick={this.getFetchData.bind(this,item.id)} (2)方式二 getFetchData(e){ this.s ...
- 【转载】C#中的==、Equal、ReferenceEqual
1. ReferenceEquals, == , Equals Equals , == , ReferenceEquals都可以用于判断两个对象的个体是不是相等. a) ReferenceEquals ...
- Laravel 5.4建站06--API 认证系统 Passport
介绍 在 Laravel 中,实现基于传统表单的登陆和授权已经非常简单,但是如何满足 API 场景下的授权需求呢?在 API 场景里通常通过令牌来实现用户授权,而非维护请求之间的 Session 状态 ...
- linux查看进程、端口
1 查看进程pidps -ef|grep tomcat 2 查看进程占用的端口netstat -ntlp|grep pid 3 查看端口对应的进程号lsof -i:portid
- CSDN第一期总结之三:Thread的问题(转)
C#是一门支持多线程的语言,因此线程的使用也是比较常见的.由于线程的知识在Win32编程的时候已经说得过多,所以在.Net中很少介绍这部分(可能.Net不觉得这部分是它所特有的). 那么线程相关的问题 ...
- EasyPlayer安卓Android流媒体播放器实现直播过程中客户端快照功能
本文转自:http://blog.csdn.net/jyt0551/article/details/56942795 对于一个裸的RTSP URL,存放在播放列表上略显单调与枯燥.大家可以看到Easy ...