POJ 1656 Counting Black
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 9772 | Accepted: 6307 |
Description

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
Output
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的更多相关文章
- 数黑格有多少个,模拟题,POJ(1656)
题目链接:http://poj.org/problem?id=1656 #include <stdio.h> #include <iostream> #include < ...
- POJ 1971-Parallelogram Counting,暴力1063ms!
Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...
- 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]) ...
- POJ 1656
#include<iostream>//chengdacaizi 08 .11. 12 #include<string> using namespace std; ][]={} ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
随机推荐
- javascript 和Jquery 互转
jQuery对象转成DOM对象: 两种转换方式将一个jQuery对象转换成DOM对象:[index]和.get(index); (1)jQuery对象是一个数据对象,可以通过[index]的方法,来得 ...
- WAMP安装提示缺少 msvcr100.dll文件解决方法
WAMP安装提示缺少wamp msvcr100.dll文件解决方法 因为wamp基于vs c++2010开发,需要提前安装这个组件才可以正常运行 微软官方组件下载地址: 32位:http://www. ...
- 一点对原生HTTP请求的理解与总结
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/10946165.html,多谢,=.=~ 术语 HTTP:超文本传输协议,规定Web浏览器如何从W ...
- python3.6 配置COCO API出错解决方案
使用Anaconda Prompt进行安装 问题出现的背景:在尝试使用mask-rcnn时,遇到了这个问题,最终解决掉了
- 融云红包全新升级,让App用户更便捷地用“钱”交流感情!
随着移动互联网的飞速发展,如何增强社交关系.留住用户的心已成为移动社交化时代各类App持续探索的问题,除了接入即时通讯的能力,众多社交平台开始通过趣味性十足的红包功能为App中的社交场景赋能.当即时通 ...
- Jquery库插件大全(工作中遇到总结)
Jquery UI所有插件下载:http://jqueryui.com/download/all/ Jquery layer灯箱等演示与帮助:http://sentsin.com/jquery/lay ...
- python基础之基本数据类型
1.int 整数 2.bool 布尔 3.str 字符串,一般放小量数据 4.list 列表,可以存放大量的数据 5.dict字典,以key:value的形式存储数据 6.set集合(数学) 7.tu ...
- Linux Cache 机制探究
http://www.penglixun.com/tech/system/linux_cache_discovery.html
- 01_13_Struts_默认Action
01_13_Struts_默认Action 1. 配置struts默认Action <package name="default" namespace="/&quo ...
- UIPopoverController
if (popOver == nil) { popOver = [[UIPopoverController alloc] initWithContentViewController:viewVC]; ...