Algorithm: An Application of Bitmap Data Structure

0x00: General Introduction

Bitmap is a very useful data structure which is widely used in indexing, data compressing and other situations. It doesn't only demand little memory, but also has an optimized time performance.

0x01: Lead-In: Company Problem

Description

There's a company with a staff of \(n\) numbered from \(1\) to \(n\). Because the number of staff is enormous, you need to write a management system for them.

When the staff start work or leave, they are ought to check in or check out a code in the system. Also, they are capable of updating their codes at any time. When it's time to get off work, all staff will be logged out of the system automatically.

The administrator of the company may want to know how many staff are working and which code is used for logging to the system of any staff.

Input Format

In the first line, two integers \(n\), \(m\) are given.

Each of the following \(m\) lines contains one of these contents:

I a c means number \(a\) staff logs in to the system using the code \(c\). If \(a\) is logged in, assign \(c\) to his or her code.

Oa means staff numbered \(a\) logs out of the system. If \(a\) isn't logged in, the operation is invalid.

C means it's time to get off work. All staff log out of the system.

N means inquiring how many staff are working.

Qa means making an inquiry about the code of the staff numbered \(a\). If the specific staff is logged out, answer \(-1\).

Output Format

Answer an integer, which is the sum of all inquiries beginning with N and Q.

int is supposed to be used to store that value and don't care about the overflowed part.

Sample Input

10 8
I 1 2
I 1 4
Q 1
C
I 2 4
O 2
Q 2
N

Sample Output

3

Clarification

When asked Q 1, you are supposed to return \(4\)

When asked Q2, you are supposed to return \(-1\) since the staff numbered \(4\) is logged out

When asked N, you are supposed to return \(0\) since all the staff have been logged out

The sum of these three inquires is \(3\)

Limitation

Input Data Scale

\(n \le 10,000,000\)

\(m \le 1,000,000\)

\(1\le a \le n\)

Each code is within the range of \([0,2^{31})\)

Time Limitation

2 secs per test case

Memory Limitation

256 Megabytes in total

0x02: Brute-Force Algorithm

Firstly, I use map in C++ STL to represent a pair of the correspondence between one staff and its code.

Consider an example that repeats logging out all staff of the system: each logout_all operation needs \(O(n)\), so the worst case is \(O(nm)\) which will exceed the time limitation.

0x03: Using Bitmap

To reduce the unnecessary time consumption when logging out all the staff, we can use one bit to represent whether one staff is working or not, since we can erase one binary digit in \(O(1)\).

In C++ STL, we have bitset to store a fixed-size sequence of \(N\) bits. Considering \(n\) is determined during the runtime, we have to design a dynamic data class. Among many binary functions, I referred to some bits hacks. You can check it out at the end of the text.