CA爷的题解:http://blog.csdn.net/CreationAugust/article/details/50739132
#include<cstdio> #include<cstdlib> #include<algorithm> #include<set> using namespace std; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++; } inline void read(int &x) { char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } const int N=500005; struct edge{ int l,r; int next; }G[10*N]; int inum,head[4*N]; inline void add(int rt,int l,int r,int p){ G[p].l=l; G[p].r=r; G[p].next=head[rt]; head[rt]=p; } int M; inline void Build(int n){ for (M=1;M<n+2;M<<=1); } inline void insert(int s,int t,int a,int b) { for (s+=M-1,t+=M+1;s^t^1;s>>=1,t>>=1) { if (~s&1) add(s^1,a,b,++inum); if ( t&1) add(t^1,a,b,++inum); } } int n,m,P; int dis[N]; int Q[N],l,r; //set<int> Set; //typedef set<int>::iterator ITER; int Stk[N],pnt; int fat[N]; int Fat(int u){ return u==fat[u]?u:fat[u]=Fat(fat[u]); } int main() { int a,b,c,d,u; // ITER it; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(m); read(P); Build(n); for (int i=1;i<=m;i++) { read(a); read(b); read(c); read(d); insert(a,b,c,d); insert(c,d,a,b); } l=r=-1; Q[++r]=P; dis[P]=0; // for (int i=1;i<=n;i++) if (i!=P) Set.insert(i); for (int i=1;i<=n+1;i++) fat[i]=i; fat[P]=P+1; while (l<r) { int u=Q[++l]; for (int t=M+u;t;head[t]=0,t>>=1) for (int p=head[t];p;p=G[p].next) { // for (pnt=0,it=Set.lower_bound(G[p].l);it!=Set.end() && *it<=G[p].r;it++) // dis[*it]=dis[u]+1,Q[++r]=*it,Stk[++pnt]=*it; pnt=0; for (int i=Fat(G[p].l);i<=G[p].r;i=Fat(i+1)) dis[i]=dis[u]+1,Q[++r]=i,Stk[++pnt]=i; // while (pnt) Set.erase(Stk[pnt--]); while (pnt) fat[Stk[pnt]]=Stk[pnt]+1,pnt--; } } for (int i=1;i<=n;i++) printf("%d\n",dis[i]); return 0;
时间: 2024-10-09 17:58:56