Counting Black
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9772   Accepted: 6307

Description

There is a board with 100 * 100 grids as shown below. The left-top gird is denoted as (1, 1) and the right-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 square is defined by left-top grid (x, y)

// and right-bottom grid (x+L-1, y+L-1) 2. BLACK x, y, L // Paint a black square on the board,

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

// and right-bottom grid (x+L-1, y+L-1) 3. TEST x, y, L // Ask for the number of black grids

// in the square (x, y)- (x+L-1, y+L-1)

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

Input

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

Output

For each TEST command, print a line 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
题目大意:输入n代表有n行指令,每行指令包括一个字符串cmd和三个数字x,y,nLen,如果cmd为“BLACK”表示将以x,y为左上角的边长为nLen的正方形里面的方块全部变成黑色,如果cmd为“WHITE”表示将以x,y为左上角的边长为nLen的正方形里面的方块全部变成白色,如果cmd为“TEST”则查询以x,y为左上角的边长为nLen的正方形里面的黑色方块共有多少个。
解题方法:而为树状数组。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int maze[][];
int color[][]; int lowbit(int x)
{
return x & -x;
} void add(int x, int y, int n)
{
if (color[x][y] == n)
{
return ;
}
color[x][y] = n;
for (int i = x; i <= ; i += lowbit(i))
{
for (int j = y; j <= ; j += lowbit(j))
{
maze[i][j] += n;
}
}
} int getsum(int x, int y)
{
int sum = ;
for (int i = x; i >= ; i -= lowbit(i))
{
for (int j = y; j >= ; j -= lowbit(j))
{
sum += maze[i][j];
}
}
return sum;
} int main()
{
int n;
int x, y, nLen;
char cmd[];
scanf("%d", &n);
memset(color, -, sizeof(color));
while(n--)
{
scanf("%s%d%d%d", cmd, &x, &y, &nLen);
if (strcmp(cmd, "BLACK") == )
{
for (int i = x; i < x + nLen; i++)
{
for (int j = y; j < y + nLen; j++)
{
add(i, j, );
}
}
}
else
{
if (strcmp(cmd, "WHITE") == )
{
for (int i = x; i < x + nLen; i++)
{
for (int j = y; j < y + nLen; j++)
{
add(i, j, -);
}
}
}
else
{
printf("%d\n", getsum(x + nLen - , y + nLen - ) - getsum(x - , y + nLen - ) - getsum(x + nLen - , y - ) + getsum(x - , y - ));
}
}
}
return ;
}
 
												

POJ 1656 Counting Black的更多相关文章

  1. 数黑格有多少个,模拟题,POJ(1656)

    题目链接:http://poj.org/problem?id=1656 #include <stdio.h> #include <iostream> #include < ...

  2. POJ 1971-Parallelogram Counting,暴力1063ms!

    Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...

  3. POJ Ant Counting DP

    dp[i][j]表示前i种蚂蚁组成元素个数为j的集合有多少种. 则dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ... + dp[i-1][ max(0,j-a[i]) ...

  4. POJ 1656

    #include<iostream>//chengdacaizi 08 .11. 12 #include<string> using namespace std; ][]={} ...

  5. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  6. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  7. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  8. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  9. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

随机推荐

  1. POJ 3017 Cut the Sequence (单调队列优化DP)

    题意: 给定含有n个元素的数列a,要求将其划分为若干个连续子序列,使得每个序列的元素之和小于等于m,问最小化所有序列中的最大元素之和为多少?(n<=105.例:n=8, m=17,8个数分别为2 ...

  2. MYSQL内置函数总结

    时间转换为字符串: SELECT date_format(Date, '%Y-%m-%d %H:%i:%s' ) AS TimeFROMtable o convert函数转换为字符串的时候不存在类型为 ...

  3. python之可迭代对象

    1. 可迭代对象是什么? 字面意思分析:可以重复的迭代的实实在在的东西 专业角度: 内部含有'__iter__'方法的对象,就是可迭代对象 2. 可迭代对象都有什么? list,dict(keys() ...

  4. Python——序列封包与序列解包

    一.序列封包与序列解包 把多个值赋给一个变量时,Python会自动的把多个值封装成元组,称为序列封包. 把一个序列(列表.元组.字符串等)直接赋给多个变量,此时会把序列中的各个元素依次赋值给每个变量, ...

  5. 【转】matlab练习程序(奇异值分解压缩图像)

    介绍一下奇异值分解来压缩图像.今年的上半年中的一篇博客贴了一篇用奇异值分解处理pca问题的程序,当时用的是图像序列,是把图像序列中的不同部分分离开来.这里是用的不是图像序列了,只是单单的一幅图像,所以 ...

  6. [LUOGU] P4251 [SCOI2015]小凸玩矩阵

    行列看成点,格子看成边,二分一个边权,删去大于它的边,新图上的最大流>k则答案可以更优,小于k则调整左边界. #include<algorithm> #include<iost ...

  7. html5新结构标签

    html5新结构标签 <header>  定义 section 或 page 的页眉,也就是定义头部的标签. <footer> 定义 section 或 page 的页脚. & ...

  8. mysql中的的按小数位截取

    format()函数返回类型是字符串,满三位会加一个逗号. 针对数字类型转换建议使用 convert或者cast函数,用法如下: format(param, 2) (不建议) convert(para ...

  9. LeetCode之Weekly Contest 93

    第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...

  10. Yii2 AR模型搜索数据条数不对,AR模型默认去重

    最近在做Yii2的项目时, 发现了一个yii2 自带的Ar模型会自动对搜索出来的字段去重. 默认去重字段: id,  其他字段暂没发现 1. 例如: public function fields { ...