Codeforces 787D. Legacy segment tree optimization and shortest circuit

Source: Internet
Author: User

Outputstandard output

Rick and his co-workers has made a new radioactive formula and a lot of bad guys is after them. So Rick wants to give he legacy to Morty before bad guys catch them.

There is 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 the This gun the can open one-way portal from a planet he was in to any other planet (including that planet). But there is limits on the this gun because he's still using its-free trial.

By default he can not open any portal by this gun. There is Q plans in the website that sells these guns. Every time purchase a plan you can have only use it once and can purchase it again if you want the IT more.

Plans on the website has three types:

    1. With a plan of the this type you can open a portal from Planet v to Planet u.
    2. With a plan of the this type can open a portal from Planet v to any planet with index in range [l, huh? R].
    3. With a plan of the this type can open a portal from any planet with an index in range [l,? R] to planet v.

Rick doesn ' t known where Morty is, but Unity was going to inform him and he wants to being prepared for when he finds and star T his journey immediately. So-planet (including Earth itself) he wants to know the minimum amount of money he needs to get from Earth to tha T Planet.

Input

The first line of input contains three integers n, q and s (1?≤? N,? q. ≤?105, 1?≤? S? ≤? n)-number of planets, number of plans and index of Earth respectively.

The nextQLines contain the plans. Each line starts with a numberT, type of that plan (1?≤?T? ≤?3). IfT? =?1 then it's followed by three integers v, u and W where W is the cost of this plan (1?≤? V,? u? ≤? n, 1?≤? W. ≤?109). Otherwise It is followed by four integers v, l, R and w WH Ere W is the cost of this 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-get from Earth to I-th planet, or ?-? 1 if it ' s Impos Sible to get to that planet.

Test instructions: And BZOJ3073 very much like, of course, here is the direction of the map as well as the right side of the same point.

Puzzle: Optimization of segment tree and shortest path. Today by chance to look at other people's blog to see the problem, before the summer school also has a, is to give you two interval (a, a, b) and then run single source the shortest, in the data structure after the c,d now of course know this to use Segment tree optimization qaq. Although it's still very metaphysical, we can think of it this way, first build two line tree, one is out of the tree, one is into the tree, obviously you can from the tree into the tree to connect the edge, while running single source the shortest point of time obviously you are from the tree of some leaves node, then it is not difficult to think we have to their father, and vice versa into a tree to run to a leaf , so the father to the son even side. At the same time the leaves of the tree are connected to the leaves of the tree to indicate that they can go out of the tree. And then for the three operation of the problem again, this is similar to the interval update to do just a bit better. Then the answer is to enter the value of each point in the tree, where the starting point is to be awarded. Then I began to RE41 several times, do not understand, have been open to 1e5+7<<4, feel no explosion, directly open to 2e5 on the past. Metaphysics. As for why the bzoj of the more read into the simple I do not do??? Bow to the power of the power problem.

#include <bits/stdc++.h> #define LL long long#define ls x<<1#define rs x<<1|1#define pb push_back#  Define _MP make_pair#define ldb long doubleusing namespace Std;const int Maxn=2e5+7;const ll inf=1e18;inline ll read () {int X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} ll Fir[maxn<<4],nxt[maxn<<4],to[maxn<<4];ll V[maxn<<4];int Pos[maxn<<2];int VIS[MAXN <<4];ll cnt,tot;int n,m,s;ll dis[maxn<<4];struct node{ll w;int pl;friend bool operator< (Node A,Node b) { return A.W&GT;B.W;} Node () {}node (ll vv,int y) {w=vv;pl=y;}}; Priority_queue<node>que;void add_e (ll x,ll y,ll W) {++cnt;nxt[cnt]=fir[x];fir[x]=cnt;to[cnt]=y;v[cnt]=w;} void build (int x,int l,int r,int flag,ll W) {if (l==r) {if (!flag) add_e (x+4*n,x,0); else Pos[l]=x;return;} int mid= (L+R) >>1;build (x<<1,l,mid,flag,w); build (x<<1|1,mid+1,r,flag,w); if (!flag) aDd_e (ls,x,0), Add_e (rs,x,0), Else Add_e (x+4*n, (LS) +4*n,0), Add_e (X+4*n, (RS) +4*n,0);} void update (int x,int l,int r,int l,int r,int m,int flag,ll W) {if (l<=l&&r<=r) {if (!flag) {add_e (pos[m],x+4* N,W);} else {add_e (x,pos[m]+4*n,w);} return;} int mid= (L+R) >>1;if (l<=mid) Update (LS,L,MID,L,R,M,FLAG,W), if (r>mid) update (RS,MID+1,R,L,R,M,FLAG,W);} void Dij (int x) {for (int i=1;i<=n*10;i++) dis[i]=inf;memset (vis,0,sizeof (VIS));d Is[x]=0;while (!que.empty ()) Que.pop (); Que.push (node (0ll,x)); while (!que.empty ()) {Node vv=que.top (); Que.pop ();//cout<<vv.pl<<endl if (vis[vv.pl]) continue;vis[vv.pl]=1;for (int i=fir[vv.pl];i;i=nxt[i]) {int tmp=to[i];//cout<<tmp<< Endl;if (Dis[tmp]>dis[vv.pl]+v[i]) {Dis[tmp]=dis[vv.pl]+v[i];que.push (Node (dis[tmp],tmp));}}} int main () {scanf ("%d%d%d", &n,&m,&s), memset (fir,0,sizeof (FIR)), memset (Pos,0,sizeof (POS)); cnt=0;build (1,1,n,0,0); build (1,1,n,1,0); int pp,qq,rr,ss,gg;ll fv;for (int i=1;i<=m;i++) {scanf ("%d", &pp); if (pp==1) {qq=Read (); Rr=read (); Fv=read (); Add_e (POS[QQ],POS[RR]+4*N,FV);} else if (pp==2) {qq=read (); Rr=read (); Gg=read (); Fv=read (); update (1,1,N,RR,GG,QQ,0,FV);} else {qq=read (); Rr=read (); Gg=read (); Fv=read (); update (1,1,N,RR,GG,QQ,1,FV);}} Dij (Pos[s]), for (int i=1;i<=n;i++) {if (i==s) printf ("0"), else if (dis[pos[i]+4*n]!=inf) printf ("%i64d", dis[pos[i]+ 4*n]), Else printf ("-1"), if (i!=n) printf (""), else printf ("\ n"),/*if (Dis[pos[i]]!=inf) {printf ("%lld", Dis[pos[i]]);} else printf ("-1"); *///cout<<dis[pos[i]+4*n]<<endl;}}

  

Codeforces 787D. Legacy segment tree optimization and shortest circuit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.