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. Jmeter进行接口的性能测试

    一.录制Jmeter脚本 录制Jmeter脚本有两种方法,一种是设置代理:一种则是利用badboy软件,badboy软件支持导出jmx脚本. 这里我们介绍第二种方法,利用badboy录制脚本,然后导出 ...

  2. Web端 页面跳转 页面传值

    要把  Default.aspx 页面    TextBox1中的值    传值到   Default.aspx   Label上去 C#服务端跳转页面 Response    响应请求对象 Redi ...

  3. Spark集锦

    1 Spark官网  http://spark.apache.org/ 2 Spark书籍 http://down.51cto.com/tag-spark%E4%B9%A6%E7%B1%8D.html

  4. springboot autoconfig

    springboot自动配置的核心思想是:springboot通过spring.factories能把main方法所在类路径以外的bean自动加载 springboot starter验证 我在spr ...

  5. BCB:WebBrowser 控件说明

      控件文件:system32\shdocvw.oca  shdocvw.dll 注册:regsvr32 shdocvw.dll WebBrowser 是 IE 内核做的 VB 控件, WebBrow ...

  6. 解决cocos2dx 打包lua环境搭建问题( ImportError: No module named Cheetah.Template)

    将c++ 封装成lua调用时,显示一下错误: PYTHON_BIN not defined, use current python. generating userconf.ini... Genera ...

  7. 2829: 高精A+B [1*+]

    题目描述 输入A和B,计算A+B的值 Input 两行数据,分别是A和B 0<=A<=1E200 0<=B<=10^200 Output A+B的结果 Sample Input ...

  8. 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel

    有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...

  9. 消息队列之 Kafka

    转 https://www.jianshu.com/p/2c4caed49343 消息队列之 Kafka 预流 2018.01.15 16:27* 字数 3533 阅读 1114评论 0喜欢 12 K ...

  10. matplotlib绘图(一)

    绘制这折现图 导入响应的包 import numpy as npimport pandas as pdfrom pandas import Series,DataFrame%matplotlib in ...