# 莫队算法

Dragon Ball Super has ﬁnished. As a fan of Dragon Ball, Ctr couldn’t forget any plot of it, especially the ﬁghting scene of Tournament of Power(力量大会). People who participate in the Tournament of Power will become the teammate with those who come from the same universe, even Son Goku and Frieza.

There are n people who may participate in the Tournament of Power, these people are numbered from 1 to n. Also there are m relations (ai,bi) which means person ai and person bi are come from the same universe.

Ctr deﬁnes the interest value of the Tournament of Power is the number of diﬀerent universes people, participate in Tournament of Power, come from.

Now, Ctr has q queries to ask you, each of which contains two integers: l,r. For each query you are required to tell him the interest value when people ( l,l+1,...,r ) participate in the Tournament of Power.

## 输入描述:




The ﬁrst line contains an integer number T, the number of test cases.

For each test case :

The first line contains two integer numbers n,m(1 ≤ n,m ≤ 105), the number of test cases.

The following m lines, each contains two integers ai,bi(1 ≤ ai,bi ≤ n), which means aith person and bith person are come from the same universe.

The next line contains an integer number q(1 ≤ q ≤ 105), the number of query.

The following q lines, each contains two integers l,r(1 ≤ l ≤ r ≤ n).

## 输出描述:

For each query print the answer.

## 输入

1
6 3
1 2
3 4
5 6
2
2 5
2 4

## 输出

3
2

n范围是100000，Q范围也是100000，所以暴力会超时。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;

struct node{
int l,r;//询问的区间
int id;//询问的编号
}q[100005];

int fa[100005];
int block;

int find(int x){
if(fa[x]!=x)fa[x]=find(fa[x]);
return fa[x];
}
bool cmp(const node &a,const node &b){
//按照l所在分块排序，如果在l同一块，r小的放前面
if((a.l/block)!=(b.l/block))return a.l<b.l;
return a.r<b.r;
}

int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
block=sqrt(n);
int i,j;
for(i=1;i<=n;i++)fa[i]=i;
while(m--){//建立并查集
int a,b;
scanf("%d%d",&a,&b);
int aa=find(a);
int bb=find(b);
if(aa!=bb){
fa[aa]=bb;
}
}
int Q;
scanf("%d",&Q);
for(int i=1;i<=Q;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+1,q+1+Q,cmp);
int vis[100005];//vis[i]表示父亲为i的点的个数
memset(vis,0,sizeof(vis));
int ans[100005];//第i个询问的答案
memset(ans,0,sizeof(ans));
for(int i=q[1].l;i<=q[1].r;i++){//预处理第一个询问
int f=find(i);
if(vis[f]==0){
//父亲为i的点的个数为0，说明进来了一个新的组，ans要++
ans[q[1].id]++;
}
vis[f]++;
}
int temp=ans[q[1].id];//temp记录第一个询问区间的ans，在下面的移动中不能直接在ans[q[i].id]上加减
int L=q[1].l,R=q[1].r;
int f;
for(i=2;i<=Q;i++){//点要先增再减

while(q[i].l<L){//查询区间左边界在指针左边，先左移指针，再加上指针所指的点，顺序不能换
L--;
f=find(L);
if(vis[f]==0) temp++;
vis[f]++;
}
while(q[i].r>R){
R++;
f=find(R);
if(vis[f]==0) temp++;
vis[f]++;
}
while(q[i].r<R){
f=find(R);
vis[f]--;
if(vis[f]==0) temp--;
R--;
}
while(q[i].l>L){//查询区间左边界在指针右边，先减去指针所指的点，再右移指针
f=find(L);
vis[f]--;
if(vis[f]==0) temp--;//如果父亲为i的点为0，说明少了一个分组
L++;
}
ans[q[i].id]=temp;
}
for(int i=1;i<=Q;i++){
printf("%d\n",ans[i]);
}
}
return 0;
} 

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120