线段树+并查集uva1455Kingdom

本文深入探讨了信息技术领域的各个方面,包括前端开发、后端开发、移动开发、游戏开发等细分领域。从HTML、CSS、JavaScript等基础技术,到PHP、Python、Java等高级语言的应用,再到大数据开发、开发工具、嵌入式硬件等复杂场景,全面覆盖了信息技术的广泛范畴。同时,文章还涵盖了图像处理、音视频技术、AI处理、测试、基础运维等热门话题,为读者提供了一个全面的技术视野。
摘要由CSDN通过智能技术生成

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$x, y$ \le$1, 000, 000), where (x, y) 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 m lines 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

Sample Output 

0 0 
2 8 
1 5 
0 0 
1 2
这个题的数据范围好像有点问题,tle了好长时间

并查集维护集合中城市的数量和y坐标的最大最小值,然后线段树维护区间state和city的数量

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int maxm=2000010;
int n,m,pre[maxn],cnt[maxn],maxy[maxn],miny[maxn];
int ans_city,ans_state,MAXY,A,B;
char op[10];
double C;
struct P
{
    int x,y;
}p[maxn];
void init()
{
    for(int i=0;i<=n;i++)
    {
        pre[i]=i;
        cnt[i]=1;
        maxy[i]=miny[i]=p[i].y;
    }
}
int sumcity[maxn<<2],sumstate[maxn<<2];
int add1[maxn<<2],add2[maxn<<2];
void build()
{
    memset(sumcity,0,sizeof(sumcity));
    memset(sumstate,0,sizeof(sumstate));
    memset(add1,0,sizeof(add1));
    memset(add2,0,sizeof(add2));
}
void pushdown(int o,int l,int r)
{
    if(l>=r)return ;
    if(add1[o])
    {
        add1[o<<1]+=add1[o];
        add1[o<<1|1]+=add1[o];
        sumstate[o<<1]+=add1[o];
        sumstate[o<<1|1]+=add1[o];
        add1[o]=0;
    }
    if(add2[o])
    {
        add2[o<<1]+=add2[o];
        add2[o<<1|1]+=add2[o];
        sumcity[o<<1]+=add2[o];
        sumcity[o<<1|1]+=add2[o];
        add2[o]=0;
    }
}
void update(int o,int l,int r,int q1,int q2,int val1,int val2)
{
    if(q1<=l&&r<=q2)
    {
        add1[o]+=val1;
        add2[o]+=val2;
        sumcity[o]+=val2;
        sumstate[o]+=val1;
        return;
    }
    pushdown(o,l,r);
    int mid=(l+r)>>1;
    if(q1<=mid)update(o<<1,l,mid,q1,q2,val1,val2);
    if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,val1,val2);
}
void query(int o,int l,int r,int pos)
{
    if(l>=r)
    {
        ans_city=sumcity[o];
        ans_state=sumstate[o];
        return;
    }
    pushdown(o,l,r);
    int mid=(l+r)>>1;
    if(pos<=mid)query(o<<1,l,mid,pos);
    else query(o<<1|1,mid+1,r,pos);
}
int find(int x)
{
    if(x==pre[x])return x;
    return pre[x]=find(pre[x]);
}
void unite(int a,int b)
{
    int x=find(a),y=find(b);
    if(x==y)return ;
    update(1,0,MAXY,miny[y],maxy[y],-1,-cnt[y]);
    update(1,0,MAXY,miny[x],maxy[x],-1,-cnt[x]);
    cnt[x]+=cnt[y];
    miny[x]=min(miny[y],miny[x]);
    maxy[x]=max(maxy[x],maxy[y]);
    pre[y]=x;
    update(1,0,MAXY,miny[x],maxy[x],1,cnt[x]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        MAXY=-1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            p[i].x<<=1;
            p[i].y<<=1;
            MAXY=max(MAXY,p[i].y);
        }
        init();
        build();
        for(int i=1;i<=n;i++)
            update(1,0,MAXY,p[i].y,p[i].y,1,1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='r')
            {
                scanf("%d%d",&A,&B);
                A++,B++;
                unite(A,B);
            }
            else
            {
                scanf("%lf",&C);
                A=int(C*2+0.5);
                ans_city=ans_state=0;
                if(A>=0&&A<=MAXY)query(1,0,MAXY,A);
                printf("%d %d\n",ans_state,ans_city);
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值