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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include<bits/stdc++.h> using namespace std; const int max_n=400005; inline int read(){ int x=0;bool w=0;char c=getchar(); while(c<'0' || c>'9') w|=c=='-',c=getchar(); while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return w?-x:x; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10^48); } struct graph{ int hd[max_n],nx[max_n*2],to[max_n*2],ct; graph(){ct=1;} inline void add(int u,int v){ nx[++ct]=hd[u],hd[u]=ct,to[ct]=v; } }t[2],e;
int n,m,q,fa[2][max_n][22];
struct UnionFind{ int fa[max_n]; inline void init(){ for(register int i=1;i<=n*2;++i) fa[i]=i; } inline int find(int x){ if(fa[x]==x) return x; return fa[x]=find(fa[x]); } inline void merge(int x,int y){ fa[x]=y; } }uf;
int L[2][max_n],R[2][max_n],cntd;
inline void dfs(int tp,int u){ L[tp][u]=++cntd; for(register int i=1;i<=20;++i) fa[tp][u][i]=fa[tp][fa[tp][u][i-1]][i-1]; for(register int i=t[tp].hd[u];i;i=t[tp].nx[i]){ int v=t[tp].to[i]; if(v==fa[tp][u][0]) continue; dfs(tp,v); } R[tp][u]=cntd; }
inline int find(int tp,int x,int p){ if(tp){ for(register int i=20;i>=0;--i) if(fa[1][x][i]<=p) x=fa[1][x][i]; return x; } for(register int i=20;i>=0;--i) if(fa[0][x][i]>=p) x=fa[0][x][i]; return x; }
inline void krul(int tp){ uf.init(),cntd=0;int tot=1; if(!tp){ for(register int u=n;u>=1;--u) for(register int i=e.hd[u];i && tot<n;i=e.nx[i]){ int v=e.to[i]; int fu=uf.find(u),fv=uf.find(v); if(fu==fv || v<u) continue; uf.merge(fv,fu); t[0].add(fu,fv); fa[0][fv][0]=fu, ++tot; } fa[0][1][0]=1; dfs(0,1); } else{ for(register int u=1;u<=n;++u) for(register int i=e.hd[u];i && tot<n;i=e.nx[i]){ int v=e.to[i]; int fu=uf.find(u),fv=uf.find(v); if(fu==fv || v>u) continue; uf.merge(fv,fu); t[1].add(fu,fv); fa[1][fv][0]=fu, ++tot; } fa[1][n][0]=n; dfs(1,n); } }
struct point{ int x,y; }pos[max_n]; inline point makep(int a,int b){ point res; res.x=a,res.y=b; return res; } bool cmpt=0; inline bool cmp(point i,point j){ if(cmpt) return i.x<j.x; return i.y<j.y; }
struct KD_Tree{ struct dot{ point l,r; }a[max_n*4]; int L[max_n*4],R[max_n*4]; #define ls(x) (x<<1) #define rs(x) (x<<1|1)
inline void pushup(int x){ a[x].l.x=min(a[ls(x)].l.x,a[rs(x)].l.x),a[x].r.x=max(a[ls(x)].r.x,a[rs(x)].r.x); a[x].l.y=min(a[ls(x)].l.y,a[rs(x)].l.y),a[x].r.y=max(a[ls(x)].r.y,a[rs(x)].r.y); } inline void build(int x,int l,int r,int t){ L[x]=l,R[x]=r; if(l==r){ a[x].l=a[x].r=pos[l]; return; } int mid=(l+r)>>1; cmpt=t; nth_element(pos+l,pos+mid,pos+r+1,cmp); build(ls(x),l,mid,t^1); build(rs(x),mid+1,r,t^1); pushup(x); } inline bool ask(int x,point i,point j){ if(!x || a[x].l.x>j.x || a[x].r.x<i.x || a[x].l.y>j.y || a[x].r.y<i.y) return 0; if(a[x].l.x>=i.x && a[x].r.x<=j.x && a[x].l.y>=i.y && a[x].r.y<=j.y) return 1; if(L[x]==R[x]) return 0; return ask(ls(x),i,j)||ask(rs(x),i,j); } }kdt;
signed main(){ n=read(),m=read(),q=read(); for(register int i=1;i<=m;++i){ int u=read()+1,v=read()+1; e.add(u,v),e.add(v,u); } krul(0),krul(1); for(register int i=1;i<=n;++i) pos[i]=makep(L[0][i],L[1][i]); kdt.build(1,1,n,0); while(q--){ int x=read()+1,y=read()+1,l=read()+1,r=read()+1; x=find(0,x,l),y=find(1,y,r); write(kdt.ask(1,makep(L[0][x],L[1][y]),makep(R[0][x],R[1][y]))),putchar('\n'); } return 0; }
|