莫队算法

Dragon Ball Super has finished. As a fan of Dragon Ball, Ctr couldn’t forget any plot of it, especially the fighting 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 defines the interest value of the Tournament of Power is the number of different 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 first 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

输入

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

输出

3
2

 

题意:n个点,给出m对,每对a,b表示a与b为同一组,给出Q个询问,x,y,问x到y之间,有几个不同的分组。

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;
} 

 

 

 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yz467796454/article/details/79953313
个人分类: 莫队
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