BZOJ3073 - [PA2011]Journeys

Portal

Description

一个\(n(n\leq5\times10^5)\)个点的无向图,其上的边由\(m(m\leq10^5)\)条信息所描述:\((L_1,R_1,L_2,R_2)\)表示\(\forall u\in[L_1,R_1],v\in[L_2,R_2]\),存在无向边\((u,v)\)。求\(s\)到每个点的最短路长度。

Solution

依然是线段树优化建图。
Cf787D建立两棵线段树,在其之间连边。每条信息对应\(O(log^2n)\)条边。

Code

//[PA2011]Journeys
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
inline char gc()
{
    static char now[1<<16],*s,*t;
    if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}
    return *s++;
}
inline int read()
{
    int x=0; char ch=gc();
    while(ch<'0'||'9'<ch) ch=gc();
    while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x;
}
inline int min(int x,int y) {return x<y?x:y;}
const int N=5e5+10;
int n,m,s;
const int N1=15e5+10;
int cnt,rt1,rt2,ch[N1][2];
vector< pair<int,int> > son[N1];
inline void edAdd(int u,int v,int w) {son[u].push_back(make_pair(v,w));}
void bldTr1(int &p,int L0,int R0)
{
    if(L0==R0) {p=L0; return;}
    p=++cnt;
    int mid=L0+R0>>1;
    bldTr1(ch[p][0],L0,mid);
    bldTr1(ch[p][1],mid+1,R0);
    edAdd(p,ch[p][0],0),edAdd(p,ch[p][1],0);
}
void bldTr2(int &p,int L0,int R0)
{
    if(L0==R0) {p=L0; return;}
    p=++cnt;
    int mid=L0+R0>>1;
    bldTr2(ch[p][0],L0,mid);
    bldTr2(ch[p][1],mid+1,R0);
    edAdd(ch[p][0],p,0),edAdd(ch[p][1],p,0);
}
int c,optL,optR;
vector<int> t[2];
void getT(int p,int L0,int R0)
{
    if(optL<=L0&&R0<=optR) {t[c].push_back(p); return;}
    int mid=L0+R0>>1;
    if(optL<=mid) getT(ch[p][0],L0,mid);
    if(mid<optR) getT(ch[p][1],mid+1,R0);
}
const int INF=0x3F3F3F3F;
int dst[N1];
queue<int> Q;
void SPFA(int s)
{
    memset(dst,0x3F,sizeof dst);
    dst[s]=0; Q.push(s);
    while(!Q.empty())
    {
        int u=Q.front(); Q.pop();
        for(int i=0;i<son[u].size();i++)
        {
            int v=son[u][i].first,w=son[u][i].second;
            if(dst[u]+w<dst[v]) dst[v]=dst[u]+w,Q.push(v);
        }
    }
}
int main()
{
    n=read(),m=read(),s=read();
    cnt=n;
    bldTr1(rt1,1,n); bldTr2(rt2,1,n);
    while(m--)
    {
        int L1=read(),R1=read(),L2=read(),R2=read();
        optL=L1,optR=R1,t[c=0].clear(); getT(rt2,1,n);
        optL=L2,optR=R2,t[c=1].clear(); getT(rt1,1,n);
        for(int i=0;i<t[0].size();i++)
            for(int j=0;j<t[1].size();j++) edAdd(t[0][i],t[1][j],1);
        optL=L2,optR=R2,t[c=0].clear(); getT(rt2,1,n);
        optL=L1,optR=R1,t[c=1].clear(); getT(rt1,1,n);
        for(int i=0;i<t[0].size();i++)
            for(int j=0;j<t[1].size();j++) edAdd(t[0][i],t[1][j],1);
    }
    SPFA(s);
    for(int i=1;i<=n;i++) printf("%d\n",dst[i]);
    return 0;
}
posted @ 2018-05-06 15:52  VisJiao  阅读(223)  评论(0编辑  收藏  举报