首页 > 代码库 > codechef Closing the Tweets 题解

codechef Closing the Tweets 题解

Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father‘s laptop.

They saw it for the first time but were already getting bored to see a bunch of sentences having at most 140 characters each. The only thing they liked to play with it is, closing and opening tweets.

There are N tweets on the page and each tweet can be opened by clicking on it, to see some statistics related to that tweet. Initially all the tweets are closed. Clicking on an open tweet closes it and clicking on a closed tweet opens it. There is also a button to close all the open tweets. Given a sequence of K clicks by Jack, Evan has to guess the total number of open tweets just after each click. Please help Evan in this game.

Input

First line contains two integers N K, the number of tweets (numbered 1 to N) and the number of clicks respectively (1 ≤ NK ≤ 1000). Each of the following K lines has one of the following.

  • CLICK X , where X is the tweet number (1 ≤ X ≤ N)
  • CLOSEALL

Output

Output K lines, where the ith line should contain the number of open tweets just after the ith click.

Example

Input:
3 6
CLICK 1
CLICK 2
CLICK 3
CLICK 2
CLOSEALL
CLICK 1

Output:
1
2
3
2
0
1

单点更新的题目,记录一个开关数组就可以了。

这里使用bitset来做,bitset是非常好的容器。

最好还是不使用memset吧,因为memset只是清零,要赋予一定值的话,就可能会出错。

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <bitset>
using namespace std;

class ClosingtheTweets
{
public:
	ClosingtheTweets() 
	{
		int n, m;
		scanf("%d %d", &n, &m);
		bitset<1001> arr;

		char click[9];
		int num;

		int ans = 0;
		while (m--)
		{
			scanf("%s", click);
			if (click[3] == 'S')
			{
				arr.reset();
				ans = 0;
			}
			else
			{
				scanf("%d", &num);
				if (arr[num] == 0) ans++;
				else ans--;
				arr[num] = !arr[num];
			}
			printf("%d\n", ans);
		}
	}
};