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分类 流传最广的一种分类: 初期: 一.基本算 ...
随机推荐
- GoAccess自动分割Nginx日志
GoAccess 是一款开源的网站日志实时分析工具.GoAccess 的工作方式很容易理解,就是读取和解析 Apache/Nginx/Lighttpd 的访问日志文件 access log,然后以更友 ...
- python+selenium之多表单切换
在Web应用中经常会遇到fram/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于fram/iframe表单内嵌套页面上的元素无法直接定位.这是需要通过swit ...
- window对象的几个重要方法
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Java ...
- redis的一些问题总结,转载自infoq
Redis是时下比较流行的Nosql技术.在优酷我们使用Redis Cluster构建了一套内存存储系统,项目代号蓝鲸.到目前为止集群有700+节点,即将达到作者推荐的最大集群规模1000节点.集群从 ...
- WINDOWS-基础:Thread.Sleep(0)
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7 12:00:00.000,如果我调用 ...
- WINDOWS-API:关于线程 GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId
{返回当前线程的虚拟句柄} GetCurrentThread: THandle; {返回当前线程 ID} GetCurrentThreadId: DWORD; {返回当前进程的虚拟句柄} GetCur ...
- 【转】再谈 最速下降法/梯度法/Steepest Descent
转载请注明出处:http://www.codelast.com/ 最速下降法(又称梯度法,或Steepest Descent),是无约束最优化领域中最简单的算法,单独就这种算法来看,属于早就“过时”了 ...
- 【转】Matlab的regionprops详解
matlab函数_连通区域 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下 ...
- mysql 在线添加字段
使用工具pt-online-schema-change #! /bin/bash stime=`date +%s` echo "增加字段开始测试时间为:`date +%H:%M:%S`&qu ...
- redis学习笔记(2)
redis学习笔记第二部分 --配置文件介绍 二,解析redis的配置文件redis.conf常见配置参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通 ...