uva1455 - Kingdom 线段树+并查集

从零完成 STM32 + ESP-01S + 云服务器 TCP 通信测试 STM32 通过 USART3 控制 ESP-01S 接入手机 2.4 GHz 热点,随后连接公网云服务器 8.152.204.86:9002。云端在同一 TCP 连接中连续收到 seq=1~6 的测试报文,5 个发送间隔平均约 5.4 秒,说明本次 TCP 上行链路已经打通。 阅读详情

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

  N个城市,road A B代表在AB间建一条路,line C询问y=C的水平线有多少个州和有多少个城市。

  因为询问有0.5的,所以首先把坐标乘以2。这里面x坐标是没用的。

  线段树维护两个值,当前点的state数和city数,因为询问的是点,所以并不用再向上传值(也不能传),这里唯一跟区间有关系的是记录当前区间要pushdown的state和city的增量addv1和addv2。

  road A B的时候找到两个集合的区间范围,分类讨论,哪些区间要增加city,哪些区间要增加state,哪些区间要减少。。。update完之后把两个集合合并。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 100010
#define MAXM 2000010
#define MAXNODE MAXN*4
#define MOD 999983
typedef long long LL;
using namespace std;
int T,N,M,A,B,ans1,ans2,x[MAXM],y[MAXM],ly[MAXM],ry[MAXM],p[MAXN],cnt[MAXN],state[MAXNODE],city[MAXNODE],addv1[MAXNODE],addv2[MAXNODE];
char str[10];
void init(){
    memset(state,0,sizeof(state));
    memset(city,0,sizeof(city));
    memset(addv1,0,sizeof(addv1));
    memset(addv2,0,sizeof(addv2));
    memset(p,0,sizeof(p));
    for(int i=1;i<=N;i++){
        p[i]=i;
        cnt[i]=1;
    }
}
int find(int x){
    return x==p[x]?x:p[x]=find(p[x]);
}
void pushdown(int o){
    addv1[o<<1]+=addv1[o];
    addv1[o<<1|1]+=addv1[o];
    addv2[o<<1]+=addv2[o];
    addv2[o<<1|1]+=addv2[o];
    state[o<<1]+=addv1[o];
    state[o<<1|1]+=addv1[o];
    city[o<<1]+=addv2[o];
    city[o<<1|1]+=addv2[o];
    addv1[o]=addv2[o]=0;
}
void update(int o,int L,int R,int ql,int qr,int v1,int v2){
    if(ql<=L&&qr>=R){
        state[o]+=v1;
        city[o]+=v2;
        addv1[o]+=v1;
        addv2[o]+=v2;
        return;
    }
    pushdown(o);
    int mid=(L+R)>>1;
    if(ql<=mid) update(o<<1,L,mid,ql,qr,v1,v2);
    if(qr>mid) update(o<<1|1,mid+1,R,ql,qr,v1,v2);
}
void query(int o,int L,int R,int q){
    if(L>=R){
        ans1+=state[o];
        ans2+=city[o];
        return;
    }
    pushdown(o);
    int mid=(L+R)>>1;
    if(q<=mid) query(o<<1,L,mid,q);
    if(q>mid) query(o<<1|1,mid+1,R,q);
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        int MAXY=-1;
        for(int i=1;i<=N;i++){
            scanf("%d%d",&x[i],&y[i]);
            x[i]<<=1;
            y[i]<<=1;
            ly[i]=ry[i]=y[i];
            MAXY=max(MAXY,y[i]);
        }
        init();
        for(int i=1;i<=N;i++) update(1,0,MAXY,y[i],y[i],1,1);
        scanf("%d",&M);
        while(M--){
            scanf("%s",str);
            if(str[0]=='r'){
                scanf("%d%d",&A,&B);
                A++;
                B++;
                int pa=find(A),pb=find(B);
                if(pa==pb) continue;
                if(ly[pa]<ly[pb]) update(1,0,MAXY,ly[pa],ly[pb]-1,0,cnt[pb]);
                if(ly[pb]<ly[pa]) update(1,0,MAXY,ly[pb],ly[pa]-1,0,cnt[pa]);
                if(ry[pa]<ry[pb]) update(1,0,MAXY,ry[pa]+1,ry[pb],0,cnt[pa]);
                if(ry[pb]<ry[pa]) update(1,0,MAXY,ry[pb]+1,ry[pa],0,cnt[pb]);
                if(ry[pa]+1<ly[pb]) update(1,0,MAXY,ry[pa]+1,ly[pb]-1,1,0);
                if(ry[pb]+1<ly[pa]) update(1,0,MAXY,ry[pb]+1,ly[pa]-1,1,0);
                if(max(ly[pa],ly[pb])<=min(ry[pa],ry[pb])) update(1,0,MAXY,max(ly[pa],ly[pb]),min(ry[pa],ry[pb]),-1,0);
                ly[pb]=min(ly[pa],ly[pb]);
                ry[pb]=max(ry[pa],ry[pb]);
                cnt[pb]+=cnt[pa];
                p[pa]=p[pb];
            }
            else{
                double t;
                int line;
                scanf("%lf",&t);
                line=(int)(t*2+0.5);
                ans1=ans2=0;
                if(line>=0&&line<=MAXY) query(1,0,MAXY,line);
                printf("%d %d\n",ans1,ans2);
            }
        }
    }
    return 0;
}


