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
| #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std;
const int maxn=2e5+5;
struct QT{ int l,r,fk,i; }; bool cmp(const QT &a,const QT &b) {return a.fk<b.fk || a.fk==b.fk && a.r<b.r;}
int n,size,m,x[maxn],y[maxn]; QT Q[maxn];
int fa[2][maxn],ans[2]; int get(int ty,int x) {return (fa[ty][x]==x) ?x :fa[ty][x]=get(ty,fa[ty][x]) ;} void merge(int ty,int x,int y) { int t1=get(ty,x), t2=get(ty,y); if (t1!=t2) ans[ty]--, fa[ty][t1]=t2; }
int q,Ans[maxn]; int main() { scanf("%d %d %d",&n,&m,&q); size=sqrt(m); fo(i,1,m) scanf("%d %d",&x[i],&y[i]); fo(i,1,q) { scanf("%d %d",&Q[i].l,&Q[i].r); Q[i].fk=Q[i].l/size+1; Q[i].i=i; } sort(Q+1,Q+1+q,cmp); fo(i,1,n) fa[1][i]=i; fo(i,1,q) { if (Q[i].fk>Q[i-1].fk) { ans[0]=n; fo(j,1,n) fa[0][j]=j; fo(j,size*Q[i].fk,Q[i].r) merge(0,x[j],y[j]); } else { fo(j,max(Q[i-1].r+1,Q[i].fk*size),Q[i].r) merge(0,x[j],y[j]); } ans[1]=ans[0]; int en=min(Q[i].r,Q[i].fk*size-1); fo(j,Q[i].l,en) merge(1,get(0,x[j]),get(0,y[j])); Ans[Q[i].i]=ans[1]; fo(j,Q[i].l,en) fa[1][fa[0][x[j]]]=fa[0][x[j]], fa[1][fa[0][y[j]]]=fa[0][y[j]]; } fo(i,1,q) printf("%d\n",Ans[i]); }
|