2025-12-22
线段树优化建图。
把一次加边存在区间上(类似线段树分治的思路)
处理答案:
- 类似
dijstra每次找dis最小的点去松弛。 - 用并查集优化
bfs,top表示往右第一个待入队点。
注:记得删边,每个区间最多贡献一次。
代码
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5,MX=2e9;
int dis[N],top[N],q[N],sg[N<<2],tp=1,tr[N<<2];;
struct{pair<int,int> to;int ne;}b[N*20];
int find(const int x){return top[x]==x?x: top[x]=find(top[x]);}
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((L+R)>>1)
void push(const int x,const int L,const int R,const int l,const int r,const pair<int,int> i){
if(R<l||r<L) return;
if(l<=L&&R<=r) b[++tp]={i,tr[x]},tr[x]=tp;
else push(ls,L,mid,l,r,i),push(rs,mid+1,R,l,r,i);
}
void update(const int x,const int L,const int R,const int id){
for(int i=tr[x];i;i=b[i].ne)
for(int j=find(b[i].to.first);j&&j<=b[i].to.second;)
dis[j]=dis[id]+1,q[++tp]=j,top[j]=top[j+1],j=find(j);
tr[x]=0;
if(L<R) id<=mid?update(ls,L,mid,id): update(rs,mid+1,R,id);
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,m;
cin>>n>>m>>q[1];
for(int l,r,x,y;m--;) cin>>l>>r>>x>>y,push(1,1,n,l,r,{x,y}),push(1,1,n,x,y,{l,r});
for(int i=1;i<=n;i++) top[i]=i;
top[q[1]]=q[1]+1,tp=1;
for(int j=1;j<=tp;j++) update(1,1,n,q[j]);
for(int i=1;i<=n;i++) cout<<dis[i]<<"\n";
}