ResNet改进(7):添加 SK注意力机制 实现 SK 注意力机制。 阅读详情

相关推荐

基于Ventoy制作单个U盘引导Windows+Linux+黑苹果macOS

一款国内高手开发的一个名为Ventoy的开源多系统启动U盘制作工具,官方链接:https://www.ventoy.net/cn/index.html Ventoy 特点 不影响U盘日常普通使用 直接从ISO文件启动,无需解开 支持MBR和GPT分区格式(1.0.15+) 支持超过4GB的ISO文件 支持大部分常见操作系统, 已测试650+ 个ISO文件 不仅仅是启动,而是完整的安装过程 版本升级时数据不会丢失 Ventoy 待改进 不支持macOS和ChromeOS 准备2个空的U.

wybarcelona的博客 3万+

UVA 1455 - Kingdom(线段树+并查集)

UVA 1455 - Kingdom 题目链接 题意:给定一些城市坐标点,连在一起的城市称为一个州,现在用两种操作,road表示把城市a,b建一条路,line表示询问一个y轴上穿过多少个州,和这些州共包含多少个城市 思路:利用并查集维护每个州的上界和下界还有城市个数,然后每次加进一条路的时候,根据两个集合的位置可以处理出区间的州和城市数如何进行加减,然后利用线段树搞就可以了

Remilia's 1338

Zabbix安装与部署 系统环境:centos7.0

Zabbix安装与部署 系统环境:centos7.0

weixin_42485027的博客 2604

UVA 1455 Kingdom (线段树 + 并查集 + 离散化)

题意: 给你n 个城市,初始时,没有任何双向道路相连接。 两种操作: 1. 将A点和B点连接。 2.询问一条y = C的水平线 和多少个连通块 多少个点连接。 思路: 因为询问只是涉及到y 坐标。并且数据较大。 我们直接存下所有的y 坐标 离散化一下。 然后就可以建立线段树了。 线段树维护两个值 一个时连通块个数,一个是点的个数。 操作1 ,我们维护每个连通块 y 坐标最大和最

aozil_yang的博客 417

保姆级教程:在DJI无人机遥控器上部署YOLOv8模型(基于MSDK 5.0与NCNN)

本文详细介绍了在DJI无人机遥控器上部署YOLOv8模型的保姆级教程,基于MSDK 5.0与NCNN框架实现边缘计算。通过视频流处理、模型转换与优化、多线程同步等关键技术,实现在遥控器端完成目标检测任务,适用于野外作业、应急响应等场景。

weixin_42561476的博客 257

HDMI与DisplayPort混搭方案:如何用Xilinx Video PHY Controller实现多协议视频接口转换

本文详细解析了如何利用Xilinx Video PHY Controller IP核实现HDMI与DisplayPort协议转换,涵盖混合视频接口架构设计、GT收发器动态重配置策略及跨协议时钟域处理方案。通过实战案例和配置技巧,帮助开发者构建高性能多协议视频转换系统,适用于医疗影像、工业控制等场景。

weixin_30776273的博客 452

uva 1455 - Kingdom(并查集+线段树)

题目链接:uva 1455 - Kingdom 题目大意:平面上又n个城市,初始时城市之间没有任何双向道路相连,要求一次执行指令。 road A B :在城市A和城市B之间连接一条双向道路line C:询问一条y=C的水平线上穿过多少州和这些州总共有多少城市。 一个联通分量算一个州,C保证为小数部分为0.5的实数。 解题思路:线段树维护每个位置上州和城市的个数,并查集维护哪些城

不慌不忙、不急不躁 1529

01|无代码开发 | 如何使用Zion实现导航网站制作

以上就是我制作导航网站的心得,分享给大家,作为一个小白想要独立搭建一个网站还是比较困难的,而在Zion不仅可以用可视化的方法进行搭建,甚至还可以一键发布,不用考虑服务器域名啥的问题,真是太方便了。

2301_76262110的博客 1394

UVA 1455 Kingdom(线段树+并查集)

因为c是小数,那么把c向上取整,区间低加一就可以了,其他的线段树维护。#include #include #include #include using namespace std; const int maxn=100000+5; struct Interval{ int sumv[5][maxn*4],addv[5][maxn*4]; void clear() {

qq_33901573的博客 261

华为OD机试真题 新系统【Skill执行链完整性检测】

华为OD机试 新系统真题【Skill执行链完整性检测】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【Skill执行链完整性检测】

qq_45776114的博客 771

PyTorch生成式人工智能(25)——基于Transformer实现机器翻译

