[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\ ...
随机推荐
- PS 如何用PS制作GIF图像
首先我们准备好要依次播放的图片(这里使用的是CS的光标缩放,只有两张图) 然后在窗口中打开动画,则下方会出现动画的面板. 点击图层按钮可以添加一帧,我们让第一帧显示为大图片,第二帧为小图片.还可以设置 ...
- AMD单桥主板上电时序的详细解释
3个待机条件: 1.桥需要得到待机电压:3.3V,1.5V/1.2V2.25M起振注:NV的RTC电路,一般不会导致时序故障,都可以出CPURST#3.PWRGD-SB(即INTEL芯片组的RSMRS ...
- PA-RISC
http://baike.baidu.com/view/167703.htm PA-RISC处理器 编辑 HP(惠普)公司的RISC芯片PA-RISC于1986年问世. 第一款芯片的型号为PA-8 ...
- <转>关于 error LNK2019:无法解析的外部符号 ,该符号在函数**中被引用的思考
错误提示信息摘抄如下: ---------------------------------------------------------------------------------------- ...
- Android数据自己主动更新库DataAutoRefresh
非常多android应用.比方音乐播放器.视频播放器.小说阅读器或者其他须要获取本地磁盘指定数据格式数据列表的应用,在磁盘数据有变化(新增或者删除.比方下载完毕,拔TF卡.换TF卡)时.须要自己主动更 ...
- css实现弹出窗体始终垂直水平居中
<!DOCTYPE html><html> <head> <meta charset=" utf-8"> <meta name ...
- beifen---http://vdisk.weibo.com/s/uhCtnyUhD0Ooc
- 高性能MySQL(二)
MySQL基准测试 为什么需要benchmark 验证基于系统的假设,确认是否符合实际情况 重现系统中的某些异常行为,以解决它们 测试系统当前的运行情况,如果不清楚当前性能,就无法确认优化效果 模拟比 ...
- 图像处理之opencv---加减乘除运算cvdiv
http://chyyeng.blog.163.com/blog/static/16918230201211632135456/
- CentOS 安装和配置 Mantis
Mantis是一个基于PHP技术的轻量级的开源缺陷跟踪系统,以Web操作的形式提供项目管理及缺陷跟踪服务.在功能上.实用性上足以满足中小型项目的管理及跟踪.更重要的是其开源,不需要负担任何费用. 1. ...