# Legacy

## 题目描述

Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.
There are n planets in their universe numbered from $1$ to $n$. Rick is in planet number $s$ (the earth) and he doesn’t know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he’s still using its free trial.
By default he can not open any portal by this gun. There are $q$ plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.
Plans on the website have three types:

1. With a plan of this type you can open a portal from planet $v$ to planet $u$.
2. With a plan of this type you can open a portal from planet $v$ to any planet with index in range $[l, r]$.
3. With a plan of this type you can open a portal from any planet with index in range $[l, r]$ to planet $v$.

Rick doesn’t known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.

## 代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
struct node_type {
long long l, r, child[2];
} node[2000001];
struct edge {
long long v, w, nxt;
} e[2000001];
long long n, q, s, dist[2000001], tot, nume, h[2000001];
bool in[2000001];
queue<long long> que;
void add_edge(long long u, long long v, long long w) {
if (u == v) return;
e[++nume].v = v, e[nume].w = w, e[nume].nxt = h[u], h[u] = nume;
}
void build_tree(long long root, int mode) {
long long mid = node[root].l + node[root].r >> 1;
if (node[root].l == mid) node[root].child[0] = node[root].l;
else {
node[++tot].l = node[root].l, node[tot].r = mid;
node[root].child[0] = tot, build_tree(tot, mode);
}
if (mid + 1 == node[root].r) node[root].child[1] = node[root].r;
else {
node[++tot].l = mid + 1, node[tot].r = node[root].r;
node[root].child[1] = tot, build_tree(tot, mode);
}
if (mode) {
} else {
}
}
void insert_line(long long root, long long l, long long r, long long v, long long w, int mode) {
if (l == node[root].l && r == node[root].r) {
return;
}
if (r < node[node[root].child[1]].l) insert_line(node[root].child[0], l, r, v, w, mode);
else if (l > node[node[root].child[0]].r) insert_line(node[root].child[1], l, r, v, w, mode);
else {
insert_line(node[root].child[0], l, node[node[root].child[0]].r, v, w, mode);
insert_line(node[root].child[1], node[node[root].child[1]].l, r, v, w, mode);
}
}
void spfa() {
memset(dist, 0x1f, sizeof(dist));
while (!que.empty()) que.pop();
que.push(s), in[s] = true, dist[s] = 0;
while (!que.empty()) {
long long t = que.front(); que.pop(); in[t] = false;
for (long long i = h[t]; i; i = e[i].nxt) {
if (dist[e[i].v] > dist[t] + e[i].w) {
dist[e[i].v] = dist[t] + e[i].w;
if (!in[e[i].v]) que.push(e[i].v), in[e[i].v] = true;
}
}
}
}
int main()
{
scanf("%lld%lld%lld", &n, &q, &s);
for (int i = 1; i <= n; ++i) {
node[i].l = node[i].r = i;
}
tot = n + 2;
node[n + 1].l = node[n + 2].l = 1;
node[n + 1].r = node[n + 2].r = n;
if (n > 1) build_tree(n + 1, 0), build_tree(n + 2, 1);
for (long long i = 1, t, v, u, l, r, w; i <= q; ++i) {
scanf("%lld", &t);
if (t == 1) {
scanf("%lld%lld%lld", &v, &u, &w);
} else {
scanf("%lld%lld%lld%lld", &v, &l, &r, &w);
insert_line(n + t - 1, l, r, v, w, t - 2);
}
}
spfa();
for (int i = 1; i <= n; ++i) {
if (dist[i] != dist[0]) printf("%lld ", dist[i]);
else printf("-1 ");
}
printf("\n");
return 0;
}


418 I'm a teapot