A8511 | Smart Beaver and Resolving Collisions
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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<=10^{9})$ , 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<=10^{9})$ . 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·10^{4}$
- $1<=n<=5·10^{4}$
The input limitations for getting 100 points are:
- $1<=h<=2·10^{5}$
- $1<=n<=2·10^{5}$
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<=10^{9})$ , 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<=10^{9})$ . 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·10^{4}$
- $1<=n<=5·10^{4}$
The input limitations for getting 100 points are:
- $1<=h<=2·10^{5}$
- $1<=n<=2·10^{5}$
输出格式
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.
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.
输入输出样例
输入 #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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted