1. 程式人生 > >codeforces 786B Legacy 線段樹建圖最短路

codeforces 786B Legacy 線段樹建圖最短路

B. Legacy

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line of input contains three integers nq and s (1 ≤ n, q ≤ 105, 1 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.

The next q lines contain the plans. Each line starts with a number t, type of that plan (1 ≤ t ≤ 3). If t = 1 then it is followed by three integers vu and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers vlr and w where w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).

Output

In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or  - 1 if it's impossible to get to that planet.

Examples

input

Copy

3 5 1
2 3 2 3 17
2 3 2 2 16
2 2 2 3 3
3 3 1 1 12
1 3 3 17

output

Copy

0 28 12 

input

Copy

4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16

output

Copy

0 -1 -1 12 

Note

In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.

題目大意:n個點,有三種操作

1. u v w 點u向點v連權重為w的邊

2. u l r w 點u向區間[l, r]的點均連一條權重為w的邊

3. u l r w 區間[l, r]的點均連一條權重為w的邊到u

建立兩棵線段樹,第一棵線段樹的父節點向子節點連線權重為0的邊,葉結點向圖中節點連權重為0的邊,另一棵子節點向父節點連權重為0的邊,圖中節點向葉子節點連權重為0的邊,對於第二種操作,則只需u向第一棵線段樹中[l, r]區間對應的點連邊即可,因此每次操作將o(n)的邊降為了o(1)。圖片來自這裡

#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
#define INF 200000000000000
typedef  long long ll;
const int maxn = 6e5 + 10;
int n, q, s;
int rts, num[2][maxn], vis[maxn];
ll dist[maxn];
vector<pair<int, ll> > G[maxn];

void build(int l, int r, int o, int x) {
    rts++;
    num[x][o] = rts;
    if (l == r) {
        if (x == 0) G[num[x][o]].push_back({l, 0});
        else G[l].push_back({num[x][o], 0});
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, o * 2, x);
    build(mid + 1, r, o * 2 + 1, x);
    if (x == 0) {
        G[num[x][o]].push_back({num[x][o * 2], 0});
        G[num[x][o]].push_back({num[x][o * 2 + 1], 0});
    }
    else {
        G[num[x][o * 2]].push_back({num[x][o], 0});
        G[num[x][o * 2 + 1]].push_back({num[x][o], 0});
    }
}

void update(int fr, int L, int R, int l, int r, int o, int x, ll w) {
    if (L <= l && r <= R) {
        if (x == 0) {
            G[fr].push_back({num[x][o], w});
        }
        else {
            G[num[x][o]].push_back({fr, w});
        }
        return;
    }
    int mid = (l + r) / 2;
    if (L <= mid) update(fr, L, R, l, mid, o * 2, x, w);
    if (R > mid) update(fr, L, R, mid + 1, r, o * 2 + 1, x, w);
}

void spfa() {
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    dist[s] = 0;
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].first;
            ll w = G[u][i].second;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                if (!vis[v]) {
                    Q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}

int main() {
    scanf("%d%d%d", &n, &q, &s);
    rts = n;
    memset(vis, 0, sizeof(vis));
    build(1, n, 1, 0);
    build(1, n, 1, 1);
    for (int i = 1; i <= rts; i++) dist[i] = INF;
    int t, u, v, l, r, w;
    for (int i = 0; i < q; i++) {
        scanf("%d", &t);
        if (t == 1) {
            scanf("%d%d%I64d", &u, &v, &w);
            G[u].push_back({v, w});
        }
        else if (t == 2) {
            scanf("%d%d%d%I64d", &u, &l, &r, &w);
            update(u, l, r, 1, n, 1, 0, w);
        }
        else {
            scanf("%d%d%d%I64d", &u, &l, &r, &w);
            update(u, l, r, 1, n, 1, 1, w);
        }
    }
    spfa();
    for (int i = 1; i <= n; i++)
        if (dist[i] == INF) printf("-1 ");
        else printf("%I64d ", dist[i]);
    return 0;
}