当前位置:网站首页>洛谷P1196 银河英雄传说
洛谷P1196 银河英雄传说
2022-08-11 04:00:00 【CLH_W】
题目背景
公元 58015801 年,地球居民迁至金牛座 \alphaα 第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展。
宇宙历 799799 年,银河系的两大军事集团在巴米利恩星域爆发战争。泰山压顶集团派宇宙舰队司令莱因哈特率领十万余艘战舰出征,气吞山河集团点名将杨威利组织麾下三万艘战舰迎敌。
题目描述
杨威利擅长排兵布阵,巧妙运用各种战术屡次以少胜多,难免恣生骄气。在这次决战中,他将巴米利恩星域战场划分成 3000030000 列,每列依次编号为 1, 2,\ldots ,300001,2,…,30000。之后,他把自己的战舰也依次编号为 1, 2, \ldots , 300001,2,…,30000,让第 ii 号战舰处于第 ii 列,形成“一字长蛇阵”,诱敌深入。这是初始阵形。当进犯之敌到达时,杨威利会多次发布合并指令,将大部分战舰集中在某几列上,实施密集攻击。合并指令为 M i j,含义为第 ii 号战舰所在的整个战舰队列,作为一个整体(头在前尾在后)接至第 jj 号战舰所在的战舰队列的尾部。显然战舰队列是由处于同一列的一个或多个战舰组成的。合并指令的执行结果会使队列增大。
然而,老谋深算的莱因哈特早已在战略上取得了主动。在交战中,他可以通过庞大的情报网络随时监听杨威利的舰队调动指令。
在杨威利发布指令调动舰队的同时,莱因哈特为了及时了解当前杨威利的战舰分布情况,也会发出一些询问指令:C i j。该指令意思是,询问电脑,杨威利的第 ii 号战舰与第 jj 号战舰当前是否在同一列中,如果在同一列中,那么它们之间布置有多少战舰。
作为一个资深的高级程序设计员,你被要求编写程序分析杨威利的指令,以及回答莱因哈特的询问。
输入格式
第一行有一个整数 TT(1 \le T \le 5 \times 10^51≤T≤5×10
5
),表示总共有 TT 条指令。
以下有 TT 行,每行有一条指令。指令有两种格式:
M i j:ii 和 jj 是两个整数(1 \le i,j \le 300001≤i,j≤30000),表示指令涉及的战舰编号。该指令是莱因哈特窃听到的杨威利发布的舰队调动指令,并且保证第 ii 号战舰与第 jj 号战舰不在同一列。
C i j:ii 和 jj 是两个整数(1 \le i,j \le 300001≤i,j≤30000),表示指令涉及的战舰编号。该指令是莱因哈特发布的询问指令。
输出格式
依次对输入的每一条指令进行分析和处理:
如果是杨威利发布的舰队调动指令,则表示舰队排列发生了变化,你的程序要注意到这一点,但是不要输出任何信息。
如果是莱因哈特发布的询问指令,你的程序要输出一行,仅包含一个整数,表示在同一列上,第 ii 号战舰与第 jj 号战舰之间布置的战舰数目。如果第 ii 号战舰与第 jj 号战舰当前不在同一列上,则输出 -1−1。
输入输出样例
输入 #1复制
4
M 2 3
C 1 2
M 2 4
C 4 2
输出 #1复制
-1
1
说明/提示
战舰位置图:表格中阿拉伯数字表示战舰编号
上代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int n,x,y,f[3000005][2],la[3000005],fa[3000005];//f的两个存储0存储自己父亲,1存储自己在队伍的位置,la是所在队列的最后一个数
char p;
int read(){
//好像加不加都一样
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
int find(int x)//只用来快速找祖先
{
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
f[i][0]=i;//和后面fa是一样的,不过用途不同
f[i][1]=1;//自己是第一个
la[i]=i;//自己是最后
fa[i]=i;//初始化
}
while(n--)
{
scanf("%s",&p);x=read();y=read();
if(p=='M')
{
int fax=find(x),fay=find(y),k=f[la[fay]][1];//k是y所在列的最后一个数的序号,即此数列个数 。
for(int i=la[fax];;i=f[i][0])
{
f[i][1]+=k;//自己前方多了k个战舰
if(i==fax)break;//不再继续更新
}
f[fax][0]=la[fay];//更新自己的父亲用作后继更新
fa[fax]=fay;//直接换祖宗用作找祖先
la[fay]=la[fax];//y所在列的最后一个数变成x所在列的最后一个数
}
else
{
if(find(x)!=find(y)){
cout<<-1<<endl;continue;}//不是一个祖先
cout<<abs(f[x][1]-f[y][1])-1<<endl;//输出序号差值
}
}
return 0;
}
边栏推荐
- typedef defines the structure array type
- 【FPGA】abbreviation
- 【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
- [C Language] Getting Started
- Day20 FPGA 】 【 - block the I2C read and write EEPROM
- LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
- Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
- LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
- 【FPGA】设计思路——I2C协议
- Get Qt installation information: including installation directory and various macro addresses
猜你喜欢
Multi-serial port RS485 industrial gateway BL110
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
Binary tree related code questions [more complete] C language
2022-08-10 The sixth group Hiding spring study notes
LeetCode刷题第10天字符串系列之《125回文串验证》
这些云自动化测试工具值得拥有
Provincial level of Echart maps, as well as all prefecture-level download and use
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
Build Zabbix Kubernetes cluster monitoring platform
Get Qt installation information: including installation directory and various macro addresses
随机推荐
The development of the massage chair control panel makes the massage chair simple and intelligent
Get Qt installation information: including installation directory and various macro addresses
Map中的getOrDefualt方法
How to delete statements audit log?
Will oracle cardinality affect query speed?
华南师范宋宇老师课堂对话论文翻译
Getting Started with Raspberry Pi (5) System Backup
What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
Alibaba Cloud releases 3 high-performance computing solutions
Watch to monitor
【深度学习】基于卷积神经网络的天气识别训练
[C Language] Getting Started
The FTP error code list
Read the article, high-performance and predictable data center network
机器学习中什么是集成学习?
Element's BFC attribute
校园兼职平台项目反思
使用百度EasyDL实现智能垃圾箱
使用jackson解析json数据详讲