[题目链接]

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的更多相关文章

  1. Codeforces 787D. Legacy 线段树建模+最短路

    D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  2. 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 最 ...

  3. Codeforces 787D Legacy 线段树 最短路

    题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...

  4. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

  5. 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 ...

  6. CF 787D Legacy(线段树思想构图+最短路)

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  7. CodeForces 786B Legacy(线段树优化建图+最短路)

    [题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...

  8. Codeforces 786B Legacy(线段树优化建图)

    题目链接  Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...

  9. Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)

    题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...

随机推荐

  1. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 远程Event Receivers App级别生命周期

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers  远程Event Receivers App级别生命周期 ...

  2. 并行程序设计---cuda memory

    CUDA存储器模型: GPU片内:register,shared memory: host 内存: host memory, pinned memory. 板载显存:local memory,cons ...

  3. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  4. python(2)- python程序的编写简单介绍

    一.语句和语法 # 注释 \ 转译回车,继续上一行,在一行语句较长的情况下可以使用其来切分成多行,因其可读性差所以不建议使用 : 将两个语句连接到一行,可读性差,不建议使用 : 将代码的头和体分开 语 ...

  5. eclipse的快捷键(常用)

    1. Ctrl+O 显示类中方法和属性的大纲,能快速定位类的方法和属性,在查找Bug时非常有用. 2. Ctrl+M 窗口最大化和还原,用户在窗口中进行操作时,总会觉得当前窗口小(尤其在编写代码时), ...

  6. 根据ip识别地区

    <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></scri ...

  7. centos6.4中文输入法安装和切换(转载)

    1.用root登录,或者切换到root账户(su root): 2.yum install "@Chinese Support"; 3.exit: 4.System→prefere ...

  8. 网络协议之rtp---h264的rtp网络协议实现

    完整的C/S架构的基于RTP/RTCP的H.264视频传输方案.此方案中,在服务器端和客户端分别进行了功能模块设计.服务器端:RTP封装模块主要是对H.264码流进行打包封装:RTCP分析模块负责产牛 ...

  9. 程序基石系列之C++多态的前提条件

    准备知识 C++中多态(polymorphism)有下面三个前提条件: 必须存在一个继承体系结构. 继承体系结构中的一些类必须具有同名的virtual成员函数(virtualkeyword) 至少有一 ...

  10. HUD 2031: 进制转换

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...