给定n颗行星,q次处理,地球位置为s,求解在q次处理后,地球到每一颗行星的位置。

其中q有三种不同的操作:

  1. 输入v,u,wv,u,w,构建一条从vv到uu的代价为ww的路线

  2. 输入u,l,r,wu,l,r,w,构建一条从uu到区间[l,r][l,r]中任意一颗行星的代价为ww的路线

  3. 输入u,l,r,wu,l,r,w,构建区间[l,r]中任意一颗行星到uu的代价为ww的路线

建立两颗线段树,一颗记录操作2中其他点AOE到这些点的区间,一颗记录所有点单独到一个节点的路径,把线段树上的点单独作为一个节点来维护,偷了一个很好的图来表达

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#define For(i, x, y) for(int i=x; i<=y; i++)
#define Mem(f, x) memset(f, x, sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Pri(x) printf("%d\n", x)
#define CLR(u) for(int i = 0; i <= N ; i ++) u[i].clear();
#define LL long long
#define mp make_pair
#define PI pair<int,int>
#define PIL pair<int,long long>
#define PLI pair<long long,int>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef vector<int> VI;
const int maxn = 1e5 + ;
const int maxm = 3e5 + ;
const LL INF = 1e18 + ;
const int mod = 1e9 + ;
inline int read()
{
int now=;register char c=getchar();
for(;!isdigit(c);c=getchar());
for(;isdigit(c);now=now*+c-'',c=getchar());
return now;
}
struct Tree
{
int left,right;
int lr,rr;
}tree[maxm];
int N,M;
int Q,S;
int tot;
vector<PIL> P[maxm];
LL dis[maxm];
bool vis[maxm];
int Build(int left,int right,int flag)
{
if(left == right) return left;
int root = ++tot;
tree[root].left = left; tree[root].right = right;
int mid = (left + right) / ;
tree[root].lr = Build(left,mid,flag);
tree[root].rr = Build(mid + ,right,flag);
if(flag){
P[root].pb(mp(tree[root].lr,));
P[root].pb(mp(tree[root].rr,));
}else{
P[tree[root].lr].pb(mp(root,));
P[tree[root].rr].pb(mp(root,));
}
return root;
}
void update(int v,int l,int r,int root,int flag,LL w)
{
if(l == r){
if(flag) P[v].pb(mp(l,w));
else P[l].pb(mp(v,w));
return;
}
if(l <= tree[root].left && tree[root].right <= r)
{
if(flag) P[v].pb(mp(root,w));
else P[root].pb(mp(v,w));
return;
}
int mid = (tree[root].left + tree[root].right) >> ;
if(r <= mid) update(v,l,r,tree[root].lr,flag,w);
else if(l > mid) update(v,l,r,tree[root].rr,flag,w);
else{
update(v,l,mid,tree[root].lr,flag,w);
update(v,mid + ,r,tree[root].rr,flag,w);
} }
void Dijkstra(int start){
Mem(vis,);
for(int i = ; i <= tot; i ++){
dis[i] = INF;
}
dis[start] = ;
priority_queue<PLI,vector<PLI>,greater<PLI>>Q;
Q.push(mp(,start));
while(!Q.empty()){
PLI u = Q.top(); Q.pop();
if(vis[u.se]) continue;
vis[u.se] = ;
for(int j = ; j < P[u.se].size(); j ++){
PIL v = P[u.se][j];
if(!vis[v.fi] && dis[v.fi] > dis[u.se] + v.se){
dis[v.fi] = dis[u.se] + v.se;
Q.push(mp(dis[v.fi],v.fi));
}
}
}
} int main()
{
N = read(); Q = read(); S = read();
tot = N;
int L = Build(,N,);
int R = Build(,N,);
For(i,,Q){
int op = read() , v = read();
LL w;
if(op == ){
int u = read();
scanf("%lld",&w);
P[v].pb(mp(u,w));
}else if(op == ){
int l = read(); int r = read();
scanf("%lld",&w);
update(v,l,r,R,,w);
}else{
int l = read(); int r = read();
scanf("%lld",&w);
update(v,l,r,L,,w);
}
}
/* For(i,N + 1,tot){
printf("%d : %d %d\n",i,tree[i].left,tree[i].right);
}
For(i,1,tot){
printf("%d : ",i);
for(int j = 0 ; j < P[i].size(); j ++){
printf("%d ",P[i][j]);
}
printf("\n");
} */
Dijkstra(S);
For(i,,N){
if(dis[i] == INF) dis[i] = -;
printf("%lld ",dis[i]);
}
return ;
}

CodeForces786B 线段树 + 最短路的更多相关文章

  1. Vijos 1404 遭遇战 - 动态规划 - 线段树 - 最短路 - 堆

    背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC ...

  2. Codeforces787D(SummerTrainingDay06-D 线段树+最短路)

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

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

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

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

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

  5. HDU5361 In Touch(线段树 + 最短路)

    传送门 恰逢才做过VFK的A+B Problem,发现这道题也可以那样搞.区间连边的时候,我们就可以给那个区间在线段树对应的标号上连边. 线段树也可以不建出来,直接当做一个标号的合集,不占用内存,只用 ...

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

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

  7. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  8. HDU5669 Road 分层最短路+线段树建图

    分析:(官方题解) 首先考虑暴力,显然可以直接每次O(n^2) ​的连边,最后跑一次分层图最短路就行了. 然后我们考虑优化一下这个连边的过程 ,因为都是区间上的操作,所以能够很明显的想到利用线段树来维 ...

  9. 最短路 BZOJ3694 树链剖分+线段树

    分析: 树剖裸题,[Usaco2009 Jan]安全路经Travel 的简化版 剖开最短路树,遍历每一条没在最短路树上的边. 这种情况下,有且仅有u到v路径上,出来lca之外的点能够通过这条边到达,并 ...

随机推荐

  1. 软件工程驻足篇章:第十七周和BugPhobia团队漫长的道别

    0x01 :序言 I am a slow walker, but I never walk backwards. 成长于被爱,学着爱人 成长的故事 也是年少的星期六结束的故事 就仿佛我和BugPhob ...

  2. C#【结对编程作业】小学数学习题助手

    一.软件成品展示 软件本体下载(包括程序及其更新日志,源码工程包,UML图,API接口文档,算法介绍文档,算式计算excel实例,浅查重程序) 链接: http://pan.baidu.com/s/1 ...

  3. 软件工程导论课后习题Github作业(把一个英文句子中的单词次序逆序,单词中字母正常排列)

    Java源代码    package yly; import java.util.Scanner; public class ruanjian { public static void main(St ...

  4. APS.NET MVC4生成解析二维码简单Demo

    一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

  5. Review software requirements specification and create test scenarios (what to test for a certain functionality)

    1 srs2 what to test3 establish guidelines on how this deliverable is to be presented , the template4 ...

  6. Java之反射举例

    package reflection; import bean.User; public class ReflectionDemo { public static void main(String[] ...

  7. Hbase Shell 数据操作说明

    启动.关闭hbase./bin/start-hbase.sh./bin/stop-hbase.sh 查询HBase版本.它的语法如下:hbase(main):010:0> version 建表c ...

  8. SESSION和cookie的使用和区别

    PHP中SESSION和cookie的使用和区别 cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制. PHP在http协议的头信息里发送cookie, 因此 setcookie( ...

  9. unorder_map 自定义KEY

    1. boost::unorder_map 实现自定义KEY // boostLibTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...

  10. c# 获取变量名

    也不知道哪里需要用到.反正很多人问. 这里就贴一下方法,也是忘记从哪里看到的了,反正是转载的! public static void Main(string[] args) { string abc= ...