CF178C3.Smart Beaver and Resolving Collisions
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The Smart Beaver from ABBYY has a lot of hobbies. One of them is constructing efficient hash tables. One of the most serious problems in hash tables is resolving collisions. The Beaver is interested in this problem very much and he decided to explore it in detail.
We assume that the hash table consists of h cells numbered from 0 to h - 1. Objects are added to and removed from it. Every object has its own unique identifier. In addition, every object has a corresponding hash value — an integer between 0 and h - 1, inclusive. When an object is added to the table, if the cell corresponding to the hash value of the object is free, then this object goes there. If the cell is already occupied by another object, there is a collision. When an object is deleted from the table, the cell which it occupied becomes empty.
The Smart Beaver has recently learned about the method of linear probing to resolve collisions. It is as follows. Let's say that the hash value for the added object equals t and cell t of the table is already occupied. Then we try to add this object to cell (t + m) mod h. If it is also occupied, then we try cell (t + 2·m) mod h, then cell (t + 3·m) mod h, and so on. Note that in some cases it's possible that the new object can not be added to the table. It is guaranteed that the input for this problem doesn't contain such situations.
The operation a mod b means that we take the remainder of the division of number a by number b.
This technique immediately seemed very inoptimal to the Beaver, and he decided to assess its inefficiency. So, you are given a sequence of operations, each of which is either an addition of an object to the table or a deletion of an object from the table. When adding a new object, a sequence of calls to the table is performed. Calls to occupied cells are called dummy. In other words, if the result of the algorithm described above is the object being added to cell (t + i·m) mod h (i ≥ 0), then exactly i dummy calls have been performed.
Your task is to calculate the total number of dummy calls to the table for the given sequence of additions and deletions. When an object is deleted from the table, assume that no dummy calls are performed. The table is empty before performing the operations, that is, initially it doesn't contain any objects.
ABBYY 的聪明海狸有许多爱好,其中之一就是构建高效的哈希表。哈希表中最严重的问题之一是解决冲突。海狸对这一问题非常感兴趣,并决定对其进行深入研究。
我们假设哈希表由 $ h $ 个单元格组成,编号从 $ 0 $ 到 $ h-1 $。对象可被添加或删除。每个对象都有其唯一的标识符。此外,每个对象还对应一个哈希值——一个介于 $ 0 $ 和 $ h-1 $(含端点)之间的整数。当向表中添加一个对象时,若该对象哈希值所对应的单元格为空,则该对象直接放入其中;否则,若该单元格已被另一对象占据,则发生一次冲突。当从表中删除一个对象时,它原先占据的单元格变为空。
聪明海狸最近学习了**线性探测法(linear probing)**来解决冲突,其方法如下:设待添加对象的哈希值为 $ t $,而表中第 $ t $ 号单元格已被占用,则尝试将该对象放入单元格 $ (t + m) \bmod h $;若该单元格也被占用,则继续尝试 $ (t + 2\cdot m) \bmod h $,再尝试 $ (t + 3\cdot m) \bmod h $,依此类推。注意,在某些情况下,新对象可能无法被加入表中;但本题保证输入数据不会出现此类情况。
运算 $ a \bmod b $ 表示 $ a $ 除以 $ b $ 所得的余数。
这种技术立刻被海狸认为是极不高效的,于是他决定评估其低效程度。因此,你将获得一系列操作,每项操作要么是向表中添加一个对象,要么是从表中删除一个对象。在添加一个新对象时,算法会按上述规则依次访问哈希表若干次。其中,对已被占用单元格的访问称为“伪访问(dummy call)”。换言之,若上述算法最终将对象放入单元格 $ (t + i\cdot m) \bmod h $(其中 $ i \geq 0 $),则恰好执行了 $ i $ 次伪访问。
你的任务是:对于给定的添加与删除操作序列,计算总共发生的伪访问次数。当从表中删除一个对象时,不产生任何伪访问。所有操作开始前,哈希表为空,即初始状态下表中不含任何对象。
输入格式
The first line of input contains three integers h, m and n (1 ≤ m < h), separated by spaces, where h is the size of the hash table, m is the number that is used to resolve collisions, n is the number of operations.
The following n lines contains the descriptions of the operations. Their execution order corresponds to the order in which they appear in the input file. Each operation is described by a single line. The operations are described as follows:
-
"+ id hash"
This is the format of the operation that adds an object to the table. The first character is "+" (ASCII 43), followed by a single space, then the object identifier id (0 ≤ id ≤ 109), then another space, and the hash value of the given object hash (0 ≤ hash < h). The object identifier and the hash value of this object are integers.
-
"- id"
This is the format of the operation that deletes an object from the table. The first character is "-" (ASCII 45), followed by a single space, then the object identifier id (0 ≤ id ≤ 109). The object identifier is an integer.
It is guaranteed that for all addition operations the value of id is unique. It is also guaranteed that the initial data is correct, that is, it's always possible to add an object to the hash table and there won't be any deletions of nonexisting objects.
The input limitations for getting 20 points are:
- 1 ≤ h ≤ 5000
- 1 ≤ n ≤ 5000
The input limitations for getting 50 points are:
- 1 ≤ h ≤ 5·104
- 1 ≤ n ≤ 5·104
The input limitations for getting 100 points are:
- 1 ≤ h ≤ 2·105
- 1 ≤ n ≤ 2·105
输入的第一行包含三个整数 h、m 和 n(满足 1≤m<h),以空格分隔,其中 h 表示哈希表的大小,m 是用于解决冲突的数值,n 是操作的总数。
接下来的 n 行描述了各项操作,其执行顺序与输入文件中出现的顺序一致。每项操作由单独一行描述,具体格式如下:
-
+ id hash
此为向哈希表中添加对象的操作。首字符为+(ASCII 码 43),其后跟一个空格,接着是对象标识符 id(0≤id≤109),再跟一个空格,最后是该对象的哈希值 hash(0≤hash<h)。对象标识符和哈希值均为整数。 -
- id
此为从哈希表中删除对象的操作。首字符为-(ASCII 码 45),其后跟一个空格,接着是对象标识符 id(0≤id≤109)。对象标识符为整数。
保证所有添加操作中的 id 值互不相同。同时保证初始数据合法,即:总能成功将对象加入哈希表,且不会出现对不存在对象的删除操作。
获取 20 分的输入限制为:
- 1≤h≤5000
- 1≤n≤5000
获取 50 分的输入限制为:
- 1≤h≤5⋅104
- 1≤n≤5⋅104
获取 100 分的输入限制为:
- 1≤h≤2⋅105
- 1≤n≤2⋅105
输出格式
Print a single number — the total number of dummy calls to the hash table.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams and the %I64d specifier.
输出一个整数——哈希表的伪调用总次数。
请注意,在 C++ 中不要使用 %lld 说明符读写 64 位整数。推荐使用 cin、cout 流以及 %I64d 说明符。
输入输出样例
输入#1
10 2 7 + 11 0 + 22 2 + 33 6 + 44 0 + 55 0 - 22 + 66 0
输出#1
7
输入#2
5 1 6 + 123 0 + 234 1 + 345 2 - 234 + 456 0 + 567 0
输出#2
4
输入解题思路,AI测评打分。不知道怎么写?