题意:

有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\)

路有三种类型:

  • 从点\(v\)到点\(u\)需要花费\(w\)
  • 从点\(v\)到区间\([l,r]\)中的点花费为\(w\)
  • 从区间\([l,r]\)中的点到点\(v\)花费为\(w\)

求起点到各个点的最少花费

分析:

如下图,构建两颗线段树,边的花费都为\(0\)

对于第一种路直接加边即可

对于第二种路,添加从\(v\)到上面线段树对应区间中的点的边

对于第三种路,添加从下面线段树对应区间中的点到\(v\)的边



最后直接跑最短路

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
#define SZ(a) ((int)a.size())
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
typedef long long LL;
typedef pair<LL, int> PII; const int maxn = 100000 + 10; int n, q, s; struct Edge {
int v, w, nxt;
Edge() {}
Edge(int v, int w, int nxt): v(v), w(w), nxt(nxt) {}
}; int ecnt, head[maxn * 9];
Edge edges[maxn * 36]; void AddEdge(int u, int v, int w) {
printf("%d->%d, w = %d\n", u, v, w);
edges[ecnt] = Edge(v, w, head[u]);
head[u] = ecnt++;
} int tot, T[2][maxn << 2];
void build(int t, int o, int L, int R) {
T[t][o] = ++tot;
if(L == R) {
if(0 == t) AddEdge(T[t][o], L, 0);
else AddEdge(L, T[t][o], 0);
return;
}
int M = (L + R) / 2;
build(t, o<<1, L, M);
build(t, o<<1|1, M+1, R);
if(0 == t) {
AddEdge(T[t][o], T[t][o<<1], 0);
AddEdge(T[t][o], T[t][o<<1|1], 0);
} else {
AddEdge(T[t][o<<1], T[t][o], 0);
AddEdge(T[t][o<<1|1], T[t][o], 0);
}
} int type, v, qL, qR, w; void update(int o, int L, int R) {
if(qL <= L && R <= qR) {
if(0 == type) AddEdge(v, T[type][o], w);
else AddEdge(T[type][o], v, w);
return;
}
int M = (L + R) / 2;
if(qL <= M) update(o<<1, L, M);
if(qR > M) update(o<<1|1, M+1, R);
} const LL INF = 0x3f3f3f3f3f3f3f3f;
bool vis[maxn * 9];
LL d[maxn * 9]; priority_queue<PII, vector<PII>, greater<PII> > Q;
void dijkstra() {
memset(d, 0x3f, sizeof(d));
d[s] = 0;
Q.emplace(0, s);
while(!Q.empty()) {
PII t = Q.top(); Q.pop();
int& u = t.second;
if(vis[u]) continue;
vis[u] = true;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int& v = edges[i].v;
int& w = edges[i].w;
if(d[v] > d[u] + w) {
d[v] = d[u] + w;
Q.emplace(d[v], v);
}
}
}
} int main() {
scanf("%d%d%d", &n, &q, &s);
memset(head, -1, sizeof(head));
tot = n;
REP(t, 0, 2) build(t, 1, 1, n); while(q--) {
scanf("%d%d%d", &type, &v, &qL);
if(type == 1) {
scanf("%d", &w);
AddEdge(v, qL, w);
} else {
type -= 2;
scanf("%d%d", &qR, &w);
update(1, 1, n);
}
} dijkstra();
REP(i, 1, n + 1) printf("%lld ", d[i] == INF ? -1 : d[i]);
printf("\n"); return 0;
}

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.786B.Legacy(线段树优化建图 最短路Dijkstra)

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

  4. CodeForces - 786B Legacy (线段树+DIjkstra+思维)

    题意:给N个点和Q条选项,有三种类型的选项:1.从u到v花费w修建一条路:2.从u到下标区间为[L,R]的点花费w修建一条路; 3.从下标区间为[L,R]的点到u花费w修建一条路. 然后求起点s到其余 ...

  5. Codeforces 786B. Legacy 线段树+spfa

    题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...

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

    题意: 就是给定一张n nn个点的图,求源点s ss到每个点的单源最短路.这张图共有q组边,连边方式有3种: a→b ,边权为w的单向边:a→[l,r] ,即a到连续区间[l,r]中的每一个点都有一条 ...

  7. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  8. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  9. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

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

随机推荐

  1. php的yii框架开发总结10

    1.CActiveForm是Chtml类的封装,但是它有数据验证的功能,有三种方式:服务器端.客户端.Ajax数据验证. 服务器端验证:当整个表单页面被提交后,在服务器端 进行验证.如果存在任何验证错 ...

  2. Python中的Numpy、SciPy、MatPlotLib安装与配置

    Python安装完Numpy,SciPy和MatplotLib后,可以成为非常犀利的科研利器.网上关于这三个库的安装都写得非常不错,但是大部分人遇到的问题并不是如何安装,而是安装好后因为配置不当,在使 ...

  3. c++中explicit关键字用法

    C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢? 如果c++类 ...

  4. [转载]互联网 免费的WebService接口

    股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebServices/St ...

  5. C#自定义规则对比两个集合的对象是否相等

    IList<获取的类> ret = 类的结果集; return ret.Except(另一个相同类型的对象列表集, new AClassComPare()): public class A ...

  6. 【BZOJ2127】happiness(网络流)

    点此看题面 大致题意: 每个人只能在文科与理科中选择一种.选择每种科目会带来不同的喜悦值,如果相邻的两位同学选择了同一种科目则会带来额外的喜悦值.求喜悦值总和的最大值. 网络流 这道题做法显然是网络流 ...

  7. Uva 11384 正整数序列

    题目链接:https://vjudge.net/problem/UVA-11384 题意:给定正整数 n,用最少的操作把序列 1,2,,,n 全部变成 0: 操作是:每次可以从序列中选择一个或者多个, ...

  8. Uva 10820 交表

    题目链接:https://uva.onlinejudge.org/external/108/10820.pdf 题意: 对于两个整数 x,y,输出一个函数f(x,y),有个选手想交表,但是,表太大,需 ...

  9. 彩色图像直方图均衡(Histogram Equalization)

    直方图均衡(Histogram Equalization) 一般步骤: 1.统计直方图每个灰度级出现的次数(概率) 2.累计归一化的直方图 3.计算新的像素值 重要:彩色直方图均衡不能对RGB分别做再 ...

  10. 前端css样式及选择器

    标题: 1.scc概述 2.行内样式 3.内接样式 4.外接样式(链接式)    推荐使用 5.外接样式(导入式) 6.嵌套规则 7.css选择器 1.scc(Cascading Style Shee ...