不同于循环神经网络 (RNN) 按顺序处理数据,Transformer 以并行的方式处理输入数据(例如句子)。这种并行处理提高了效率,但并未使其能够识别输入的顺序。为了解决这个问题,Transformer 向输入的词嵌入中添加了位置编码。这些位置编码是分配给输入序列中每个位置的独特向量,其维度与输入词嵌入对齐。本节将以英语到法语的翻译为例,通过介绍如何训练 Transformer 模型将英语句子转换为法语,深入理解 Transformer 的架构及自注意力机制的运作方式。

盼小辉丶的博客 2934

uva1455 - Kingdom

并查集、线段树

ACoder 250

Linux存储软件栈剖析之第5篇:F2FS文件系统

F2FS是三星专为NAND闪存设计的开源文件系统,通过异地更新、磨损均衡等机制优化闪存特性。其架构采用层次化存储管理(Block→Segment→Section→Zone)和多日志区域设计,基于数据热度分类(热/温/冷数据)提升性能。核心创新包括节点地址表(NAT)解决级联更新问题,以及基于成本的垃圾回收算法。F2FS通过检查点机制确保崩溃一致性,并利用并行访问能力充分发挥闪存性能优势,显著降低写放大效应,延长设备寿命。

指尖动听知识库 203

UVA1455 - Kingdom(并查集 + 线段树)

UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum (浮点数小数点一定是0.5) 查询y = fnum这条直线穿过了多少个州和city。州指的是连通的城市。 解题思路:用并查集记录城市之间是否连通,还有每个州的y的上下界。建立坐标y的

1006

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

UVa 1455 Kingdom 线段树 并查集

题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数以及这些连通分量点的总数 分析: 其实横坐标是没用的,首先可以先将纵坐标离散化。 用并查集维护点的连通性,连通分量的大小以及连通分量中纵坐标的最大值和最小值。 线段树中维护的是每条直线穿过的连通分...

weixin_30566111的博客 156

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

UVA 1455 Kingdom 线段树+并查集

并查集维护:y的最大最小值、城市数量 线段树维护:城市数量,洲数量 合并两个集合时,先在线段树上删除两个子集合的旧的信息,然后再将合并完的新集合更新到线段树。 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #incl...

weixin_34186950的博客 115

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

线段树+并查集uva1455Kingdom

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

龙崎的专栏 546

【UVA1455】Kingdom

题意   有nn个点和mm个操作,分为两种:   ①.roadroad uu vv表示连接uu号点和vv号点   ②.lineline CC(CC为小数,并且小数部分一定为0.5)表示询问y=Cy=C这条直线穿越了几个联通块,这些联通块的点数之和是多少   n≤105,m≤2∗105n≤10^5,m≤2*10^5 解法 并查集

清疚的博客 421

LA4730 UVA1455 线段树+并查集

以y轴建线段树 把坐标*2 初始化各节点 洲,城市 数目为0 并查集维护每一个连通块的 y坐标最大值最小值(x坐标没用)城市数目 合并时 如果是一个洲(城市数大于1) 线段树对应区间减掉该洲城市数 然后将两个洲合并,再在线段树对应区间增加大洲的城市数 (区间城市数不等于子区间城市数之和)#include <bits/stdc++.h>using namespace std; const in

CoderFly的博客 447

(intermediate) UVA 1455 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

We Can 632

LA4730,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 wit

SCUT_Pein 1494

【UVa 12436 Rip Van Winkle's Code 】线段树lazy标记

UVA12436 题目大意: 给你不超过400000次操作,操作区间范围为[1,250000],每次操作可以为: 1.A L R 表示[L,R]中从i=L开始,每个数依次增加i-L+1直到i=R。 2.B L R 表示[L,R]中从i=L开始,每个数依次增加R-i+1直到i=R。 3.C L R V 表示将[L,R]内的数全部设为V。 4.S L R 查询[L,R]内的数之和。 解法 我的做法是 ...

我是谁 --- 卡比兽 281

UVA12086 - Potentiometer(线段树/树状数组)

UVA12086 - Potentiometer(线段树/树状数组) 题目链接 题目大意:给你N个数字,然后有q个操作,操作类型:M代表修改某个位置的值为r,S代表查询某一段的数字和。 解题思路:线段树或者树状数组。 线段树 #include #include const int N = 8e5 + 5; int v[N]; int n; int Query (

996

别只当存储用!HDFS这些‘隐藏’命令能帮你省下大量运维时间

本文深入探讨了HDFS运维中五个被低估的高效命令与实战脚本,帮助管理员从被动清理转向主动预防。通过`-du`、`-count`等命令组合实现智能空间管理,利用`-setrep`优化副本策略,并分享元数据巡检与数据迁移的高级技巧,大幅提升Hadoop集群运维效率。

weixin_30570101的博客 290

UVA-11990(线段树)

题目链接:https://vjudge.net/problem/UVA-11990 过几天我再来写题解 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio&...

rainbowsea_1的博客 261
上一篇: hdu 3397 Sequence operation 线段树 区间和 连续1的个数
下一篇: poj2464 Brownie Points II 树状数组
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值