Kingdom uva+树状数组+并查集

Kingdom

There were n cities in an ancient kingdom. In the beginning of the kingdom, all cities were isolated. Kings ordered their subjects to construct roads connecting cities. A lot of roads were built with time. Every road was always constructed along the line segment between two cities. All cities are partitioned into disjoint components of cities by road-connectivity. A connected component of cities was called a state. A state consists of cities and roads connecting them.

A historical record tells a time sequence of road constructions in order. A road connecting two cities A and B doesn't intersect with other roads at a point except for A and B. Before construction, A and B may have belonged to the same state or different states. After construction, A and B would belong to a same state, i.e., two states would merge into a state if needed.

Prof. Kim, a historian, is concerned about the following question: How many states does a horizontal line (corresponding to the latitude of a specific place) pass by at a moment of the past? The figure below shows an example of a configuration of roads at some moment. A circle represents a city and a line segment represents a road between two cities. There are 3 states. A line with y = 4.5 passes by two states with total 8 cities and a line with y = 6.5 passes by one state with 5 cities.

\epsfbox{p4730.eps}

You are to write a program which handles the following two types of commands:


  • road A B

    A road between two cities A and B will be constructed. The road doesn't intersect with other roads at a point except for A and B. This is an informative command and your program does not need to respond.

  • line C

    This is a query. The program should output the number of states which a line y = C passes by and the total number of cities of them.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. The first line of each test case contains an integer n, the number of cities, where 1$ \le$n$ \le$100, 000. Each of the following n lines contains two integers x and y (0$ \le$xy$ \le$1, 000, 000), where (xy) represents the coordinate of a city. There is a single space between the integers. The cities are numbered from 0 to n - 1 in order. The next line contains an integer m, the number of commands, where 1$ \le$m$ \le$200, 000. Each of the following mlines contains a command, either ``road A B" or ``line C", where 0$ \le$A $ \neq$ B < n and C (0 < C < 1, 000, 000) is a real number of which the fractional part is always 0.5. There exists at most one road construction connecting a pair of cities and there exists at least one query per a test case.

Output 

Your program is to write to standard output. Print exactly one line for a query through all test cases. The line should contain two integers which represent the number of states and the total number of cities of them respectively.

The following shows sample input and output for three test cases.

Sample Input 

3 
10 
1 7 
5 7 
8 6 
3 5 
5 5 
2 3 
10 3 
7 2 
4 1 
11 1 
11 
road 0 1 
road 3 5 
line 6.5 
road 4 2 
road 3 8 
road 4 7 
road 6 9 
road 4 1 
road 2 7 
line 4.5 
line 6.5 
1 
100 100 
1 
line 100.5 
2 
10 10 
20 20 
2 
road 0 1 
line 15.5
解决方案:城市之间的联通可用并查集,y轴的区间的信息记录可用线段树,也可用树状数组,这里用树状数组。由于既查看洲的个数,及城市的个数,所以可用两个树状数组来维护,state,city。然后,为了查询避免小数,所以y轴值要加两倍。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MMAX 100003
using namespace std;
int fa[MMAX],Rank[MMAX],y1[MMAX],y2[MMAX];
int faset(int x)
{
    return x!=fa[x]?fa[x]=faset(fa[x]):x;
}///并查集,路径压缩
void U(int a,int b)
{
    int x=faset(a),y=faset(b);
    if(x==y) return ;
    else
    {
        fa[x]=y;
        Rank[y]+=Rank[x];
    }

}///a,b并在一起
struct node
{
    int C[MMAX],N;
    void init(int n){
    memset(C,0,sizeof(C));
    N=n;
    }
    int update(int x,int y,int add)
    {
        while(x>=1)
        {
            C[x]-=add,x-=x&(-1*x);
        }
        while(y>=1)
        {
            C[y]+=add,y-=y&(-1*y);
        }
    }///区间信息的修改,由于是区间,而数状数组只能对
    ///前缀区间修改,所以要把没涉及的区间还原
    int query(int x)
    {
        int sum=0;
        while(x<=N)
        {
            sum+=C[x],x+=x&(-x);
        }
        return x;
    }///单点查询,可查询每个点的覆盖大小
} city,state;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       int Q,n,a,b,_Max=0;
       char st[20];
       scanf("%d",&n);
       for(int i=0;i<n;i++){
        scanf("%d%d",a,b);
        b*=2;
        fa[i]=i;
        y1[i]=b;
        y2[i]=b;
        Rank[i]=1;
        _Max=max(_Max,b);
       }
       city.init(_Max),state.init(_Max);
       scanf("%d",&Q);
       for(int i=0;i<Q;i++){
        scanf("%s",st);
        if(st[0]=='r')
        {
            int a,b,x,y;
            scanf("%d%d",&a,&b);
            x=faset(a),y=faset(b);
            if(x==y) continue;
            else if(Rank[x]==1&&Rank[y]==1){
                U(a,b);
                int Fa=faset(a);
                y1[Fa]=min(y1[a],y1[b]);
                y2[Fa]=max(y2[a],y2[b]);
                state.update(y1[Fa]-1,y2[Fa],1);
                city.update(y1[Fa]-1,y2[Fa],Rank[Fa]);///这里y1为什么要减一,看回看update函数
            }///若a,b两点是孤立的点,直接更新
            else if(Rank[x]==1||Rank[y]==1){
                if(Rank[x]==1)swap(x,y);
                state.update(y1[x]-1,y2[x],-1);
                city.update(y1[x]-1,y2[x],-Rank[x]);
                U(a,b);
                int Fa=faset(a);
                y1[Fa]=min(y1[Fa],min(y1[a],y1[b]));
                y2[Fa]=max(y1[Fa],max(y2[a],y2[b]));
                state.update(y1[Fa]-1,y2[Fa],1);
                city.update(y1[Fa]-1,y2[Fa],Rank[Fa]);
            }///若有一个是孤立的点,应先把另一个不孤立的点在state,city上的信息清除,
            ///把两点连在一起再更新state,city信息
            else {
                  state.update(y1[x]-1,y2[x],-1);
                city.update(y1[x]-1,y2[x],-Rank[x]);
                  state.update(y1[y]-1,y2[y],-1);
                city.update(y1[y]-1,y2[y],-Rank[y]);
                U(a,b);
                int Fa=faset(b);
                  y1[Fa]=min(y1[Fa],min(y1[a],y1[b]));
                y2[Fa]=max(y2[Fa],max(y2[a],y2[b]));
                 state.update(y1[Fa]-1,y2[Fa],1);
                city.update(y1[Fa]-1,y2[Fa],Rank[Fa]);
                ///若两点都不孤立,先清除所有关于两点在state,city上的信息
                ///连接这两点,在更新。。。。
            }
        }
        else {
            int c,d;
            scanf("%d.%d",&c,&d);
            printf("%d %d\n",state.query(c*2+1),city.query(c*2+1));
        }
       }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值