长安大学第三节acm-icpc程序设计赛D

链接:https://www.nowcoder.com/acm/contest/102/D
来源:牛客网

题目描述
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
题意:问[L,R]区间内有多少不同宇宙的人
思路:其实就是并查集+莫队。莫队算法搜了一下,就是一个离线分块的算法,将所有询问区间先根据左端L进行分块,每块大概是根号n的长度,然后同一块内对R进行从小到大的排序,注意,同一块内的L不一定是严格从小到大的

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<set>
using namespace std;
#define mod (int)(1e9+9) 
int n,m,T,q,d;

struct node
{
    int l,r;
    int id;
    bool operator <(const node &a)const 
    {
        if(a.l/d==l/d) return r<a.r;
        return l/d<a.l/d;
    }
}ask[100005];
int answer[100005];
int num[100005];
int fa[100005];
int find(int x)
{
    if(x==fa[x])
    return x;
    return fa[x]=find(fa[x]);//这里一开始写成return find(fa[x]);就超时了
}
void  merge(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        fa[fx]=fy;
    }
}
int main()
{

    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) num[i]=0;
        d=sqrt(n);
        for(int i=1;i<=n;i++) fa[i]=i;      
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            merge(a,b);
        }       
        for(int i=1;i<=n;i++) fa[i]=find(i);//一开始忘记了这句wa了,怎么会忘记呢

        scanf("%d",&q);
        for(int i=1;i<=q;i++)
        {

            scanf("%d%d",&ask[i].l,&ask[i].r);
            ask[i].id=i;
        }
        sort(ask+1,ask+1+q);
        int l=1,r=0;
        int ans=0;
        for(int i=1;i<=q;i++)
        {

            int L=ask[i].l;
            int R=ask[i].r;
            while(r<R)
            {
                r++;
                if(num[fa[r]]==0) ans++;
                num[fa[r]]++;

            }
            while(r>R)
            {
                num[fa[r]]--;
                if(num[fa[r]]==0) ans--;
                r--;
            }   
            while(l>L)
            {
                l--;
                if(num[fa[l]]==0) ans++;
                num[fa[l]]++;

            }
            while(l<L)
            {
                num[fa[l]]--;
                if(num[fa[l]]==0) ans--;
                l++;
            }
            answer[ask[i].id]=ans;  

        }
        for(int i=1;i<=q;i++)
        printf("%d\n",answer[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Aurum_potestas_est/article/details/79994909
今日推荐