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.


有$n$个点以及$q$条边。边有$3$种类型:①从点$v$到点$u$;②从点$v$到编号在$[l, r]$之间的点;③从编号在$[l, r]$之间的点到点$v$。每条边都有一个权值$w$。求从点$s$出发到其他所有点的最短路径长度。
数据范围:$1 \le n, q \le 10^5, \; 1 \le w \le 10^9$。


Two Segment Trees
如图,红色边表示线段树中的边。对于输入的每一条边,在这张图中建立相对应的边:①直接在最中间一层连边;②从最中间一层的点向上面的线段树连边;③从下面的线段树向最中间一层的点连边。最后跑一遍最短路即可。显然,点的数目为$O(n)$,边的数目为$O(n\log n)$,因此可以AC。


#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) {
        add_edge(node[root].child[0], root, 0);
        add_edge(node[root].child[1], root, 0);
    } else {
        add_edge(root, node[root].child[0], 0);
        add_edge(root, node[root].child[1], 0);
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) {
        if (!mode) add_edge(v, root, w);
        else add_edge(root, v, w);
    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);
            add_edge(v, u, w);
        } else {
            scanf("%lld%lld%lld%lld", &v, &l, &r, &w);
            insert_line(n + t - 1, l, r, v, w, t - 2);
    for (int i = 1; i <= n; ++i) {
        if (dist[i] != dist[0]) printf("%lld ", dist[i]);
        else printf("-1 ");
    return 0;

RegMs If

418 I'm a teapot

Leave a Reply

Your email address will not be published. Required fields are marked *