热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UESTCTrainingforDataStructures——G

ProblemGProblemDescriptionTherewerencitiesinanancientkingdom.Inthebegi

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

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
#include
#include
#include
#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>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 
 


 


推荐阅读
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
author-avatar
yubu5208_176
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有