计蒜客链接:https://nanti.jisuanke.com/t/41384

题目大意:给定n个数,从1到n排列,其中有q次操作,操作(1) 删除一个数字 // 操作(2)求这个数字之后第一个没有被删除的数字(包括自己)。

题解:考虑到实践复杂度问题,n范围是1e9,而q的范围是1e6,所以可以从q入手。用并查集的思路模拟出一个链表,用hashmap存储一个数距离它最近没有被删除的数(即并查集的父亲节点),初始化每个点的map的value存储下一个没有被删除的点,若删除一次,则x的父亲节点变为x+1的父亲,依次以并查集的思想递归下去。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#define inf 0x3f3f3f3f
#include<tr1/unordered_map>
using namespace std::tr1;
unordered_map<int,int> m;//hashmap
int findfa(int x){//并查集find函数
if(!m.count(x)){
return x;
}
else{
return m[x] = findfa(m[x]);
}
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
while(q--){
int z,x;
scanf("%d%d",&z,&x);
if(z==1){
m[x] = findfa(x+1);
}
else{
if(m.count(x) == 0){
printf("%d\n",x);
}
else{
int res = findfa(x);
if(res>n){
printf("%d\n",-1);
}
else{
printf("%d\n",res);
}
}
}
}
return 0;
}

2019 ICPC 徐州网络赛 B.so easy (并查集)的更多相关文章

  1. 2019 ICPC徐州网络赛 E. XKC's basketball team(二分)

    计蒜客题目链接:https://nanti.jisuanke.com/t/41387 题目大意:给定一组无序序列,从第一个数开始,求最远比这个数大m的数,与这个数之间相隔多少数字?如果没有输出-1,否 ...

  2. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  3. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  4. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

  5. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 B so easy

    题目链接:https://nanti.jisuanke.com/t/41384 这题暴力能过,我用的是并查集的思想,这个题的数据是为暴力设置的,所以暴力挺快的,但是当他转移的点多了之后,我觉得还是我这 ...

  7. 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛

    XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...

  8. HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...

  9. 2018 icpc 徐州网络赛 F Features Track

    这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...

随机推荐

  1. matlab HW求法笔记

    for i=1:9400 Sbox_out_11(i) = length(findstr(num2str(dec2bin(Sbox_out_1(i,:))),'1')); end

  2. koa文档笔记

    请求 get ctx.request.query // 查询对象 ctx.request.querystring // 查询字符串 ctx.query // 查询对象 ctx.querystring ...

  3. react-绑定this并传参的三种方式

    绑定this并传参的三种方式 在事件中绑定this并传参: <input type="button" value="在事件中绑定this并传参" onCl ...

  4. js上传文件工具类

    个人博客 地址:http://www.wenhaofan.com/article/20180808210417 jQuery.extend({ uploadUtil:function(){ } }); ...

  5. 安卓开发中遇到java.net.SocketException: Permission denied

    仅需在AndroidManifest.xml添加 <uses-permission android:name="android.permission.INTERNET" /& ...

  6. 浅谈radis

    1.概述 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API 从2010年3月15日起,Redis的开发工作由VM ...

  7. 你所不知道的Hello World[C++实现]

    要说OIer界内最简单的程序,那恐怕非Hello World莫属了, 那么这篇文章就介绍如何写Hello World(被打). 最简单的一种实现: #include <iostream> ...

  8. 2.4测试赛AC代码临时保存

    //H #include<cstdio> #include<cstdlib> #include<cstring> #include<stack> usi ...

  9. python面试的100题(14)

    32.请写出一个函数满足以下条件 该函数的输入是一个仅包含数字的list,输出一个新的list,其中每一个元素要满足以下条件: 1.该元素是偶数 2.该元素在原list中是在偶数的位置(index是偶 ...

  10. bugku sql2

    sql注入2 200 http://123.206.87.240:8007/web2/ 全都tm过滤了绝望吗? 提示 !,!=,=,+,-,^,%