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);
		}
	}
};

codechef Closing the Tweets 题解

时间: 2024-08-27 20:59:19

codechef Closing the Tweets 题解的相关文章

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi

codechef - Bytelandian gold coins 题解

In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to ma

codechef Ciel and Receipt题解

Tomya is a girl. She loves Chef Ciel very much. Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose total price is exactly p. The current menus of Ciel's restaurant are shown the following table. Name of Men

codechef The Lead Game 题解

The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient. The game consists of several rounds and in each round both players obtain a score, based on

codechef The Morning Commute 题解

The Morning Commute The Chef commutes to work every day using the city's underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the stati

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

codechef Three Way Communications 题解

The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they

Codechef Not a Triangle题解

找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,但是时间效率就是O(n*n*n)了,很慢. 不过既然是组合问题就必定可以使用排序后处理的方法降低时间效率的. 这里降低时间效率的方法是: 选一个最大的数c,然后选两个小数a和b,其中a < b,如果符合条件,那么两个数中间的数必定都可以作为b符合条件a + b < c 这样可以把时间效率降到O(n*n). 这个规律也不好找啊.要

codechef Subtraction Game 1题解

Subtraction Game 1 Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows. If all the numbers are equal, the game ends. Otherwise Select two numbers which are unequal Subtract the smaller number