poj_1656 Counting Black

来源:互联网 发布:淘宝网的盈利模式分析 编辑:程序博客网 时间:2024/04/29 11:12

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]置为1if(str[0]=='B'){update(x,y,l,'B');}else if(str[0]=='W'){//将block[x][y]到block[x+l-1][y+l-1]置为0update(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;}


 

原创粉丝点击