RT Code:
# include <bits/stdc++.h>
# define reg register
# define N 500000
# define INF 0x3f3f3f3f3f3f3f3f
# define Ls(K) (K<<1)
# define Rs(K) (K<<1|1)
# define Gc getchar()//(SS == TT && (TT = (SS = BB) + fread(BB,1,1 << 15,stdin),TT == SS) ? EOF : *SS++)
char BB[1<<15],*SS=BB,*TT=BB;
inline int Read()
{
register int x = 0;char ch = Gc;
while(ch < '0' || ch > '9')ch = Gc;
while(ch >= '0' && ch <= '9'){x = x*10+(ch^48);ch = Gc;}
return x;
}
struct edge
{
int Next,to,wi;
edge(const int Next_=0,const int To_=0,const int wi_=0):Next(Next_),to(To_),wi(wi_){}
} e[(N<<1) + 42];
int head[N + 42],edge_tot = 1;
inline void add_edge(const int u,const int v,const int wi=0)
{
e[++edge_tot] = edge(head[u],v,wi);
head[u] = edge_tot;
}
struct Seg
{
int Up,Down;
Seg(const int Up_=0,const int Down_=0):Up(Up_),Down(Down_){}
} t[N+5];
long long dis[N+5];
int n,s,tot;
void Build(const int L,const int R,const int K)
{
if(L == R) t[K] = Seg(L,L);
else
{
t[K] = Seg(++tot,++tot);
int mid = (L+R)>>1;
Build(L,mid,Ls(K));
Build(mid+1,R,Rs(K));
add_edge(t[K].Down,t[Ls(K)].Down);
add_edge(t[K].Down,t[Rs(K)].Down);
add_edge(t[Ls(K)].Up,t[K].Up);
add_edge(t[Rs(K)].Up,t[K].Up);
}
}
void Modify(const int AL,const int AR,const int x,const int opt,const int L,const int R,const int K)
{
if(AL <= L && R <= AR)
{
if(opt == 1) add_edge(x,t[K].Down);
else add_edge(t[K].Up,x);
}
else
{
int mid = (L+R)>>1;
if(AL <= mid) Modify(AL,AR,x,opt,L,mid,Ls(K));
if(AR > mid) Modify(AL,AR,x,opt,mid+1,R,Rs(K));
}
}
void Input()
{
n = Read();
tot = n = Read();
register int v,l,r,w,opt,q = Read();s = Read();
Build(1,n,1);
while(q--)
{
opt = Read();
if(opt == 1)
{
l = Read();r = Read();
add_edge(l,r,Read());
}
else if(opt == 2)
{
v = Read();l = Read();r = Read();++tot;
add_edge(v,tot,Read());
Modify(l,r,tot,1,1,n,1);
}
else
{
v = Read();l = Read();r = Read();++tot;
add_edge(tot,v,Read());
Modify(l,r,tot,2,1,n,1);
}
}
}
bool inq[N+5];
void Solve()
{
std::queue<int> q;
Input();
while(q.size()) q.pop();
for(int i = 1; i <= tot; ++i) dis[i] = INF;
q.push(s);inq[s] = 1;dis[s] = 0;
while(q.size())
{
int u = q.front();q.pop();inq[u] = 0;
for(int i = head[u]; i ; i = e[i].Next)
if(dis[e[i].to] > dis[u]+e[i].wi)
{
dis[e[i].to] = dis[u]+e[i].wi;
if(!inq[e[i].to]){inq[e[i].to] = 1;q.push(e[i].to);}
}
}
for(int i = 1; i <= n ; ++i) printf("%d ",dis[i] == INF ? -1 : dis[i]);
}
int main()
{
//freopen("ysqm.in","r",stdin);
Solve();
return 0;
}