【CF786B】Legacy
题目大意:初始给定 N 个点,支持三种操作:两点之间连边;一个点与一个连续区间编号的点之间连边;一个连续区间内的点和一个点连边,求执行 N 次操作之后的单源最短路。
题解:学会了线段树优化建图。
发现若暴力进行连边,时间和空间都会被卡到 \(O(n^2)\),直接起飞。
发现连边的点的编号是连续的,结合线段树可以维护连续区间信息的思想,就产生了线段树优化建图的方法。
在初始的 N 个节点的基础上建立两棵线段树,分别表示入树和出树,其中入树的上的各个节点允许单个节点的连接;出树上的节点允许连接单个节点。对于入树中的每个节点来说,需要连一条边权为 0 的有向边到它的两个儿子,表示若有节点连向父节点,那么一定也连向了儿子节点;对于出树上的每个节点来说,需要连一条边权为 0 的有向边到它的父节点,表示若当前节点可以连向某个节点,那么其子节点也一定可以走到这个节点。
时空复杂度分析:空间为两棵线段树的空间减去一份叶子节点的大小,即:\(3 * n\),时间复杂度为 \(elog^2v\),其中 e 是边的条数,v 是节点的个数。
代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct edge {
int to;
LL w;
edge(int x = -1, int y = -1) {
to = x, w = y;
}
};
struct segtree {
#define ls(o) tree[o].lc
#define rs(o) tree[o].rc
struct node {
int lc, rc;
};
vector<node> tree;
int tot, rootin, rootout;
segtree(int n, vector<vector<edge>> &adj) {
tot = n;
tree.resize(3 * n);
buildin(rootin, 1, n, adj);
buildout(rootout, 1, n, adj);
}
void buildin(int &o, int l, int r, vector<vector<edge>> &adj) {
if (l == r) {
o = l;
return;
}
o = ++tot;
int mid = l + r >> 1;
buildin(ls(o), l, mid, adj);
buildin(rs(o), mid + 1, r, adj);
adj[o].emplace_back(ls(o), 0);
adj[o].emplace_back(rs(o), 0);
}
void buildout(int &o, int l, int r, vector<vector<edge>> &adj) {
if (l == r) {
o = l;
return;
}
o = ++tot;
int mid = l + r >> 1;
buildout(ls(o), l, mid, adj);
buildout(rs(o), mid + 1, r, adj);
adj[ls(o)].emplace_back(o, 0);
adj[rs(o)].emplace_back(o, 0);
}
void link(int o, int l, int r, int x, int y, int u, int w, int opt, vector<vector<edge>> &adj) {// opt 2 -> in 3 -> out
if (l == x && r == y) {
opt == 2 ? adj[u].emplace_back(o, w) : adj[o].emplace_back(u, w);
return;
}
int mid = l + r >> 1;
if (y <= mid) {
link(ls(o), l, mid, x, y, u, w, opt, adj);
} else if (x > mid) {
link(rs(o), mid + 1, r, x, y, u, w, opt, adj);
} else {
link(ls(o), l, mid, x, mid, u, w, opt, adj);
link(rs(o), mid + 1, r, mid + 1, y, u, w, opt, adj);
}
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, s;
cin >> n >> m >> s;
vector<vector<edge>> adj(3 * n);
segtree t(n, adj);
while (m--) {
int opt;
cin >> opt;
if (opt == 1) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
} else {
int u, l, r, w;
cin >> u >> l >> r >> w;
t.link(opt == 2 ? t.rootin : t.rootout, 1, n, l, r, u, w, opt, adj);
}
}
vector<LL> d(3 * n, 1e18);
vector<bool> expand(3 * n);
priority_queue<pair<LL, int>> q;
auto dijkstra = [&]() {
d[s] = 0, q.push(make_pair(0, s));
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (expand[u] == 1) {
continue;
}
expand[u] = 1;
for (auto e : adj[u]) {
int v = e.to, w = e.w;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
q.push(make_pair(-d[v], v));
}
}
}
};
dijkstra();
for (int i = 1; i <= n; i++) {
if (d[i] == 1e18) {
cout << "-1" << " ";
} else {
cout << d[i] << " ";
}
}
return 0;
}
【CF786B】Legacy的更多相关文章
- 【翻译】Sencha Touch2.4 The Layout System 布局
[翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...
- 【转】在RedHat上搭建自己Email服务器
原文:http://6839976.blog.51cto.com/6829976/1323482 by LN__@linux 目前邮件服务器中,想要拥有自己的邮件服务器,单单使用senmail,pos ...
- 在前端页面中使用@font-face来显示web自定义字体【转】
本文转自W3CPLUS 的<CSS @font-face> @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现, ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) Processors
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) source
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
- 【教程】在UEFI启动方式下,通过GRUB2引导,直接从硬盘ISO文件安装Windows10和Ubuntu双系统
本文为作者原创,允许转载,但必须注明原文地址: https://www.cnblogs.com/byronxie/p/9949789.html 动机 最近在自学MIT6.828 Operating S ...
- 【转】PEP8 规范
[转]PEP8 规范 Python PEP8 编码规范中文版 原文链接:http://legacy.python.org/dev/peps/pep-0008/ item detail PEP 8 ...
- 【转】CSS3属性 @font-face 整理
原文: http://www.w3cplus.com/content/css3-font-face 出自: w3cplus.com 一.语法规则 @font-face { font-family: & ...
- 【Java】-NO.20.Exam.1.Java.1.001- 【1z0-807】- OCEA
1.0.0 Summary Tittle:[Java]-NO.20.Exam.1.Java.1.001-[1z0-807] Style:EBook Series:Java Since:2017-10- ...
随机推荐
- tp5 ThinkPHP5 自定义异常处理类
在项目的开发过程中异常抛出尤为重要不仅能够做出友好提示帮助掩盖我们伟大的程序员们尴尬的瞬间,还能做到提示开发人员代码白编写的错误,下面进行自定义异常抛出类,纯属个人理解,希望大家指正 首先在框架中我们 ...
- 如何禁止谷歌浏览器隐藏url的www前缀
若要将Chrome浏览器的设置恢复为隐藏HTTP.HTTPS以及WWW前缀,则只需再次进入此页面: chrome://flags/#omnibox-ui-hide-steady-state-url-s ...
- XSS练习平台- https://alf.nu/alert1
https://alf.nu/alert1 参考:https://www.cnblogs.com/renzongxian/p/5617551.html 我目前的进度:https://alf.n ...
- IT架构的本质
老僧三十年前未参禅时,见山是山,见水是水. 及至后来,亲见知识,有个入出,见山不是山,见水不是水. 而今得个休歇处,依前见山只是山,见水只是水. 参禅的三重境界在IT技术圈同样适用,初学者感叹每个产品 ...
- arm-linux的gdb移植
转载于:http://blog.chinaunix.net/uid-23381466-id-309369.html arm-linux的gdb移植分为两种情况.一种是交叉调试版.这一种模式是需要编译一 ...
- Spring之一:IoC容器体系结构
温故而知心. Spring IoC概述 常说spring的控制反转(依赖反转),看看维基百科的解释: 如果合作对象的引用或依赖关系的管理要由具体对象来完成,会导致代码的高度耦合和可测试性降低,这对复杂 ...
- Javascript - BOM 对象
浏览器相关的对象.获取浏览器相关的信息,可以设置和修改浏览器属性. 0. web-app 版 TodoList 小程序 用以下内容可以自己手写一个 TodoList 小程序,再添加几行代码就可以用手机 ...
- Web前端开发CSS基础
CSS 层叠样式表(英文全称:Cascading Style Sheets),是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS不 ...
- word、ppt转换为pdf
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 打包JavaFX11桌面应用程序
打包JavaFX11桌面应用程序 这是JavaFX系列的第二弹,第一弹在这里 在第一弹中,我们使用的是OpenJDK8,但是OpenJDK8和Oracle Java JDK不一样,它没有内置JavaF ...