UESTC Training for Data Structures——G

Problem  G

Problem Description

  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.

  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. You may assume that 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(T<=10) 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<=n<=100000. Each of the following n lines contains two integers x and y (0<=x, y<=10^6), 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<=m<=200000. Each of the following m lines contains a command, either ``road A B" or ``line C", where 0<=A != B < n and C (0 < C < 10^6) 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

 

/*用并查集+线段树*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 1000005
#define INF 100000000
using namespace std;
int max(int a,int b)
{
    return a>b?a:b;
}
int min(int a,int b)
{
    return a<b?a:b;
}
struct data1
{
    int fa;  //节点的父亲
    int min;   //节点覆盖的y的最小值
    int max;  //节点覆盖的y的最大值
    int sum;  //节点的city数量
}a[100005];  //并查集的中的点
struct data2
{
    int st;  //覆盖的区域的开始点
    int en;  //覆盖的区域的结束点
    int state;  //覆盖的区域中有多少state
    int city;  //覆盖的区域中有多少city
} tree[4*N];  //线段树中的节点
int n;
void make_tree(int v,int st,int en)  //建树
{
    tree[v].st=st;
    tree[v].en=en;
    tree[v].state=tree[v].city=0;
    if(en==st) return;
    int mid=(st+en)>>1;
    make_tree(v<<1,st,mid);
    make_tree((v<<1)+1,mid+1,en);
}
void clear(int v)  //传递city state的值给孩子
{
    tree[v<<1].state+=tree[v].state;
    tree[v<<1].city+=tree[v].city;
    tree[(v<<1)+1].state+=tree[v].state;
    tree[(v<<1)+1].city+=tree[v].city;
    tree[v].state=0;
    tree[v].city=0;
}
void add(int v,int st,int en,int state,int city)  //更新区间 [st,en] 的state 及 city
{
    if(st>en) return;
    if(tree[v].st==st && tree[v].en==en)
    {
        tree[v].state+=state;
        tree[v].city+=city;
        return;
    }
    if(tree[v].state!=0 || tree[v].city!=0) clear(v);
    if(en<=tree[v<<1].en) add(v<<1,st,en,state,city);
    else if(tree[(v<<1)+1].st<=st) add((v<<1)+1,st,en,state,city);
    else
    {
        add(v<<1,st,tree[v<<1].en,state,city);
        add((v<<1)+1,tree[(v<<1)+1].st,en,state,city);
    }
}
int qu_state(int v,int x)  //询问点x处的state数量
{
    if(tree[v].st==tree[v].en && tree[v].en==x) return tree[v].state;
    if(tree[v].state!=0 || tree[v].city!=0) clear(v);
    int mid=(tree[v].st+tree[v].en)>>1;
    if(x<=mid) return qu_state(v<<1,x);
    return qu_state((v<<1)+1,x);
}
int qu_city(int v,int x)  //询问点x处的city数量
{
    if(tree[v].st==tree[v].en && x==tree[v].en) return tree[v].city;
    if(tree[v].state!=0 || tree[v].city!=0) clear(v);
    int mid=(tree[v].st+tree[v].en)>>1;
    if(x<=mid) return qu_city(v<<1,x);
    return qu_city((v<<1)+1,x);
}
void make_set()  //建立并查集并初始化
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].min,&a[i].min);
        a[i].max=a[i].min;
        a[i].fa=i;
        a[i].sum=1;
    }
}
int find_set(int x)  //找集合的代表
{
    if(a[x].fa!=x) a[x].fa=find_set(a[x].fa);
    return a[x].fa;
}
void merge_set(int x,int y)  //合并两个集合
{
    int i=find_set(x);
    int j=find_set(y);
    if(i==j) return;  //集合代表相同,不用合并
    add(1,a[i].min+1,a[i].max,-1,-a[i].sum);  //先将原来的覆盖区域撤掉
    add(1,a[j].min+1,a[j].max,-1,-a[j].sum);
    a[j].fa=i;  //合并两个集合
    a[i].max=max(a[i].max,a[j].max);
    a[i].min=min(a[i].min,a[j].min);
    a[i].sum+=a[j].sum;
    add(1,a[i].min+1,a[i].max,1,a[i].sum);  //再将现在的覆盖区域加进去
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        make_tree(1,1,N);
        make_set();
        int m;
        char s[10];
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",s);
            if(s[0]=='r')
            {
                int st,en;
                scanf("%d%d",&st,&en);
                merge_set(st,en);
            }
            else
            {
                double line;
                scanf("%lf",&line);
                int x=(int)(line+1);
                printf("%d %d\n",qu_state(1,x),qu_city(1,x));
            }
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值