当前位置:网站首页>D - Linear Probing- 并查集
D - Linear Probing- 并查集
2022-08-01 22:42:00 【秦小咩】
D - Linear Probing Editorial

/
Time Limit: 4 sec / Memory Limit: 1024 MB
Score : 400400 points
Problem Statement
There is a sequence A = (A_0, A_1, \dots, A_{N - 1})A=(A0,A1,…,AN−1) with N = 2^{20}N=220 terms. Initially, every term is -1−1.
Process QQ queries in order. The ii-th query (1 \leq i \leq Q)(1≤i≤Q) is described by an integer t_iti such that t_i = 1ti=1 or t_i = 2ti=2, and another integer x_ixi, as follows.
- If t_i = 1ti=1, do the following in order.
- Define an integer hh as h = x_ih=xi.
- While A_{h \bmod N} \neq -1AhmodN=−1, keep adding 11 to hh. We can prove that this process ends after finite iterations under the Constraints of this problem.
- Replace the value of A_{h \bmod N}AhmodN with x_ixi.
- If t_i = 2ti=2, print the value of A_{x_i \bmod N}AximodN at that time.
Here, for integers aa and bb, a \bmod bamodb denotes the remainder when aa is divided by bb.
Constraints
- 1 \leq Q \leq 2 \times 10^51≤Q≤2×105
- t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)ti∈{1,2}(1≤i≤Q)
- 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)0≤xi≤1018(1≤i≤Q)
- There is at least one ii (1 \leq i \leq Q)(1≤i≤Q) such that t_i = 2ti=2.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
QQ
t_1t1 x_1x1
\vdots⋮
t_{Q}tQ x_{Q}xQ
Output
For each query with t_i = 2ti=2, print the response in one line. It is guaranteed that there is at least one such query.
Sample Input 1 Copy
Copy
4 1 1048577 1 1 2 2097153 2 3
Sample Output 1 Copy
Copy
1048577 -1
We have x_1 \bmod N = 1x1modN=1, so the first query sets A_1 = 1048577A1=1048577.
In the second query, initially we have h = x_2h=x2, for which A_{h \bmod N} = A_{1} \neq -1AhmodN=A1=−1, so we add 11 to hh. Now we have A_{h \bmod N} = A_{2} = -1AhmodN=A2=−1, so this query sets A_2 = 1A2=1.
In the third query, we print A_{x_3 \bmod N} = A_{1} = 1048577Ax3modN=A1=1048577.
In the fourth query, we print A_{x_4 \bmod N} = A_{3} = -1Ax4modN=A3=−1.
Note that, in this problem, N = 2^{20} = 1048576N=220=1048576 is a constant and not given in input.
=========================================================================非常巧妙的一个题目,直接循环外加优化的话只会TLE一个点,本题正解是并查集,每当修改一个位置,就把这个位置的父亲节点和父亲节点+1位置合并,注意合并顺序,这样我们就完美的定位了下一个位置,十分精巧
# include<iostream>
# include<iomanip>
# include<algorithm>
# include<map>
# include<vector>
# include<set>
# include<cstring>
# pragma optimize(2)
# define mod 1048576
using namespace std;
typedef long long int ll;
int f[(1<<20)+10];
ll ans[(1<<20)+10];
bool book[1048576+10];
ll getf(int x)
{
if(x==f[x])
return x;
else
{
f[x]=getf(f[x]);
return f[x];
}
}
void hebing(int x,int y)
{
int t1=getf(x);
int t2=getf(y);
if(t1!=t2)
f[t1]=t2;
}
int main ()
{
for(int i=0; i<(1<<20); i++)
{
f[i]=i;
}
int t;
cin>>t;
while(t--)
{
ll x,y;
scanf("%lld%lld",&x,&y);
if(x==1)
{
ll pre=y;
y%=1048576;
ll now=getf(y);
ans[now]=pre;
book[now]=1;
hebing(now,(now+1)%1048576);
}
else
{
ll now=y%1048576;
if(!book[now])
{
cout<<-1<<'\n';
}
else
{
cout<<ans[now]<<'\n';
}
}
}
return 0;
}
边栏推荐
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
- 华为无线设备配置双链路冷备份(AP指定配置方式)
- leetcode刷题
- excel cell contian two words, seperated by a slash
- Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
- Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
- excel remove all carriage return from a cell
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
- 小程序中的多表联合查询
- y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)
猜你喜欢
![[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)](/img/26/4c3080f1b21efb9401d8c3a55bc15d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)

JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法

力扣第 304 场周赛复盘

npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)

SRv6 L3VPN的工作原理

13、学习MySQL 分组

编曲软件FL studio20.8中文版功能和作用

y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)

2022 edition of MySQL tutorial, top collection good, take your time

xctf attack and defense world web master advanced area webshell
随机推荐
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
excel edit a cell without double clicking
PAM 回文自动机
blender3.2.1 unit setting
解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
1391D. 505 状压dp
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
Postman 批量测试接口详细教程
PAM Palindromic Automata
选择合适的 DevOps 工具,从理解 DevOps 开始
dvwa 通关记录1 - 暴力破解 Brute Force
【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
文件查询匹配神器 【glob.js】 实用教程
从0到1:图文投票小程序设计与研发笔记
46.全排列
隔离和降级
得物客服热线的演进之路
excel split text into different rows