poj_1656 Counting Black

Counting Black

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 8951

Accepted: 5775

题目链接:http://poj.org/problem?id=1656

Description

There is a board with 100 *100 grids as shown below. The left-top gird is denoted as (1, 1) and theright-bottom grid is (100, 100).


We may apply three commands to the board:


1.      WHITE  x, y, L    // Paint a white square on the board,


                           // the squareis defined by left-top grid (x, y)


                           // andright-bottom grid (x+L-1, y+L-1)




2.      BLACK  x, y, L    // Paint a black square on the board,


                           // the squareis defined by left-top grid (x, y)


                           // andright-bottom grid (x+L-1, y+L-1)




3.      TEST     x, y, L   // Ask for the number of black grids


                            // in thesquare (x, y)- (x+L-1, y+L-1)


In the beginning, all the grids on the board are white. We apply a series ofcommands to the board. Your task is to write a program to give the numbers ofblack grids within a required region when a TEST command is applied.

Input

The first line of the input isan integer t (1 <= t <= 100), representing the number of commands. Ineach of the following lines, there is a command. Assume all the commands arelegal which means that they won't try to paint/test the grids outside theboard.

Output

For each TEST command, print aline with the number of black grids in the required region.

Sample Input

5

BLACK 1 1 2

BLACK 2 2 2

TEST 1 1 3

WHITE 2 1 1

TEST 1 1 3

Sample Output

7

6

题意:

         BLACK x y l是指将第x行,y列到第x+l-1行,y+l-1列翻成黑色(初始化时为白色)

WHITE x y l是指将第x行,y列到第x+l-1行,y+l-1列翻成白色(初始化时为白色)

TEST x y l是指求从第x行,y列到第x+l-1行,y+l-1列中白色的有几个

 

解题思路:

      这个题目可以用树状数组来做,更新一个二维区间,查找一个二维区间两个函数,其中更新区间

 

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<cstring>
using namespace std;

int block[102][102];//默认为0,就是白色


int getbit(int x)
{
	return x&(-x);
}

//将block[x][y]到block[x+l-1][y+l-1]区间的值置反
void update(int x,int y,int l,char color)
{	int i,j;
	for(i=x;i<=x+l-1;i++)
	{
		for(j=y;j<=y+l-1;j++)
		{
			if(color=='B')
				block[i][j]=1;
			else if(color=='W')
				block[i][j]=0;
		}
	}
}

int count(int x,int y,int l)
{
	int i,j;
	int sum=0;
	for(i=x;i<=x+l-1;i++)
	{
		for(j=y;j<=y+l-1;j++)
			if(block[i][j]==1)
		sum++;
	}
	return sum;
}

int main()
{
	int command;
	int x,y,l;
	
	while(scanf("%d",&command)!=EOF)
	{
		//默认一开始值为0——全是白色的
		memset(block,0,sizeof(block));
		while(command--)
		{
			char str[6];
			cin>>str>>x>>y>>l;
			//getchar();
			//将block[x][y]到block[x+l-1][y+l-1]置为1
			if(str[0]=='B')
			{
				update(x,y,l,'B');
			}
			else if(str[0]=='W')
			{//将block[x][y]到block[x+l-1][y+l-1]置为0
				update(x,y,l,'W');
			}
			else if(str[0]=='T')
			{//输出block[x][y]到block[x+l-1][y+l-1]区间中为白色的个数
				printf("%d\n",count(x,y,l));
			}
			
		}
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值