1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include<iostream> #include<vector> #include<algorithm> using namespace std; constexpr int N=2e5+10,M=4e5+10; int n,m,q; struct edge{int u,v;}a[M]; inline bool cmp1(const edge &x,const edge &y){ return max(x.u,x.v)<max(y.u,y.v); } inline bool cmp2(const edge &x,const edge &y){ return min(x.u,x.v)>min(y.u,y.v); } int boss[N<<1],tot1,tot2; int find(int x){return boss[x]==x?x:boss[x]=find(boss[x]);} int fa1[19][N<<1],fa2[19][N<<1]; int val1[N<<1],val2[N<<1]; vector<int> e1[N<<1],e2[N<<1]; int dfn1[N<<1],dfn2[N<<1],dfncnt,siz1[N<<1],siz2[N<<1],idfn2[N<<1]; void dfs1(int u){ dfn1[u]=++dfncnt; siz1[u]=1; for(int v:e1[u]){ dfs1(v); siz1[u]+=siz1[v]; } } void dfs2(int u){ dfn2[u]=++dfncnt; idfn2[dfncnt]=u; siz2[u]=1; for(int v:e2[u]){ dfs2(v); siz2[u]+=siz2[v]; } } int tot; struct ask{ int x,l,r,id,k; inline bool operator<(const ask &a)const{return x<a.x;} }b[N<<1]; #define lowbit(x) (x&-x) int tree[N<<1],cnt[N]; void modify(int x){ for(;x<=tot1;x+=lowbit(x)) ++tree[x]; } int query(int x){ int res=0; for(;x;x^=lowbit(x)) res+=tree[x]; return res; } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n>>m>>q; for(int i=1;i<=m;i++){ cin>>a[i].u>>a[i].v; ++a[i].u,++a[i].v; } sort(a+1,a+1+m,cmp1); tot1=n; for(int i=1;i<=n*2;i++) boss[i]=i; for(int i=1;i<=n;i++) val1[i]=i; for(int i=1;i<=m;i++){ int x=find(a[i].u),y=find(a[i].v); if(x==y) continue; boss[x]=boss[y]=++tot1; e1[tot1].push_back(x),e1[tot1].push_back(y); val1[tot1]=max(a[i].u,a[i].v); fa1[0][x]=fa1[0][y]=fa1[0][tot1]=tot1; } dfncnt=0; dfs1(tot1); for(int i=1;i<=__lg(tot1);i++) for(int j=1;j<=tot1;j++) fa1[i][j]=fa1[i-1][fa1[i-1][j]]; sort(a+1,a+1+m,cmp2); tot2=n; for(int i=1;i<=n*2;i++) boss[i]=i; for(int i=1;i<=n;i++) val2[i]=i; for(int i=1;i<=m;i++){ int x=find(a[i].u),y=find(a[i].v); if(x==y) continue; boss[x]=boss[y]=++tot2; e2[tot2].push_back(x),e2[tot2].push_back(y); val2[tot2]=min(a[i].u,a[i].v); fa2[0][x]=fa2[0][y]=fa2[0][tot2]=tot2; } dfncnt=0; dfs2(tot2); for(int i=1;i<=__lg(tot2);i++) for(int j=1;j<=tot2;j++) fa2[i][j]=fa2[i-1][fa2[i-1][j]]; for(int i=1,s,t,l,r;i<=q;i++){ cin>>s>>t>>l>>r; ++s,++t,++l,++r; for(int j=__lg(tot1);j>=0;j--) if(val1[fa1[j][t]]<=r) t=fa1[j][t]; for(int j=__lg(tot2);j>=0;j--) if(val2[fa2[j][s]]>=l) s=fa2[j][s]; if(dfn2[s]>1) b[++tot]={dfn2[s]-1,dfn1[t],dfn1[t]+siz1[t]-1,i,-1}; b[++tot]={dfn2[s]+siz2[s]-1,dfn1[t],dfn1[t]+siz1[t]-1,i,1}; } sort(b+1,b+1+tot); for(int i=1,j=1;i<=tot2;i++){ if(idfn2[i]<=n) modify(dfn1[idfn2[i]]); while(j<=tot&&b[j].x<=i){ cnt[b[j].id]+=b[j].k*(query(b[j].r)-query(b[j].l-1)); ++j; } } for(int i=1;i<=q;i++) cout<<(cnt[i]?"1\n":"0\n"); return 0; }
|